| 1 | /** |
| 2 | * The top bar's Ask: the lavender pixel 1 beside its words. The panel it |
| 3 | * opens (components/ask-composer.tsx: the direct message with @g1t, its |
| 4 | * answers drawn with Markdown, code and tool steps) is loaded the first |
| 5 | * time it is wanted, starting when the pointer comes near, so no page |
| 6 | * carries it until then. Kept mounted once loaded, so the conversation |
| 7 | * stays while the panel is closed. |
| 8 | */ |
| 9 | import { Suspense, lazy, useEffect, useState } from "react"; |
| 10 | import { useLocation } from "react-router"; |
| 11 | |
| 12 | import { ASK_EVENT } from "../lib/ask"; |
| 13 | import { MarkGlyph } from "./logo"; |
| 14 | import { Button } from "./ui/button"; |
| 15 | import { Hint } from "./ui/hint"; |
| 16 | import { Sheet, SheetContent, SheetTitle, SheetTrigger } from "./ui/sheet"; |
| 17 | import { Skeleton } from "./ui/skeleton"; |
| 18 | |
| 19 | const load = () => import("./ask-composer"); |
| 20 | const AskSheet = lazy(() => load().then((module) => ({ default: module.AskSheet }))); |
| 21 | |
| 22 | export function AskButton({ slug }: { slug: string }) { |
| 23 | const [open, setOpen] = useState(false); |
| 24 | const [wanted, setWanted] = useState(false); |
| 25 | // A question a page asked (lib/ask.ts askG1t), until the panel sends it. |
| 26 | const [queued, setQueued] = useState<{ text: string; at: number } | null>(null); |
| 27 | const location = useLocation(); |
| 28 | // Any completed navigation, including answer citations and the current DM, |
| 29 | // reveals its destination. Revalidation and links opened in another tab do not. |
| 30 | useEffect(() => setOpen(false), [location.key]); |
| 31 | useEffect(() => { |
| 32 | const onAsk = (event: Event) => { |
| 33 | const text = (event as CustomEvent<{ text?: string }>).detail?.text?.trim(); |
| 34 | if (text) setQueued({ text, at: Date.now() }); |
| 35 | setWanted(true); |
| 36 | setOpen(true); |
| 37 | }; |
| 38 | window.addEventListener(ASK_EVENT, onAsk); |
| 39 | return () => window.removeEventListener(ASK_EVENT, onAsk); |
| 40 | }, []); |
| 41 | const change = (next: boolean) => { |
| 42 | if (next) setWanted(true); |
| 43 | setOpen(next); |
| 44 | }; |
| 45 | return ( |
| 46 | <Sheet open={open} onOpenChange={change}> |
| 47 | <Hint label="Ask g1t about your work"> |
| 48 | <SheetTrigger asChild> |
| 49 | <Button variant="soft" size="bar" aria-label="Ask g1t" className="gap-2 max-md:ring-0 lg:px-3" onPointerEnter={() => void load()} onFocus={() => void load()}> |
| 50 | <MarkGlyph className="h-3.5" /> |
| 51 | <span className="max-lg:hidden">Ask</span> |
| 52 | </Button> |
| 53 | </SheetTrigger> |
| 54 | </Hint> |
| 55 | {wanted && ( |
| 56 | <Suspense fallback={<Loading />}> |
| 57 | <AskSheet slug={slug} open={open} queued={queued} onQueuedTaken={() => setQueued(null)} /> |
| 58 | </Suspense> |
| 59 | )} |
| 60 | </Sheet> |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | /** The panel's frame while it loads. */ |
| 65 | function Loading() { |
| 66 | return ( |
| 67 | <SheetContent side="right" className="sm:max-w-xl"> |
| 68 | <SheetTitle className="sr-only">Ask g1t</SheetTitle> |
| 69 | <div className="flex flex-col gap-3 p-5" aria-busy="true"> |
| 70 | <Skeleton className="h-5 w-32" /> |
| 71 | <Skeleton className="h-4 w-56" /> |
| 72 | </div> |
| 73 | </SheetContent> |
| 74 | ); |
| 75 | } |