Files
buzz/desktop/src/features/onboarding/ui/MachineOnboardingFlow.tsx
Taylor Ho 38edd28336 feat(desktop): explain system keychain protection during onboarding
- Track whether the active identity is stored in the system keyring, local fallback file, environment, or ephemeral memory.
- Expose storage metadata through the Tauri identity response and shared TypeScript identity model.
- Add a responsive first information card beside the two backup actions without revealing boot-time identity creation.
- Show keychain password education only when secure storage succeeded and accurate neutral copy for fallback states.
- Extend identity persistence tests and split storage types into focused modules to preserve file-size limits.

Co-authored-by: Taylor Ho <taylorkmho@gmail.com>
Signed-off-by: Taylor Ho <taylorkmho@gmail.com>
2026-07-29 23:52:27 -07:00

365 lines
14 KiB
TypeScript

import * as React from "react";
import type { QueryClient } from "@tanstack/react-query";
import { ArrowUp } from "lucide-react";
import {
getIdentity,
importIdentity,
persistCurrentIdentity,
} from "@/shared/api/tauriIdentity";
import type { IdentityStorage } from "@/shared/api/types";
import { Button } from "@/shared/ui/button";
import { StartupWindowDragRegion } from "@/shared/ui/StartupWindowDragRegion";
import { BackupStep } from "./BackupStep";
import { DefaultConfigStep } from "./DefaultConfigStep";
import { DownloadKeyStep } from "./DownloadKeyStep";
import {
backupSessionToPasswordEntry,
useEncryptedBackupSession,
} from "./EncryptedBackupCreator";
import { IdentityKeyHelpDialog } from "./IdentityKeyHelpDialog";
import { LandingBees } from "./LandingBees";
import { NostrKeyImportForm } from "./NostrKeyImportForm";
import {
ONBOARDING_LANDING_CTA_CLASS,
OnboardingChrome,
} from "./OnboardingChrome";
import { OnboardingFooterProvider } from "./OnboardingFooter";
import { OnboardingSlideTransition } from "./OnboardingSlideTransition";
import { SetupStep } from "./SetupStep";
export type MachineOnboardingPage =
| "identity"
| "key-import"
| "backup"
| "setup"
| "config";
type BackupSubview = "created" | "options" | "password";
/** A pending navigation the parent should execute after RouterProvider mounts. */
export type PostOnboardingNavigation = {
to: string;
search?: Record<string, string>;
};
export function MachineOnboardingFlow({
complete,
continueWithIdentity,
identityLost,
initialPage,
queryClient,
navigateAfterComplete,
}: {
complete: (pubkey?: string) => void;
continueWithIdentity: (pubkey: string) => void;
identityLost: boolean;
initialPage?: MachineOnboardingPage;
queryClient: QueryClient;
/**
* Called when the user finishes onboarding and requests navigation to a
* specific route (e.g. Settings → Agents). The parent owns the RouterProvider,
* so navigation must be deferred to it — calling router.navigate() here races
* with RouterProvider mounting.
*/
navigateAfterComplete?: (nav: PostOnboardingNavigation) => void;
}) {
const [page, setPage] = React.useState<MachineOnboardingPage>(
identityLost ? "key-import" : (initialPage ?? "identity"),
);
const [error, setError] = React.useState<string | null>(null);
const [isPending, setIsPending] = React.useState(false);
const [identityWasImported, setIdentityWasImported] = React.useState(false);
const [selectedPubkey, setSelectedPubkey] = React.useState<string | null>(
null,
);
const [identityStorage, setIdentityStorage] = React.useState<
IdentityStorage | undefined
>();
const [readyRuntimeIds, setReadyRuntimeIds] = React.useState<string[]>([]);
const [backupSubview, setBackupSubview] =
React.useState<BackupSubview>("created");
const [backupDirection, setBackupDirection] = React.useState<
"forward" | "backward"
>("forward");
const [returningFromSecurity, setReturningFromSecurity] =
React.useState(false);
// Owned here so switching between the yellow onboarding view and the dark
// security subview keeps the created backup, password, and test progress.
const backupSession = useEncryptedBackupSession();
const isSecuritySubview = page === "backup" && backupSubview !== "created";
const handleReadyRuntimeIdsChange = React.useCallback(
(runtimeIds: readonly string[]) => {
setReadyRuntimeIds(Array.from(new Set(runtimeIds)));
},
[],
);
const loadFreshIdentity = React.useCallback(async () => {
setIsPending(true);
setError(null);
try {
const identity = await getIdentity();
queryClient.setQueryData(["identity"], identity);
setSelectedPubkey(identity.pubkey);
setIdentityStorage(identity.storage);
setBackupDirection("forward");
setReturningFromSecurity(false);
setBackupSubview("created");
setPage("backup");
} catch (cause) {
setError(
cause instanceof Error ? cause.message : "Failed to load identity",
);
} finally {
setIsPending(false);
}
}, [queryClient]);
const replaceLostIdentity = React.useCallback(async () => {
const confirmed = window.confirm(
"This will create a new identity and abandon your previous key. This cannot be undone. Continue?",
);
if (!confirmed) return;
setIsPending(true);
setError(null);
try {
const identity = await persistCurrentIdentity();
queryClient.setQueryData(["identity"], identity);
setSelectedPubkey(identity.pubkey);
setIdentityStorage(identity.storage);
setBackupDirection("forward");
setReturningFromSecurity(false);
setBackupSubview("created");
setPage("backup");
} catch (cause) {
setError(
cause instanceof Error ? cause.message : "Failed to save identity",
);
} finally {
setIsPending(false);
}
}, [queryClient]);
const importExistingIdentity = React.useCallback(
async (nsec: string, password?: string) => {
const identity = await importIdentity(nsec, password);
continueWithIdentity(identity.pubkey);
queryClient.setQueryData(["identity"], identity);
setIdentityWasImported(true);
setSelectedPubkey(identity.pubkey);
setPage("setup");
},
[continueWithIdentity, queryClient],
);
return (
<div
className={`buzz-onboarding-neutral-theme buzz-startup-shell flex max-h-dvh items-start justify-center overflow-x-hidden overflow-y-auto px-4 text-foreground ${
isSecuritySubview ? "buzz-onboarding-security-theme" : ""
} ${
page === "identity"
? "buzz-onboarding-welcome py-8"
: "pb-28 pt-[106px]"
}`}
data-testid="machine-onboarding-gate"
>
<StartupWindowDragRegion />
{page === "identity" ? <LandingBees /> : null}
{isSecuritySubview ? (
<div className="fixed inset-x-0 top-8 z-20 flex justify-center px-6">
<Button
className="h-9 gap-2 rounded-full bg-foreground/10 px-5 text-foreground hover:bg-foreground/15 hover:text-foreground"
data-testid="backup-return-to-onboarding"
onClick={() => {
setBackupDirection("backward");
setReturningFromSecurity(true);
setBackupSubview("created");
}}
type="button"
variant="ghost"
>
<ArrowUp className="h-4 w-4" aria-hidden="true" />
Return to onboarding
</Button>
</div>
) : page !== "identity" ? (
<OnboardingChrome
current={page === "config" ? 4 : page === "setup" ? 3 : 2}
/>
) : null}
<OnboardingFooterProvider>
<div
className={`relative flex w-full max-w-[1040px] flex-col items-center text-center ${
page === "identity" ? "my-auto" : "buzz-onboarding-step-frame"
}`}
>
{page === "identity" ? (
<OnboardingSlideTransition
className="flex w-full max-w-[720px] flex-col items-center text-center"
direction="forward"
effect="mask-reveal-up"
transitionKey="machine-identity"
>
<img
alt="Buzz"
className="w-full max-w-[600px]"
src="/landing/buzz-wordmark.png"
/>
<p className="mt-2 max-w-[560px] text-center text-2xl font-normal leading-none text-foreground">
Your people, your agents, your projects —<br />
all in one place.
</p>
{error ? (
<p className="mt-4 text-sm text-destructive">{error}</p>
) : null}
<div className="mt-10 flex flex-col items-center gap-3">
<Button
className={ONBOARDING_LANDING_CTA_CLASS}
disabled={isPending}
onClick={() => void loadFreshIdentity()}
type="button"
>
{isPending
? "Loading identity…"
: selectedPubkey
? "Continue setup"
: "Create a new identity key"}
</Button>
<Button
className="h-9 rounded-full bg-foreground/10 px-5 hover:bg-foreground/15"
disabled={isPending}
onClick={() => setPage("key-import")}
type="button"
variant="ghost"
>
{selectedPubkey
? "Use a different key instead"
: "Use an existing key"}
</Button>
</div>
<IdentityKeyHelpDialog />
</OnboardingSlideTransition>
) : page === "key-import" ? (
<OnboardingSlideTransition
className="flex min-h-[calc(100dvh-13.25rem)] w-full max-w-[837px] flex-col items-center text-center"
direction="forward"
effect="fade"
transitionKey="machine-key-import"
>
<div className="shrink-0">
<h1 className="text-title font-normal text-foreground">
{identityLost
? "Re-import your key"
: "Enter your private key"}
</h1>
<p className="mt-5 max-w-[440px] text-sm leading-6 text-foreground/80">
{identityLost
? "Your identity is no longer in the system keyring. Re-import your nsec to restore it."
: "If you already have a Buzz account, enter your private key below to get started."}
</p>
</div>
<div className="buzz-onboarding-key-import-position w-full">
<NostrKeyImportForm
backLabel={identityLost ? "Start new identity" : "Back"}
onBack={
identityLost
? () => void replaceLostIdentity()
: () => setPage("identity")
}
onImport={importExistingIdentity}
variant="spotlight"
/>
</div>
</OnboardingSlideTransition>
) : page === "backup" ? (
backupSubview === "password" ? (
<DownloadKeyStep
direction={backupDirection}
onBack={() => {
setBackupDirection("backward");
setReturningFromSecurity(false);
setBackupSubview("options");
}}
session={backupSession}
/>
) : (
<BackupStep
direction={backupDirection}
identityStorage={identityStorage}
onBack={() => setPage("identity")}
onNext={() => setPage("setup")}
onOpenPasswordBackup={() => {
setBackupDirection("forward");
setReturningFromSecurity(false);
setBackupSubview("password");
}}
onShowOptions={() => {
setBackupDirection("forward");
setReturningFromSecurity(false);
setBackupSubview("options");
}}
optionsExpanded={backupSubview === "options"}
returningFromSecurity={returningFromSecurity}
/>
)
) : page === "setup" ? (
<SetupStep
actions={{
// Fresh-key users return to whichever identity backup subview
// they used to reach setup; imported keys skip backup entirely.
back: () => {
if (identityWasImported) {
setPage("key-import");
return;
}
if (backupSubview === "password") {
backupSessionToPasswordEntry(backupSession);
}
setBackupDirection("backward");
setReturningFromSecurity(false);
setPage("backup");
},
next: (runtimeIds) => {
const ids = Array.from(runtimeIds);
setReadyRuntimeIds(ids);
// Harness install can fail (Windows/PATH/network). Don't soft-lock
// onboarding — users can finish setup later in Settings → Agents.
if (ids.length === 0) {
complete(selectedPubkey ?? undefined);
return;
}
setPage("config");
},
navigateToAgentSettings: () => {
// Complete onboarding first, then delegate the Settings → Agents
// navigation to the parent. The parent owns RouterProvider, so
// navigation from within the onboarding flow races with the
// router mounting — calling router.navigate() here is unsafe.
complete(selectedPubkey ?? undefined);
navigateAfterComplete?.({
to: "/settings",
search: { section: "agents" },
});
},
}}
direction="forward"
onReadyRuntimeIdsChange={handleReadyRuntimeIdsChange}
/>
) : (
<DefaultConfigStep
actions={{
back: () => setPage("setup"),
complete: () => complete(selectedPubkey ?? undefined),
}}
direction="forward"
readyRuntimeIds={readyRuntimeIds}
/>
)}
</div>
</OnboardingFooterProvider>
</div>
);
}