'use client'; import { useState, useCallback } from 'react'; import { useCanvas } from '@/store/canvas'; import { useHistory } from '@/store/history'; import { useArtboards, patchArtboard } from '@/hooks/useArtboards'; import { useDiffs } from '@/hooks/useDiffs'; import { useQueryClient } from '@tanstack/react-query'; import type { PropChange } from '@originmain/diff-engine'; import type { Artboard, IntentDiff } from '@originmain/origin-graph'; const TYPE_COLORS: Record = { s: '#7DD3A8', n: '#7EB8FF', b: '#FFBA7B', }; type TabId = 'props' | 'diff' | 'graph'; // Dark panel tokens const T = { bg: '#111115', border: 'rgba(255,255,255,0.055)', sep: 'rgba(255,255,255,0.04)', label: 'rgba(255,255,255,0.22)', key: 'rgba(255,255,255,0.32)', dim: 'rgba(255,255,255,0.18)', accent: '#3385FF', tabFg: 'rgba(255,255,255,0.28)', tabOn: 'rgba(255,255,255,0.88)', }; export function Inspector() { const { selectedArtboardId, liveArtboardIds, workspaceId, projectId } = useCanvas(); const [tab, setTab] = useState('props'); const { rawArtboards } = useArtboards(workspaceId ?? undefined, projectId ?? undefined); const selectedArtboard = rawArtboards.find((ab) => ab.id === selectedArtboardId) ?? null; const isLive = selectedArtboardId ? liveArtboardIds.has(selectedArtboardId) : false; return (
{/* Tab bar */}
{(['props', 'diff', 'graph'] as TabId[]).map((t) => ( ))}
{/* Content */}
{!selectedArtboardId ? (
Select an artboard
) : tab === 'props' ? ( ) : tab === 'diff' ? ( ) : (
)}
{/* Status bar */}
{isLive ? 'Live render connected' : selectedArtboardId ? 'No render — set URL in Props' : 'No artboard selected'} {selectedArtboard ? `${selectedArtboard.metadata_jsonb['width'] ?? '?'} × ${selectedArtboard.metadata_jsonb['height'] ?? '?'}` : '—'}
); } /* ── Props tab ────────────────────────────────────────────── */ function PropsTab({ artboard, workspaceId, projectId, }: { artboard: Artboard | null; workspaceId: string | null; projectId: string | null; }) { const queryClient = useQueryClient(); const [editingUrl, setEditingUrl] = useState(false); const [urlDraft, setUrlDraft] = useState(''); const saveRenderUrl = useCallback(async () => { if (!artboard) return; const { renderUrl: _removed, ...rest } = artboard.metadata_jsonb; const meta: Record = urlDraft.trim() ? { ...rest, renderUrl: urlDraft.trim() } : { ...rest }; try { await patchArtboard(artboard.id, { metadata_jsonb: meta }); queryClient.invalidateQueries({ queryKey: ['artboards', workspaceId, projectId ?? undefined] }); } catch (e) { console.error('[Inspector] patch renderUrl failed', e); } setEditingUrl(false); }, [artboard, urlDraft, workspaceId, projectId, queryClient]); if (!artboard) return null; const meta = artboard.metadata_jsonb; const N = TYPE_COLORS['n']!; const B = TYPE_COLORS['b']!; const S = TYPE_COLORS['s']!; const canvasProps: Array<{ key: string; val: string; color: string }> = [ { key: 'x', val: String(meta['x'] ?? 0), color: N }, { key: 'y', val: String(meta['y'] ?? 0), color: N }, { key: 'width', val: String(meta['width'] ?? 0), color: N }, { key: 'height', val: String(meta['height'] ?? 0), color: N }, ]; const reservedKeys = new Set(['x', 'y', 'width', 'height', 'renderUrl']); const extraProps = Object.entries(meta) .filter(([k]) => !reservedKeys.has(k)) .map(([k, v]) => { const t = typeof v; const color = t === 'number' ? N : t === 'boolean' ? B : S; const val = t === 'string' ? `"${v}"` : String(v); return { key: k, val, color }; }); const renderUrl = typeof meta['renderUrl'] === 'string' ? meta['renderUrl'] as string : ''; return ( <> {extraProps.length > 0 && ( <>
{extraProps.map(({ key, val, color }) => ( ))}
)}
{canvasProps.map(({ key, val, color }) => ( ))}
{/* renderUrl — inline editable */}
url
{editingUrl ? (
setUrlDraft(e.target.value)} onKeyDown={e => { if (e.key === 'Enter') void saveRenderUrl(); if (e.key === 'Escape') setEditingUrl(false); }} placeholder="http://localhost:3000" style={{ flex: 1, fontSize: '0.5875rem', fontFamily: "'JetBrains Mono', monospace", background: 'rgba(255,255,255,0.05)', border: '1px solid rgba(255,255,255,0.12)', borderRadius: 5, padding: '4px 8px', color: 'rgba(255,255,255,0.85)', outline: 'none', }} />
) : renderUrl ? ( {renderUrl} ) : ( not connected )}
); } function Section({ label, children }: { label: string; children: React.ReactNode }) { return (
{label}
{children}
); } function PropRow({ label, value, color }: { label: string; value: string; color: string }) { return (
{label} {value}
); } function GraphNode({ label, depth, isRoot }: { label: string; depth: number; isRoot?: boolean }) { return (
{label}
); } function HSep() { return
; } /* ── Diff tab ─────────────────────────────────────────────── */ function DiffTab({ artboardId }: { artboardId: string | null }) { const { stacks } = useHistory(); const { diffs, createDiff, isLoading } = useDiffs(artboardId); const artboardHistory = artboardId ? (stacks[artboardId] ?? { past: [], future: [] }) : { past: [], future: [] }; const pendingChanges: PropChange[] = artboardHistory.past.flatMap(e => e.changes); const hasChanges = pendingChanges.length > 0; const exportDiff = useCallback(() => { if (!artboardId || !hasChanges) return; createDiff.mutate({ artboard_id: artboardId, changes_jsonb: { propChanges: pendingChanges, styleChanges: [] }, summary: '', status: 'DRAFT', }); }, [artboardId, pendingChanges, hasChanges, createDiff]); if (!artboardId) { return (
No artboard selected
); } return ( <> {/* Pending local changes */}
c.changeType !== 'unchanged').length : 0} changes`}> {hasChanges ? ( <> {pendingChanges .filter(c => c.changeType !== 'unchanged') .map((change, i) => ( ))} {createDiff.isError && ( Export failed — try again )} ) : ( No pending changes )}
{/* Saved diffs from DB */}
{isLoading ? ( Loading… ) : diffs.length === 0 ? ( No exported diffs yet ) : ( diffs.map(d => ) )}
); } const STATUS_COLOR: Record = { DRAFT: '#FFBA7B', REVIEWED: '#7EB8FF', APPLIED: '#7DD3A8', REJECTED: '#FF8080', }; function SavedDiffRow({ diff }: { diff: IntentDiff }) { const changes = diff.changes_jsonb as { propChanges?: PropChange[]; styleChanges?: PropChange[] } | null; const count = (changes?.propChanges?.length ?? 0) + (changes?.styleChanges?.length ?? 0); const color = STATUS_COLOR[diff.status] ?? T.dim; return (
{diff.status} {count} change{count !== 1 ? 's' : ''}
{diff.summary && ( {diff.summary} )}
); } function DiffChangeRow({ change }: { change: PropChange }) { const isRemoved = change.changeType === 'removed'; const isAdded = change.changeType === 'added'; const isModified = change.changeType === 'modified'; const rows: Array<{ op: 'del' | 'add'; text: string }> = []; if (isModified) { rows.push({ op: 'del', text: `− ${change.key}: ${change.before}` }); rows.push({ op: 'add', text: `+ ${change.key}: ${change.after}` }); } else if (isRemoved) { rows.push({ op: 'del', text: `− ${change.key}: ${change.before}` }); } else if (isAdded) { rows.push({ op: 'add', text: `+ ${change.key}: ${change.after}` }); } return ( <> {rows.map((r, i) => (
{r.text}
))} ); }