import { useEffect, useRef, useState, type ReactNode } from 'react' import { useMutation } from '@tanstack/react-query' import { IconSearch } from '@tabler/icons-react' import { Button } from '@/client/components/ui/button' import { Spinner } from '@/client/components/ui/spinner' import { requestJson } from '@/client/api/http' import type { WorkspaceEntry } from '@/lib/types' import type { IdeaDiscovery, OnboardingHarness, OnboardingIdea } from '@/lib/onboarding' import { useCreateWorkspace } from '@/client/features/workspace/add/api' import { usePreloadWorkspaceFonts } from '@/client/runtime/workspace-theme' import { OnboardingScreen } from './onboarding-screen' import { OnboardingIdeaDisk } from './onboarding-idea-disk' import { cancelDiscovery, useOnboardingJob } from './api' type WorkspaceSetupStepProps = { harness: OnboardingHarness onFinish: (workspaceId?: string) => void finishing: boolean finishError?: string } export function WorkspaceSetupStep({ harness, onFinish, finishing, finishError }: WorkspaceSetupStepProps) { const [discovery, setDiscovery] = useState() const [selection, setSelection] = useState<{ ideaId: string; operationId: string }>() const [retry, setRetry] = useState(false) const ideas = useOnboardingJob('discover', discovery) const build = useOnboardingJob( 'create', discovery && selection ? { ...discovery, ...selection } : undefined, retry ) useEffect(() => { if (!discovery) return return () => { void cancelDiscovery(discovery.runId).catch(() => {}) } }, [discovery]) const openedWorkspace = useRef(undefined) const workspaceId = build.data?.status === 'done' ? build.data.workspaceId : undefined useEffect(() => { if (!workspaceId || openedWorkspace.current === workspaceId) return openedWorkspace.current = workspaceId onFinish(workspaceId) }, [workspaceId, onFinish]) const createWorkspace = useCreateWorkspace() const emptyWorkspaceId = createWorkspace.data?.id const startFromScratch = useMutation({ mutationFn: async () => { if (emptyWorkspaceId) return emptyWorkspaceId if (discovery) { await cancelDiscovery(discovery.runId) setDiscovery(undefined) } const workspaces = await requestJson('/api/workspaces') if (workspaces.length > 0) return const entry = await createWorkspace.mutateAsync({ type: harness }) return entry.id }, onSuccess: id => onFinish(id) }) const discoveryError = ideas.error?.message ?? ideas.data?.error const error = startFromScratch.error?.message ?? build.error?.message ?? build.data?.error ?? (discoveryError ? 'We couldn’t find ideas this time. Try again, or start on your own.' : undefined) function findIdeas() { startFromScratch.reset() setDiscovery({ harness, runId: crypto.randomUUID() }) } function select(ideaId: string) { setSelection(current => current ?? { ideaId, operationId: crypto.randomUUID() }) } const busy = finishing || startFromScratch.isPending const feedback = ( <> {(error || finishError) && (

{error || finishError}

)} {selection && (error || finishError) && ( )} ) const footer = !selection && ( ) if (!selection && (!discovery || error || busy)) return ( ) if (ideas.data?.status === 'done') return ( ) return ( ) } type DiscoveryIntroProps = { loading?: boolean progressMessage?: string disabled: boolean failed: boolean onFindIdeas: () => void feedback: ReactNode footer: ReactNode } function DiscoveryIntro({ loading = false, progressMessage, disabled, failed, onFindIdeas, feedback, footer }: DiscoveryIntroProps) { return (

Choose a starting point

Moi can look through your recent conversations, emails, and connected apps to suggest ideas for you to build

{loading ? ( ) : ( )} {feedback}
) } type DiscoveryProgressProps = { message?: string } function DiscoveryProgress({ message }: DiscoveryProgressProps) { return (
{message ?? 'Taking a first look…'}
) } type IdeaOptionsProps = { ideas: OnboardingIdea[] selectedIdeaId?: string onSelect: (id: string) => void feedback: ReactNode footer: ReactNode } export function IdeaOptions({ ideas, selectedIdeaId, onSelect, feedback, footer }: IdeaOptionsProps) { usePreloadWorkspaceFonts() return (

Choose a starting point

Pick an idea to build, you can change and add things as you go

{ideas.map(idea => ( onSelect(idea.id)} /> ))}
{feedback}
) }