import type { ReactNode, Ref } from 'react' import { useImperativeHandle } from 'react' import { Button } from '@/client/components/ui/button' import { Spinner } from '@/client/components/ui/spinner' import { DrawingLayer } from '@/client/features/drawings/drawing-layer' import { DrawingToolbar } from '@/client/features/drawings/drawing-toolbar' import { type DrawingControls, useDrawingHistoryState } from '@/client/features/drawings/use-drawing-layer' import { type ViewSketchController, useViewSketch } from '@/client/features/drawings/use-view-sketch' import { cn } from '@/client/lib/cn' import type { PendingView } from '@/lib/types' import { draftSessionId } from '@/lib/session-drafts' import { viewTabId } from '@/lib/workspace-tabs' import { IconScribble } from '@tabler/icons-react' export type PendingViewHandle = { prepareSketchForSend: () => Promise resetSketch: () => Promise } type PendingViewScreenProps = { ref?: Ref active: boolean pendingView: PendingView running: boolean sketchSessionId?: string chatDocked: boolean chatOpen?: boolean workspaceId: string onEditingStart: () => void onContinueInChat: () => void onOpenChat: () => void onDiscard: () => void } export function PendingViewScreen({ ref, active, pendingView, running, sketchSessionId, chatDocked, chatOpen = false, workspaceId, onEditingStart, onContinueInChat, onOpenChat, onDiscard }: PendingViewScreenProps) { const displayStatus = pendingView.status === 'submitted' ? (running ? 'building' : 'waiting') : pendingView.status const sketch = useViewSketch({ active: active && pendingView.status === 'draft', viewId: pendingView.id, sessionId: sketchSessionId ?? pendingView.buildSessionId ?? draftSessionId(viewTabId(pendingView.id)), source: viewTabId(pendingView.id), workspaceId, onEditingStart, onContinueInChat }) useImperativeHandle( ref, () => ({ prepareSketchForSend: sketch.prepareForSend, resetSketch: sketch.resetDocument }), [sketch.prepareForSend, sketch.resetDocument] ) return ( ) } function PendingViewScreenStarting() { return (

Starting view chat…

) } type PendingViewScreenFailedProps = { chatOpen?: boolean error?: string onOpenChat: () => void onDiscard: () => void } function PendingViewScreenFailed({ error, chatOpen, onOpenChat, onDiscard }: PendingViewScreenFailedProps) { return (

{error}

) } type PendingViewScreenSketchSurfaceProps = { active: boolean chatDocked: boolean chatOpen?: boolean error?: string sketch: ViewSketchController displayStatus: 'draft' | 'starting' | 'building' | 'waiting' | 'failed' onDiscard: () => void onOpenChat: () => void } function PendingViewScreenSketchSurface({ active, chatDocked, chatOpen = false, error, sketch, displayStatus, onDiscard, onOpenChat }: PendingViewScreenSketchSurfaceProps) { const draft = displayStatus === 'draft' return (
{draft && ( void sketch.continueInChat()} /> )}
{displayStatus === 'building' && ( )} {displayStatus === 'starting' && } {displayStatus === 'waiting' && ( )} {displayStatus === 'failed' && ( )}
) } type PendingViewScreenSketchBackgroundProps = { chatDocked: boolean sketch: ViewSketchController draft: boolean } function PendingViewScreenSketchBackground({ chatDocked, sketch, draft }: PendingViewScreenSketchBackgroundProps) { const { controls, targetRef } = sketch const { hasStrokes } = useDrawingHistoryState(controls) return (
{draft && !controls.active && !hasStrokes && (
Sketch how the view should look
)}
) } type PendingViewScreenWaitingStateProps = { chatOpen?: boolean error?: string onDiscard: () => void onOpenChat: () => void } function PendingViewScreenWaitingState({ error, chatOpen, onDiscard, onOpenChat }: PendingViewScreenWaitingStateProps) { return (

The view needs your attention

{error ?? 'Use the chat to work through the issue and continue'}

) } type PendingViewScreenBuildingStateProps = { controls: DrawingControls } function PendingViewScreenBuildingState({ controls }: PendingViewScreenBuildingStateProps) { const { hasStrokes } = useDrawingHistoryState(controls) return ( {!hasStrokes && } ) } type PendingViewScreenStatusLayoutProps = { children: ReactNode } function PendingViewScreenStatusLayout({ children }: PendingViewScreenStatusLayoutProps) { return (
{children}
) }