|
| 1 | +import { |
| 2 | + useEffect, |
| 3 | + useState, |
| 4 | + useSyncExternalStore, |
| 5 | + type ReactNode, |
| 6 | +} from "react"; |
| 7 | +import type { Credential } from "../../bundled/builderlab/oauth/browser"; |
| 8 | +import { firstRunCredential } from "../../bundled/builderlab/oauth/first-run"; |
| 9 | +import { createOAuthSession } from "../../bundled/builderlab/oauth/session"; |
| 10 | +import { Button } from "../../shared/design-system/ui/Button"; |
| 11 | +import { Field } from "../../shared/design-system/ui/Field"; |
| 12 | +import { Input } from "../../shared/design-system/ui/Input"; |
| 13 | +import type { Identity } from "./service"; |
| 14 | +import { SetupWindowHeader } from "./SetupWindowHeader"; |
| 15 | + |
| 16 | +/** First-launch gate: renders children once a human key exists on this device. */ |
| 17 | +export function Welcome({ |
| 18 | + identity, |
| 19 | + children, |
| 20 | + connect = firstRunCredential, |
| 21 | +}: { |
| 22 | + identity: Identity; |
| 23 | + children: ReactNode; |
| 24 | + connect?: (signal: AbortSignal) => Promise<Credential>; |
| 25 | +}) { |
| 26 | + const state = useSyncExternalStore(identity.subscribe, identity.snapshot); |
| 27 | + const [login] = useState(() => createOAuthSession(connect)); |
| 28 | + const auth = useSyncExternalStore(login.subscribe, login.snapshot); |
| 29 | + useEffect( |
| 30 | + () => () => { |
| 31 | + if (login.snapshot().status === "pending") login.cancel(); |
| 32 | + }, |
| 33 | + [login], |
| 34 | + ); |
| 35 | + if (state.status === "ready") return children; |
| 36 | + return ( |
| 37 | + <main className="p-8" aria-labelledby="welcome-title"> |
| 38 | + <SetupWindowHeader /> |
| 39 | + <div className="mx-auto flex max-w-sm flex-col gap-6"> |
| 40 | + {state.status === "loading" ? ( |
| 41 | + <> |
| 42 | + <h1 id="welcome-title" className="text-title text-primary"> |
| 43 | + Buzz |
| 44 | + </h1> |
| 45 | + <p role="status">Opening your saved identity…</p> |
| 46 | + </> |
| 47 | + ) : state.status === "error" ? ( |
| 48 | + <> |
| 49 | + <h1 id="welcome-title" className="text-title text-primary"> |
| 50 | + Buzz |
| 51 | + </h1> |
| 52 | + <p role="alert">{state.error}</p> |
| 53 | + <Button onClick={() => void identity.retry()}> |
| 54 | + Retry secure storage access |
| 55 | + </Button> |
| 56 | + </> |
| 57 | + ) : auth.status === "signed-in" ? ( |
| 58 | + <> |
| 59 | + <h1 id="welcome-title" className="text-title text-primary"> |
| 60 | + Connected to Builderlab |
| 61 | + </h1> |
| 62 | + <p role="status" className="text-body text-secondary"> |
| 63 | + {auth.account?.email |
| 64 | + ? `Signed in as ${auth.account.email}.` |
| 65 | + : "Your Builderlab account is connected."} |
| 66 | + </p> |
| 67 | + </> |
| 68 | + ) : auth.error ? ( |
| 69 | + <> |
| 70 | + <h1 id="welcome-title" className="text-title text-primary"> |
| 71 | + Couldn’t connect to Builderlab |
| 72 | + </h1> |
| 73 | + <p role="alert" className="text-body text-danger"> |
| 74 | + {auth.error} |
| 75 | + </p> |
| 76 | + <div> |
| 77 | + <Button onClick={login.cancel}>Back</Button> |
| 78 | + </div> |
| 79 | + </> |
| 80 | + ) : ( |
| 81 | + <> |
| 82 | + <div> |
| 83 | + <h1 id="welcome-title" className="text-title text-primary"> |
| 84 | + Buzz |
| 85 | + </h1> |
| 86 | + <p className="mt-2 text-body-lg text-secondary"> |
| 87 | + Your people, your agents, your projects — all in one place. |
| 88 | + </p> |
| 89 | + </div> |
| 90 | + <div className="flex flex-col gap-3"> |
| 91 | + <Button |
| 92 | + variant={auth.status === "pending" ? "outline" : "primary"} |
| 93 | + size="lg" |
| 94 | + onClick={() => { |
| 95 | + if (auth.status === "pending") login.cancel(); |
| 96 | + else void login.signIn(); |
| 97 | + }} |
| 98 | + > |
| 99 | + {auth.status === "pending" |
| 100 | + ? "Cancel sign-in" |
| 101 | + : "Connect with Builderlab"} |
| 102 | + </Button> |
| 103 | + {auth.status === "pending" && ( |
| 104 | + <p role="status" className="text-body-sm text-secondary"> |
| 105 | + Finish sign-in in your browser, then return to Buzz. |
| 106 | + </p> |
| 107 | + )} |
| 108 | + <Field label="Community URL" disabled> |
| 109 | + <Input type="url" placeholder="Coming soon" disabled /> |
| 110 | + </Field> |
| 111 | + <Button size="lg" disabled> |
| 112 | + Scan a QR code — Coming soon |
| 113 | + </Button> |
| 114 | + </div> |
| 115 | + </> |
| 116 | + )} |
| 117 | + </div> |
| 118 | + </main> |
| 119 | + ); |
| 120 | +} |
0 commit comments