Open History to see the last commit
1import { useLiveQuery } from "@tanstack/react-db";
2import { useState } from "react";
3
4import { useRequestLog } from "@/api/request-log";
5import { folderNotesQuery } from "@/db/on-demand-notes";
6import { useFolders } from "@/db/queries";
7import { cn } from "@/lib/utils";
8
9/**
10 * Pick a folder, and a live query over `onDemandNotesCollection` asks for its
11 * notes. Below, the requests that collection has made to the fake API.
12 */
13export function OnDemandDemo() {
14 const folders = useFolders();
15 const [folderId, setFolderId] = useState("work");
16 const { data: notes, isLoading } = useLiveQuery({ query: folderNotesQuery(folderId) });
17 const requests = useRequestLog().filter((entry) => entry.url.startsWith("/api/notes?"));
18
19 return (
20 <div className="my-5 overflow-hidden rounded-xl border text-sm leading-normal">
21 <div className="flex flex-wrap gap-1.5 border-b bg-muted/40 p-3 dark:bg-white/[0.03]">
22 {folders.map((folder) => (
23 <button
24 key={folder.id}
25 type="button"
26 aria-pressed={folder.id === folderId}
27 onClick={() => setFolderId(folder.id)}
28 className={cn(
29 "h-8 rounded-full border px-3 font-medium transition-colors",
30 folder.id === folderId
31 ? "border-guide-accent/50 bg-guide-accent-soft text-foreground"
32 : "bg-background text-muted-foreground hover:text-foreground",
33 )}
34 >
35 {folder.name}
36 </button>
37 ))}
38 </div>
39 <div className="grid gap-0 sm:grid-cols-2 sm:divide-x">
40 <section className="p-3" aria-live="polite">
41 <h4 className="mb-2 text-xs font-semibold tracking-wide text-muted-foreground uppercase">
42 Live query result
43 </h4>
44 {isLoading ? (
45 <p className="text-muted-foreground">Loading…</p>
46 ) : notes.length === 0 ? (
47 <p className="text-muted-foreground">No notes in this folder.</p>
48 ) : (
49 <ul className="my-0 list-none space-y-1 pl-0">
50 {notes.map((note) => (
51 <li key={note.id} className="truncate">
52 {note.title || "New Note"}
53 </li>
54 ))}
55 </ul>
56 )}
57 </section>
58 <section className="border-t p-3 sm:border-t-0">
59 <h4 className="mb-2 text-xs font-semibold tracking-wide text-muted-foreground uppercase">
60 Requests from this collection
61 </h4>
62 {requests.length === 0 ? (
63 <p className="text-muted-foreground">None yet.</p>
64 ) : (
65 <ol className="my-0 list-none space-y-1 pl-0 font-mono text-xs">
66 {requests
67 .slice(-8)
68 .reverse()
69 .map((entry) => (
70 <li key={entry.id} className="flex gap-2">
71 <span className="text-muted-foreground">{entry.method}</span>
72 <span className="min-w-0 flex-1 break-all">{entry.url}</span>
73 <span className="text-muted-foreground tabular-nums">
74 {entry.state === "pending" ? "…" : entry.status}
75 </span>
76 </li>
77 ))}
78 </ol>
79 )}
80 </section>
81 </div>
82 </div>
83 );
84}