import { useState } from 'react' import { createRoot } from 'react-dom/client' import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import { Router, useLocation } from 'wouter' import { memoryLocation } from 'wouter/memory-location' import { loadAppConfig } from '@/client/api/app-config' import { Button } from '@/client/components/ui/button' import { Input } from '@/client/components/ui/input' import { Dialog, DialogContent, DialogDescription, DialogTitle } from '@/client/components/ui/dialog' import { TooltipProvider } from '@/client/components/ui/tooltip' import { Toaster } from '@/client/components/ui/toast' import { WorkspacePendingScreen } from '@/client/features/workspace/workspace-pending-screen' import { WorkspaceLayoutContext } from '@/client/features/workspace/workspace-layout-context' import { createDefaultWorkspaceLayout } from '@/lib/workspace-layout' import { useWorkspaceSetup } from '@/client/features/workspace/setup/workspace-setup' import { WorkspaceWelcomeState } from '@/client/features/workspace/setup/workspace-welcome-state' import { WorkspaceLoadingState } from '@/client/features/workspace/setup/workspace-recovery-states' import type { AppEnvironment } from '@/lib/types' import { workspaceTypeLabel } from '@/client/features/workspace/workspace-presentation' import { useHarnessStatus, type LoginHarness } from '@/client/features/harness/api' import { createSetupMocks, MOCK_SIGN_IN_URL, MOCK_CODE_SIGN_IN_URL, PREVIEW_WORKSPACE_ID, SETUP_SCENARIOS } from './workspace-setup-mocks' import type { SetupScenario } from './workspace-setup-mocks' const scenarioLabels: Record = { welcome: 'Welcome', login: 'Log in', install: 'Install', 'missing-folder': 'Folder missing', loading: 'Loading' } type SetupPreviewProps = { mocks: ReturnType client: QueryClient environment: AppEnvironment harness: LoginHarness scenario: SetupScenario } function SetupPreview({ mocks, client, environment, harness, scenario }: SetupPreviewProps) { const [provider, setProvider] = useState<'browser' | 'code' | 'closed'>() const [deviceCode, setDeviceCode] = useState('') const [layout, setLayout] = useState(createDefaultWorkspaceLayout) const auth = useHarnessStatus(harness) const pendingId = auth.data?.login?.state === 'pending' ? auth.data.login.id : undefined // Both local harnesses open the browser automatically. const providerPage = provider ?? (environment === 'local' && pendingId ? 'browser' : 'closed') function restart(next: Record = {}) { const params = new URLSearchParams({ environment, harness, scenario, ...next }) window.location.assign(`?${params.toString()}`) } function update(action: () => void) { action() void client.invalidateQueries() } return (
{ const link = (event.target as Element).closest('a') if (link?.href === MOCK_SIGN_IN_URL || link?.href === MOCK_CODE_SIGN_IN_URL) { event.preventDefault() setProvider(link.href === MOCK_CODE_SIGN_IN_URL ? 'code' : 'browser') } }} >
{(['local', 'cloud'] as const).map(value => ( ))}
{(['claude-code', 'codex'] as const).map(value => ( ))}
{SETUP_SCENARIOS.map(value => ( ))}
setLayout(current => ({ ...current, ...update })), name: 'My workspace', cwd: null, provider: harness, isLoading: false }} >
{scenario === 'welcome' ? ( ) : scenario === 'loading' ? ( ) : ( )}
!open && setProvider('closed')} > Mock sign-in page This simulates the agent’s browser sign-in. No credentials are needed. {providerPage === 'code' ? ( <>

Copy this authorization code, close this dialog, and submit it in the login screen:

MOCK-AUTH-CODE ) : ( <> {environment === 'cloud' && ( setDeviceCode(event.target.value)} /> )} )}
) } function SetupScreen() { const [path] = useLocation() const { content, ready, setupRequired } = useWorkspaceSetup(PREVIEW_WORKSPACE_ID) if (ready) return null return path === '/' ? (
Workspace removed
) : ( {content} ) } export async function mountSetupPreview(element: HTMLElement) { const params = new URLSearchParams(location.search) const environment = params.get('environment') === 'cloud' ? 'cloud' : 'local' const harness = params.get('harness') === 'codex' ? 'codex' : 'claude-code' const scenario = SETUP_SCENARIOS.find(value => value === params.get('scenario')) ?? 'welcome' const mocks = createSetupMocks(environment, harness, scenario) // Scoped to this separate document. No fallback to the real fetch or socket. window.fetch = Object.assign(mocks.request, { preconnect: fetch.preconnect }) Object.defineProperty(window, 'WebSocket', { value: class extends EventTarget { static OPEN = 1 readyState = 0 send() {} close() {} } }) await loadAppConfig() const client = new QueryClient({ defaultOptions: { queries: { retry: false } } }) const locationHook = memoryLocation({ path: `/workspace/${PREVIEW_WORKSPACE_ID}` }) createRoot(element).render( ) }