| 1 | /** |
| 2 | * Home's way into Ask (components/ask-composer): the box at the top of the |
| 3 | * page, What changed (g1t's plain-English summary of the span, streamed |
| 4 | * onto the page by components/home-summary), and Explain on each change |
| 5 | * that landed (components/home.tsx). The box and Explain hand their |
| 6 | * question to the top bar's Ask panel (lib/ask.ts askG1t). Every question |
| 7 | * goes to the viewer's direct message with @g1t, so the answers stay in |
| 8 | * Chat and Ask carries on from them. |
| 9 | */ |
| 10 | import { ArrowUp, MessageSquare, RotateCcw, Sparkles } from "lucide-react"; |
| 11 | import { type FormEvent, type KeyboardEvent, Suspense, lazy, useCallback, useEffect, useRef, useState } from "react"; |
| 12 | import { Link } from "react-router"; |
| 13 | |
| 14 | import { askG1t, openAsk } from "../lib/ask"; |
| 15 | import { HOME_STARTERS, type Span, summarizeQuestion } from "../lib/home"; |
| 16 | import { Suggestion, Suggestions } from "./ai-elements/suggestion"; |
| 17 | import { Card } from "./home"; |
| 18 | import { MarkGlyph } from "./logo"; |
| 19 | import { Button } from "./ui/button"; |
| 20 | import { Hint } from "./ui/hint"; |
| 21 | import { Skeleton } from "./ui/skeleton"; |
| 22 | import { Textarea } from "./ui/textarea"; |
| 23 | |
| 24 | const loadSummary = () => import("./home-summary"); |
| 25 | const HomeSummary = lazy(loadSummary); |
| 26 | |
| 27 | type AskSpan = Pick<Span, "key" | "note" | "words" | "from" | "now">; |
| 28 | |
| 29 | /** The window event Home's box sends to have What changed summarize the span. */ |
| 30 | const SUMMARIZE_EVENT = "g1t:home-summarize"; |
| 31 | |
| 32 | /** What the span is called on a button: "since I was last here", "in the last 24 hours". */ |
| 33 | function spanLabel(span: AskSpan): string { |
| 34 | return span.key === "last" && span.note == null ? "since I was last here" : `${span.words.charAt(0).toLowerCase()}${span.words.slice(1)}`; |
| 35 | } |
| 36 | |
| 37 | /** The top of Home: what are we building today, a box that asks g1t, and a few things to ask first. */ |
| 38 | export function HomeAsk({ span }: { span: AskSpan }) { |
| 39 | const [draft, setDraft] = useState(""); |
| 40 | const send = () => { |
| 41 | if (!draft.trim()) return; |
| 42 | askG1t(draft); |
| 43 | setDraft(""); |
| 44 | }; |
| 45 | const submit = (event: FormEvent) => { |
| 46 | event.preventDefault(); |
| 47 | send(); |
| 48 | }; |
| 49 | const onKeyDown = (event: KeyboardEvent<HTMLTextAreaElement>) => { |
| 50 | if (event.key === "Enter" && !event.shiftKey && !event.nativeEvent.isComposing) { |
| 51 | event.preventDefault(); |
| 52 | send(); |
| 53 | } |
| 54 | }; |
| 55 | return ( |
| 56 | <section aria-labelledby="home-ask" className="mx-auto flex w-full max-w-3xl flex-col items-center gap-5 pt-4 pb-2 md:pt-8"> |
| 57 | <h1 id="home-ask" className="text-center text-[1.75rem] leading-tight font-semibold tracking-tight md:text-[2rem]"> |
| 58 | What are we building today? |
| 59 | </h1> |
| 60 | <form onSubmit={submit} className="w-full"> |
| 61 | <div className="flex items-end gap-2 rounded-2xl border border-line bg-surface p-2 pl-4 shadow-sm transition-shadow focus-within:border-accent-dim focus-within:ring-2 focus-within:ring-accent/25"> |
| 62 | <MarkGlyph className="mb-2.5 h-4 shrink-0" /> |
| 63 | <label htmlFor="home-ask-box" className="sr-only"> |
| 64 | Ask g1t |
| 65 | </label> |
| 66 | <Textarea |
| 67 | id="home-ask-box" |
| 68 | value={draft} |
| 69 | onChange={(event) => setDraft(event.target.value)} |
| 70 | onKeyDown={onKeyDown} |
| 71 | rows={1} |
| 72 | maxLength={16_000} |
| 73 | placeholder="Ask g1t what changed, what's blocked, or what to do next" |
| 74 | className="field-sizing-content max-h-40 min-h-10 resize-none border-0 bg-transparent px-1 py-2 text-[0.9375rem] shadow-none focus-visible:ring-0" |
| 75 | /> |
| 76 | <Hint label="Ask (Enter)"> |
| 77 | <Button type="submit" variant="accent" size="icon-sm" disabled={!draft.trim()} aria-label="Ask" className="mb-1"> |
| 78 | <ArrowUp size={15} /> |
| 79 | </Button> |
| 80 | </Hint> |
| 81 | </div> |
| 82 | </form> |
| 83 | <Suggestions className="justify-center"> |
| 84 | <Suggestion |
| 85 | suggestion={`Summarize what changed ${spanLabel(span)}`} |
| 86 | onPick={() => window.dispatchEvent(new CustomEvent(SUMMARIZE_EVENT))} |
| 87 | onPointerEnter={() => void loadSummary()} |
| 88 | className="border-accent-dim text-fg" |
| 89 | /> |
| 90 | {HOME_STARTERS.map((starter) => ( |
| 91 | <Suggestion key={starter.label} suggestion={starter.label} onPick={() => askG1t(starter.question)} /> |
| 92 | ))} |
| 93 | </Suggestions> |
| 94 | </section> |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * What changed: g1t's summary of the span in plain English, on the page. |
| 100 | * Asked only when the viewer presses Summarize (each answer is a call to |
| 101 | * the workspace's own AI provider); asked again for another span. |
| 102 | */ |
| 103 | export function WhatChangedCard({ slug, span, tz }: { slug: string; span: AskSpan; tz: string | null }) { |
| 104 | // Each ask is its own: Summarize again asks anew. |
| 105 | const [asked, setAsked] = useState<{ question: string; at: number } | null>(null); |
| 106 | const [done, setDone] = useState<{ channelId: string | null } | null>(null); |
| 107 | const card = useRef<HTMLDivElement>(null); |
| 108 | const label = spanLabel(span); |
| 109 | |
| 110 | const summarize = useCallback(() => { |
| 111 | setDone(null); |
| 112 | setAsked({ question: summarizeQuestion(span, tz), at: Date.now() }); |
| 113 | }, [span, tz]); |
| 114 | |
| 115 | // Home's box asks for it: bring the card into view and start. |
| 116 | useEffect(() => { |
| 117 | const onAsk = () => { |
| 118 | card.current?.scrollIntoView({ behavior: "smooth", block: "start" }); |
| 119 | summarize(); |
| 120 | }; |
| 121 | window.addEventListener(SUMMARIZE_EVENT, onAsk); |
| 122 | return () => window.removeEventListener(SUMMARIZE_EVENT, onAsk); |
| 123 | }, [summarize]); |
| 124 | |
| 125 | const onDone = useCallback((channelId: string | null) => setDone({ channelId }), []); |
| 126 | |
| 127 | return ( |
| 128 | <div ref={card} className="scroll-mt-4"> |
| 129 | <Card title="What changed" hint={`In plain English, ${label}`}> |
| 130 | <div className="px-5 pb-5"> |
| 131 | {!asked ? ( |
| 132 | <div className="flex flex-col gap-3 sm:flex-row sm:items-center"> |
| 133 | <p className="grow text-sm text-muted">g1t reads the commits, pull requests, deploys and conversations {label} and tells you what changed, why, and what is live.</p> |
| 134 | <Button type="button" variant="accent" size="sm" onClick={summarize} onPointerEnter={() => void loadSummary()} className="shrink-0 self-start sm:self-auto"> |
| 135 | <Sparkles size={14} /> |
| 136 | Summarize |
| 137 | </Button> |
| 138 | </div> |
| 139 | ) : ( |
| 140 | <> |
| 141 | <Suspense |
| 142 | fallback={ |
| 143 | <div className="space-y-2.5"> |
| 144 | <Skeleton className="h-3.5 w-11/12" /> |
| 145 | <Skeleton className="h-3.5 w-3/4" /> |
| 146 | </div> |
| 147 | } |
| 148 | > |
| 149 | <HomeSummary key={asked.at} slug={slug} question={asked.question} onDone={onDone} /> |
| 150 | </Suspense> |
| 151 | {done && ( |
| 152 | <div className="mt-4 flex flex-wrap items-center gap-2 border-t border-line pt-3"> |
| 153 | <Button type="button" variant="soft" size="sm" onClick={openAsk}> |
| 154 | <MarkGlyph className="h-3" /> |
| 155 | Ask a follow-up |
| 156 | </Button> |
| 157 | <Button type="button" variant="ghost" size="sm" onClick={summarize}> |
| 158 | <RotateCcw size={13} /> |
| 159 | Summarize again |
| 160 | </Button> |
| 161 | {done.channelId && ( |
| 162 | <Button asChild variant="ghost" size="sm" className="sm:ml-auto"> |
| 163 | <Link to={`/${slug}/-/chat/dm/${done.channelId}`}> |
| 164 | <MessageSquare size={13} /> |
| 165 | In your direct message with g1t |
| 166 | </Link> |
| 167 | </Button> |
| 168 | )} |
| 169 | </div> |
| 170 | )} |
| 171 | </> |
| 172 | )} |
| 173 | </div> |
| 174 | </Card> |
| 175 | </div> |
| 176 | ); |
| 177 | } |