@decido/shell 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +13 -0
- package/package.json +65 -0
- package/src/AgentPlayer.tsx +105 -0
- package/src/DecidoPlayer.tsx +117 -0
- package/src/bridge/BridgeAgent.ts +443 -0
- package/src/components/DecidoIcon.tsx +56 -0
- package/src/components/JsonTreeEditor.tsx +117 -0
- package/src/components/PanelSplitter.tsx +71 -0
- package/src/components/PluginErrorBoundary.tsx +69 -0
- package/src/components/SafeLiquidUI.tsx +114 -0
- package/src/components/TransientLayer.tsx +92 -0
- package/src/components/agent/AgentChat.tsx +134 -0
- package/src/components/chat-extensions/IntentCatalogPanel.tsx +81 -0
- package/src/components/chat-extensions/chatSlashCommands.ts +101 -0
- package/src/components/controls/CreatorInputBar.tsx +144 -0
- package/src/components/controls/OSToolbar.tsx +90 -0
- package/src/components/controls/TimelineTape.tsx +43 -0
- package/src/components/debug/ActionTimelineTab.tsx +111 -0
- package/src/components/debug/CSSInspectorTab.tsx +436 -0
- package/src/components/debug/ExportTab.tsx +192 -0
- package/src/components/debug/FlowHealthTab.tsx +86 -0
- package/src/components/debug/LogsTab.tsx +110 -0
- package/src/components/debug/MorphStackTab.tsx +241 -0
- package/src/components/debug/NetworkTab.tsx +173 -0
- package/src/components/debug/PerformanceTab.tsx +171 -0
- package/src/components/debug/ProfilesTab.tsx +238 -0
- package/src/components/debug/ReplayTab.tsx +70 -0
- package/src/components/debug/StoresTab.tsx +255 -0
- package/src/components/debug/TopologyTab.tsx +59 -0
- package/src/components/debug/debugConfig.tsx +66 -0
- package/src/components/playground/DebugPanel.tsx +112 -0
- package/src/components/playground/HeaderCenterControls.tsx +92 -0
- package/src/components/playground/KeyframeListItem.tsx +70 -0
- package/src/components/playground/PlaygroundAppSidebar.tsx +171 -0
- package/src/components/playground/PlaygroundBottomControls.tsx +132 -0
- package/src/components/playground/PlaygroundCanvas.tsx +87 -0
- package/src/components/playground/PlaygroundChat.tsx +236 -0
- package/src/components/playground/PlaygroundErrorBoundary.tsx +63 -0
- package/src/components/playground/PlaygroundFloatingInput.tsx +352 -0
- package/src/components/playground/PlaygroundHeader.tsx +222 -0
- package/src/components/playground/PlaygroundSidebar.tsx +136 -0
- package/src/components/playground/PlaygroundTerminal.tsx +44 -0
- package/src/components/playground/SuggestionCards.tsx +29 -0
- package/src/components/playground/demos/ClinicaAINode.tsx +221 -0
- package/src/components/playground/demos/FinanceAINode.tsx +226 -0
- package/src/components/playground/demos/KiaAcademyNode.tsx +250 -0
- package/src/components/playground/demos/KiaBotNode.tsx +207 -0
- package/src/components/playground/demos/KiaCampaignNode.tsx +191 -0
- package/src/components/playground/demos/KiaComplianceNode.tsx +140 -0
- package/src/components/playground/demos/KiaCustomerJourneyNode.tsx +220 -0
- package/src/components/playground/demos/KiaCyberNode.tsx +203 -0
- package/src/components/playground/demos/KiaDashboardNode.tsx +399 -0
- package/src/components/playground/demos/KiaEmbudoOverviewNode.tsx +168 -0
- package/src/components/playground/demos/KiaExecutiveNode.tsx +169 -0
- package/src/components/playground/demos/KiaGamificationNode.tsx +229 -0
- package/src/components/playground/demos/KiaIntelligenceHubNode.tsx +165 -0
- package/src/components/playground/demos/KiaInventoryNode.tsx +183 -0
- package/src/components/playground/demos/KiaLeadScoringNode.tsx +226 -0
- package/src/components/playground/demos/KiaLiveSimulationNode.tsx +177 -0
- package/src/components/playground/demos/KiaMultiDealerNode.tsx +223 -0
- package/src/components/playground/demos/KiaNPSVoiceNode.tsx +214 -0
- package/src/components/playground/demos/KiaOmnichannelNode.tsx +162 -0
- package/src/components/playground/demos/KiaPBIBudgetNode.tsx +152 -0
- package/src/components/playground/demos/KiaPBIConversionNode.tsx +206 -0
- package/src/components/playground/demos/KiaPBIFunnelNode.tsx +184 -0
- package/src/components/playground/demos/KiaPBIOwnershipNode.tsx +113 -0
- package/src/components/playground/demos/KiaPBIPartnerNode.tsx +143 -0
- package/src/components/playground/demos/KiaPBIPreciosNode.tsx +120 -0
- package/src/components/playground/demos/KiaPBIRuntNode.tsx +205 -0
- package/src/components/playground/demos/KiaPartnerScoreNode.tsx +206 -0
- package/src/components/playground/demos/KiaPredictiveNode.tsx +226 -0
- package/src/components/playground/demos/KiaShowroomNode.tsx +194 -0
- package/src/components/playground/demos/KiaStoreNode.tsx +215 -0
- package/src/components/playground/demos/KiaSustainabilityNode.tsx +173 -0
- package/src/components/playground/demos/KiaUsedVehiclesNode.tsx +163 -0
- package/src/components/playground/demos/KiaWorkshopNode.tsx +221 -0
- package/src/components/playground/demos/SmartCityNode.tsx +205 -0
- package/src/components/playground/demos/kia_campaign_manifest.json +112 -0
- package/src/components/playground/input-parts/AIModelSelector.tsx +156 -0
- package/src/components/playground/input-parts/InputActions.tsx +80 -0
- package/src/components/playground/input-parts/InputToolbar.tsx +245 -0
- package/src/components/playground/input-parts/ResourceLibraryPanel.tsx +287 -0
- package/src/components/playground/sidebarDsdIO.ts +82 -0
- package/src/components/settings/SettingsPanel.tsx +267 -0
- package/src/components/shell/AppHeader.tsx +9 -0
- package/src/components/shell/AppShell.tsx +139 -0
- package/src/components/shell/ArtifactBar.tsx +97 -0
- package/src/components/shell/BootScreen.tsx +19 -0
- package/src/components/shell/CenterComposite.tsx +87 -0
- package/src/components/shell/CodeEditorPanel.tsx +88 -0
- package/src/components/shell/GlobalOverlays.tsx +228 -0
- package/src/components/shell/LayoutConfigurator.tsx +209 -0
- package/src/components/shell/LayoutGrid.tsx +178 -0
- package/src/components/shell/MorphShell.tsx +368 -0
- package/src/components/shell/PluginViewer.tsx +147 -0
- package/src/components/shell/ShellNexusPreview.tsx +458 -0
- package/src/components/shell/SlotRenderer.tsx +115 -0
- package/src/components/shell/TabBar.tsx +94 -0
- package/src/components/shell/TemplateLibrary.tsx +195 -0
- package/src/components/shell/layoutConstants.ts +35 -0
- package/src/components/shell/morphStageMeta.ts +15 -0
- package/src/components/shell/shells/BuiltInShells.tsx +443 -0
- package/src/components/shell/shells/DatawayChatShell.tsx +42 -0
- package/src/components/shell/shells/TokenPreview.tsx +339 -0
- package/src/components/shell/shells/bootShells.ts +31 -0
- package/src/components/shells/CreatorShell.tsx +37 -0
- package/src/components/shells/DecidoShell.tsx +447 -0
- package/src/components/shells/ExperimentalChatShell.tsx +245 -0
- package/src/components/shells/UserCanvas.tsx +44 -0
- package/src/components/studio/BlueprintManagerPanel.tsx +137 -0
- package/src/components/studio/DependencyTreePanel.tsx +192 -0
- package/src/components/studio/NodePalette.tsx +92 -0
- package/src/components/studio/NodePropertiesPanel.tsx +81 -0
- package/src/components/studio/ReactFlowEditor.tsx +242 -0
- package/src/components/studio/TimelineEditor.tsx +122 -0
- package/src/components/studio/TimelineKeyframeCard.tsx +99 -0
- package/src/components/studio/VariablePanel.tsx +181 -0
- package/src/components/studio/blueprint/BlueprintCard.tsx +82 -0
- package/src/components/studio/editor/CanvasContextMenu.tsx +107 -0
- package/src/components/studio/editor/EditorToolbar.tsx +80 -0
- package/src/components/studio/editor/StageContentRenderer.tsx +134 -0
- package/src/components/studio/editor/TrackPropertyEditors.tsx +133 -0
- package/src/components/studio/editor/TreeNodeItem.tsx +91 -0
- package/src/components/studio/editor/edgeStyles.ts +43 -0
- package/src/components/studio/editor/editorKeyHandler.ts +95 -0
- package/src/components/studio/editor/nodeTypeRegistry.ts +137 -0
- package/src/components/studio/editor/paletteCatalog.tsx +84 -0
- package/src/components/studio/nodes/shell/InteractionNodes.tsx +82 -0
- package/src/components/studio/nodes/shell/LayoutControlNodes.tsx +69 -0
- package/src/components/studio/nodes/shell/RegisterActionNode.tsx +20 -0
- package/src/components/studio/nodes/shell/RegisterButtonNode.tsx +22 -0
- package/src/components/studio/nodes/shell/RegisterPanelNode.tsx +19 -0
- package/src/components/studio/nodes/shell/RegisterSidebarNode.tsx +19 -0
- package/src/components/studio/nodes/shell/RegisterStatusBarNode.tsx +22 -0
- package/src/components/studio/nodes/shell/RegisterTabNode.tsx +21 -0
- package/src/components/studio/nodes/shell/RegisterTopBarNode.tsx +22 -0
- package/src/components/studio/nodes/shell/ShellConfigNode.tsx +51 -0
- package/src/components/studio/nodes/shell/ShellNodeBase.tsx +100 -0
- package/src/components/studio/nodes/shell/ThemeNodes.tsx +51 -0
- package/src/components/studio/nodes/shell/index.ts +12 -0
- package/src/components/widgets/BroadcastWidget.tsx +93 -0
- package/src/components/widgets/MarketplaceWidget.tsx +298 -0
- package/src/components/widgets/McpToolsWidget.tsx +231 -0
- package/src/components/widgets/OpsDashboard.tsx +59 -0
- package/src/components/widgets/QuickActionsWidget.tsx +60 -0
- package/src/components/widgets/UsageWidget.tsx +112 -0
- package/src/components/widgets/WidgetRenderer.tsx +892 -0
- package/src/components/widgets/WidgetSlotPanel.tsx +213 -0
- package/src/config/IconRegistry.ts +126 -0
- package/src/contexts/NetworkProvider.tsx +162 -0
- package/src/core/AIDirector.ts +71 -0
- package/src/core/EventBus.ts +37 -0
- package/src/core/PluginContext.tsx +141 -0
- package/src/hooks/listeners/useUIStateListener.ts +59 -0
- package/src/hooks/listeners/useWhatsAppListener.ts +110 -0
- package/src/hooks/morphBridge.ts +82 -0
- package/src/hooks/useAIModelSelector.ts +144 -0
- package/src/hooks/useAgentStream.ts +220 -0
- package/src/hooks/useAutoUpdater.ts +89 -0
- package/src/hooks/useBootSequence.ts +20 -0
- package/src/hooks/useExportDSD.ts +53 -0
- package/src/hooks/useFullscreen.ts +35 -0
- package/src/hooks/useGeminiStream.ts +282 -0
- package/src/hooks/useIntentLens.ts +224 -0
- package/src/hooks/useKeyboardShortcuts.ts +69 -0
- package/src/hooks/useLoggerBridge.ts +32 -0
- package/src/hooks/useMcpClient.ts +112 -0
- package/src/hooks/useNexusaiDeploy.ts +118 -0
- package/src/hooks/usePlaybackEngine.ts +21 -0
- package/src/hooks/usePlaygroundCommander.ts +475 -0
- package/src/hooks/usePluginEngine.ts +165 -0
- package/src/hooks/useScreenRecorder.ts +73 -0
- package/src/hooks/useShellKeyboard.ts +40 -0
- package/src/hooks/useShellShortcuts.ts +118 -0
- package/src/hooks/useSoundEffects.ts +35 -0
- package/src/hooks/useStudioConfig.ts +72 -0
- package/src/hooks/useSystemBoot.ts +84 -0
- package/src/hooks/useSystemTelemetry.ts +62 -0
- package/src/index.ts +97 -0
- package/src/lib/debugLogger.ts +80 -0
- package/src/lib/networkInterceptor.ts +100 -0
- package/src/mocks/decido.tsx +41 -0
- package/src/plugins/pluginAPI.ts +190 -0
- package/src/store/McpStore.ts +69 -0
- package/src/store/UpdaterStore.ts +60 -0
- package/src/store/engine.ts +392 -0
- package/src/store/index.ts +4 -0
- package/src/store/layoutPresets.ts +66 -0
- package/src/store/playgroundTypes.ts +98 -0
- package/src/store/useActionTimelineStore.ts +48 -0
- package/src/store/useDebugPanelStore.ts +98 -0
- package/src/store/useDebugProfileStore.ts +130 -0
- package/src/store/useLayoutStore.ts +205 -0
- package/src/store/useMorphInstanceStore.ts +289 -0
- package/src/store/useMorphologyStore.ts +103 -0
- package/src/store/usePlaygroundStore.ts +236 -0
- package/src/store/useShellRegistry.ts +123 -0
- package/src/store/useSuggestionsStore.ts +57 -0
- package/src/store/useThemeStore.ts +399 -0
- package/src/store/useUIComponentStore.ts +179 -0
- package/src/types/DecidoStoryDefinition.ts +43 -0
- package/src/utils/ai/ai-architect.ts +92 -0
- package/src/utils/ai/ai-code.ts +187 -0
- package/src/utils/ai/ai-core.ts +50 -0
- package/src/utils/ai/ai-media.ts +292 -0
- package/src/utils/layoutGraph.ts +67 -0
- package/tsconfig.json +17 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,892 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ExternalLink, RefreshCw, X, AlertTriangle, Wifi, WifiOff } from 'lucide-react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* WidgetRenderer v2 — Evolved rendering engine for plugin widgets.
|
|
6
|
+
*
|
|
7
|
+
* Option A (SDUI): ServerDrivenNode schema → native React elements
|
|
8
|
+
* - Data binding via `dataSource` → resolves against a reactive data store
|
|
9
|
+
* - Conditional rendering via `conditional`
|
|
10
|
+
* - Rich component library: ProgressBar, StatusBadge, Sparkline, MetricCard
|
|
11
|
+
* - onAction dispatch to shell
|
|
12
|
+
*
|
|
13
|
+
* Option B (iframe): External SPA in sandboxed iframe
|
|
14
|
+
* - postMessage bridge for typed host↔plugin communication
|
|
15
|
+
* - Auto-detect plugin availability
|
|
16
|
+
* - Error boundary isolation
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
20
|
+
// Types
|
|
21
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
22
|
+
|
|
23
|
+
export interface ServerDrivenNode {
|
|
24
|
+
type: string;
|
|
25
|
+
props?: Record<string, any>;
|
|
26
|
+
children?: (ServerDrivenNode | string)[];
|
|
27
|
+
onAction?: { command: string; payload?: Record<string, any> };
|
|
28
|
+
dataSource?: string;
|
|
29
|
+
conditional?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* WidgetDef — local widget contract used by the Studio renderer.
|
|
34
|
+
*
|
|
35
|
+
* Aligns with `WidgetDefinition` from `@decido/sdk/widget`.
|
|
36
|
+
* Once all consumers migrate, this can be replaced with the SDK type directly.
|
|
37
|
+
*/
|
|
38
|
+
export interface WidgetDef {
|
|
39
|
+
id: string;
|
|
40
|
+
pluginId: string;
|
|
41
|
+
title: string;
|
|
42
|
+
icon?: string;
|
|
43
|
+
slot: string;
|
|
44
|
+
schema?: ServerDrivenNode;
|
|
45
|
+
url?: string;
|
|
46
|
+
visible?: boolean;
|
|
47
|
+
order?: number;
|
|
48
|
+
/** Required permissions to view this widget (from SDK WidgetDefinition) */
|
|
49
|
+
permissions?: string[];
|
|
50
|
+
/** Width for overlays/sidebars */
|
|
51
|
+
width?: number | string;
|
|
52
|
+
/** Height for panels/overlays */
|
|
53
|
+
height?: number | string;
|
|
54
|
+
/** URL to a JS script for headless worker-sandbox execution (no UI) */
|
|
55
|
+
scriptUrl?: string;
|
|
56
|
+
/** React Component reference for naturally compiled widgets */
|
|
57
|
+
component?: any;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
61
|
+
// Data Binding Store (Option A evolution)
|
|
62
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
63
|
+
|
|
64
|
+
type DataListener = () => void;
|
|
65
|
+
|
|
66
|
+
class WidgetDataStore {
|
|
67
|
+
private data: Record<string, any> = {};
|
|
68
|
+
private listeners = new Set<DataListener>();
|
|
69
|
+
|
|
70
|
+
/** Set data and notify subscribers */
|
|
71
|
+
set(path: string, value: any) {
|
|
72
|
+
this.data[path] = value;
|
|
73
|
+
this.listeners.forEach(fn => fn());
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Resolve a dotted path: 'sentinel.metrics.uptime' → value */
|
|
77
|
+
resolve(path: string): any {
|
|
78
|
+
const parts = path.split('.');
|
|
79
|
+
let current: any = this.data;
|
|
80
|
+
for (const part of parts) {
|
|
81
|
+
if (current == null) return undefined;
|
|
82
|
+
current = current[part];
|
|
83
|
+
}
|
|
84
|
+
return current;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Bulk set */
|
|
88
|
+
merge(obj: Record<string, any>) {
|
|
89
|
+
Object.assign(this.data, obj);
|
|
90
|
+
this.listeners.forEach(fn => fn());
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
subscribe(fn: DataListener): () => void {
|
|
94
|
+
this.listeners.add(fn);
|
|
95
|
+
return () => this.listeners.delete(fn);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export const widgetDataStore = new WidgetDataStore();
|
|
100
|
+
|
|
101
|
+
// Seed with demo data for Option A
|
|
102
|
+
widgetDataStore.merge({
|
|
103
|
+
sentinel: {
|
|
104
|
+
metrics: { eventsPerMin: 1247, avgLatency: 23, uptime: 99.7, errorCount: 2 },
|
|
105
|
+
events: [
|
|
106
|
+
{ time: '10:23:01', type: 'pageload', path: '/dashboard', duration: 142 },
|
|
107
|
+
{ time: '10:22:58', type: 'api', path: '/api/nodes', duration: 89 },
|
|
108
|
+
{ time: '10:22:45', type: 'click', path: 'sidebar', duration: 3 },
|
|
109
|
+
{ time: '10:22:30', type: 'session', path: 'user:admin', duration: 0 },
|
|
110
|
+
],
|
|
111
|
+
errors: [
|
|
112
|
+
{ message: 'TypeError: Cannot read properties of undefined', source: 'IssueList.tsx:11:39', ago: '3 min', severity: 'error' },
|
|
113
|
+
{ message: 'Warning: defaultProps deprecated', source: 'recharts.js:28626', ago: '15 min', severity: 'warning' },
|
|
114
|
+
],
|
|
115
|
+
sessions: { today: 3, recording: true },
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
120
|
+
// postMessage Bridge (Option B evolution)
|
|
121
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
122
|
+
|
|
123
|
+
interface BridgeMessage {
|
|
124
|
+
type: 'DECIDO_HOST_EVENT' | 'DECIDO_PLUGIN_REQUEST' | 'DECIDO_PLUGIN_RESPONSE';
|
|
125
|
+
topic?: string;
|
|
126
|
+
action?: string;
|
|
127
|
+
payload?: any;
|
|
128
|
+
requestId?: string;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
class PluginBridge {
|
|
132
|
+
private iframe: HTMLIFrameElement | null = null;
|
|
133
|
+
private handlers = new Map<string, (payload: any) => any>();
|
|
134
|
+
private themeObserver: MutationObserver | null = null;
|
|
135
|
+
|
|
136
|
+
attach(iframe: HTMLIFrameElement) {
|
|
137
|
+
this.iframe = iframe;
|
|
138
|
+
window.addEventListener('message', this.onMessage);
|
|
139
|
+
|
|
140
|
+
// Auto-push theme changes to iframe
|
|
141
|
+
this.themeObserver = new MutationObserver(() => {
|
|
142
|
+
const isDark = document.documentElement.classList.contains('dark');
|
|
143
|
+
this.emit('theme:changed', isDark ? 'dark' : 'light');
|
|
144
|
+
});
|
|
145
|
+
this.themeObserver.observe(document.documentElement, {
|
|
146
|
+
attributes: true, attributeFilter: ['class'],
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
detach() {
|
|
151
|
+
window.removeEventListener('message', this.onMessage);
|
|
152
|
+
this.themeObserver?.disconnect();
|
|
153
|
+
this.themeObserver = null;
|
|
154
|
+
this.iframe = null;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Register a handler for plugin requests */
|
|
158
|
+
onRequest(action: string, handler: (payload: any) => any) {
|
|
159
|
+
this.handlers.set(action, handler);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Send an event into the iframe */
|
|
163
|
+
emit(topic: string, payload: any) {
|
|
164
|
+
this.iframe?.contentWindow?.postMessage({
|
|
165
|
+
type: 'DECIDO_HOST_EVENT',
|
|
166
|
+
topic,
|
|
167
|
+
payload,
|
|
168
|
+
} as BridgeMessage, '*');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
private onMessage = (event: MessageEvent<BridgeMessage>) => {
|
|
172
|
+
if (!this.iframe || event.source !== this.iframe.contentWindow) return;
|
|
173
|
+
const msg = event.data;
|
|
174
|
+
if (msg?.type === 'DECIDO_PLUGIN_REQUEST' && msg.action) {
|
|
175
|
+
const handler = this.handlers.get(msg.action);
|
|
176
|
+
if (handler) {
|
|
177
|
+
try {
|
|
178
|
+
const result = handler(msg.payload);
|
|
179
|
+
// Handle async handlers
|
|
180
|
+
if (result instanceof Promise) {
|
|
181
|
+
result.then(r => this.respond(msg.requestId, r))
|
|
182
|
+
.catch(e => this.respond(msg.requestId, { error: String(e) }));
|
|
183
|
+
} else {
|
|
184
|
+
this.respond(msg.requestId, result);
|
|
185
|
+
}
|
|
186
|
+
} catch (e) {
|
|
187
|
+
console.error('[Bridge] Handler error:', e);
|
|
188
|
+
this.respond(msg.requestId, { error: String(e) });
|
|
189
|
+
}
|
|
190
|
+
} else {
|
|
191
|
+
console.warn(`[Bridge] No handler for action: ${msg.action}`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
private respond(requestId: string | undefined, payload: any) {
|
|
197
|
+
if (!requestId) return;
|
|
198
|
+
this.iframe?.contentWindow?.postMessage({
|
|
199
|
+
type: 'DECIDO_PLUGIN_RESPONSE',
|
|
200
|
+
requestId,
|
|
201
|
+
payload,
|
|
202
|
+
} as BridgeMessage, '*');
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
207
|
+
// Error Boundary
|
|
208
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
209
|
+
|
|
210
|
+
interface ErrorBoundaryState { hasError: boolean; error?: Error }
|
|
211
|
+
|
|
212
|
+
class WidgetErrorBoundary extends React.Component<
|
|
213
|
+
{ children: React.ReactNode; widgetId: string; onReset?: () => void },
|
|
214
|
+
ErrorBoundaryState
|
|
215
|
+
> {
|
|
216
|
+
state: ErrorBoundaryState = { hasError: false };
|
|
217
|
+
|
|
218
|
+
static getDerivedStateFromError(error: Error) {
|
|
219
|
+
return { hasError: true, error };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
componentDidCatch(error: Error, info: React.ErrorInfo) {
|
|
223
|
+
console.error(`[Widget:${this.props.widgetId}] Crash:`, error, info);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
render() {
|
|
227
|
+
if (this.state.hasError) {
|
|
228
|
+
return (
|
|
229
|
+
<div className="w-full h-full flex flex-col items-center justify-center bg-surface-primary gap-3 p-6">
|
|
230
|
+
<div className="w-14 h-14 rounded-2xl bg-red-500/10 border border-red-500/20 flex items-center justify-center">
|
|
231
|
+
<AlertTriangle className="w-7 h-7 text-red-400" />
|
|
232
|
+
</div>
|
|
233
|
+
<p className="text-sm font-semibold text-text-primary">Widget Crashed</p>
|
|
234
|
+
<p className="text-xs text-text-muted text-center max-w-[280px] font-mono bg-surface-glass px-3 py-2 rounded-lg">
|
|
235
|
+
{this.state.error?.message || 'Unknown error'}
|
|
236
|
+
</p>
|
|
237
|
+
<button
|
|
238
|
+
onClick={() => { this.setState({ hasError: false, error: undefined }); this.props.onReset?.(); }}
|
|
239
|
+
className="px-4 py-1.5 rounded-lg text-xs font-medium bg-surface-glass hover:bg-surface-elevated border border-border-subtle transition-colors"
|
|
240
|
+
>
|
|
241
|
+
Retry
|
|
242
|
+
</button>
|
|
243
|
+
</div>
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
return this.props.children;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
251
|
+
// Main Renderer
|
|
252
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
253
|
+
|
|
254
|
+
interface WidgetRendererProps {
|
|
255
|
+
widget: WidgetDef;
|
|
256
|
+
onClose?: () => void;
|
|
257
|
+
className?: string;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export function WidgetRenderer({ widget, onClose, className = '' }: WidgetRendererProps) {
|
|
261
|
+
return (
|
|
262
|
+
<WidgetErrorBoundary widgetId={widget.id}>
|
|
263
|
+
{widget.url
|
|
264
|
+
? <IframeWidget widget={widget} onClose={onClose} className={className} />
|
|
265
|
+
: widget.schema
|
|
266
|
+
? <SDUIWidget widget={widget} onClose={onClose} className={className} />
|
|
267
|
+
: widget.scriptUrl
|
|
268
|
+
? <HeadlessWidget widget={widget} onClose={onClose} className={className} />
|
|
269
|
+
: widget.component
|
|
270
|
+
? <NativeComponentWidget widget={widget} onClose={onClose} className={className} />
|
|
271
|
+
: (
|
|
272
|
+
<div className={`w-full h-full flex items-center justify-center text-text-muted ${className}`}>
|
|
273
|
+
<p className="text-xs">Widget "{widget.title}" has no schema, URL, scriptUrl, or component</p>
|
|
274
|
+
</div>
|
|
275
|
+
)
|
|
276
|
+
}
|
|
277
|
+
</WidgetErrorBoundary>
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
282
|
+
// OPTION D: Native Component Widget
|
|
283
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
284
|
+
// Renders a React component natively bound into the host process.
|
|
285
|
+
|
|
286
|
+
function NativeComponentWidget({ widget, onClose, className = '' }: WidgetRendererProps) {
|
|
287
|
+
const Component = widget.component;
|
|
288
|
+
return (
|
|
289
|
+
<div className={`w-full h-full flex flex-col bg-surface-primary ${className} min-w-0 min-h-0`}>
|
|
290
|
+
<WidgetToolbar label="OPTION D · NATIVE" color="emerald" title={widget.title} onClose={onClose}>
|
|
291
|
+
<div className="flex items-center gap-1 px-1.5 py-0.5 rounded bg-emerald-500/10 border border-emerald-500/20">
|
|
292
|
+
<span className="text-[9px] text-emerald-400 font-mono">REACT</span>
|
|
293
|
+
</div>
|
|
294
|
+
</WidgetToolbar>
|
|
295
|
+
<div className="flex-1 min-h-0 min-w-0 overflow-y-auto overflow-x-auto bg-surface-primary rr-block relative" data-rr-block>
|
|
296
|
+
<React.Suspense fallback={<LoadingOverlay title={widget.title} color="emerald" />}>
|
|
297
|
+
<Component />
|
|
298
|
+
</React.Suspense>
|
|
299
|
+
</div>
|
|
300
|
+
</div>
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
306
|
+
// OPTION C: Headless Worker-Sandbox Widget
|
|
307
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
308
|
+
// Runs JS code inside a Web Worker (PluginSandbox from @decido/sdk).
|
|
309
|
+
// Shows a minimal status card — no iframe or SDUI needed.
|
|
310
|
+
|
|
311
|
+
function HeadlessWidget({ widget, onClose, className = '' }: WidgetRendererProps) {
|
|
312
|
+
const [status, setStatus] = React.useState<'idle' | 'running' | 'stopped' | 'error'>('idle');
|
|
313
|
+
const [runCount, setRunCount] = React.useState(0);
|
|
314
|
+
const [lastError, setLastError] = React.useState<string | null>(null);
|
|
315
|
+
const sandboxRef = React.useRef<any>(null);
|
|
316
|
+
|
|
317
|
+
// Cleanup on unmount
|
|
318
|
+
React.useEffect(() => {
|
|
319
|
+
return () => {
|
|
320
|
+
if (sandboxRef.current) {
|
|
321
|
+
sandboxRef.current.destroy();
|
|
322
|
+
sandboxRef.current = null;
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
}, []);
|
|
326
|
+
|
|
327
|
+
const handleStart = React.useCallback(async () => {
|
|
328
|
+
try {
|
|
329
|
+
setStatus('running');
|
|
330
|
+
setLastError(null);
|
|
331
|
+
|
|
332
|
+
const scriptUrl = widget.scriptUrl || '';
|
|
333
|
+
|
|
334
|
+
// ── Path A: WASM plugin (wasm://name/function) → Tauri IPC bridge ──
|
|
335
|
+
if (scriptUrl.startsWith('wasm://')) {
|
|
336
|
+
const parts = scriptUrl.replace('wasm://', '').split('/');
|
|
337
|
+
const pluginName = parts[0];
|
|
338
|
+
const fnName = parts[1] || 'run';
|
|
339
|
+
|
|
340
|
+
// Dynamic import the WASM bridge
|
|
341
|
+
const { runWasmPlugin } = await import('../../../../sdk/src/wasm');
|
|
342
|
+
const result = await runWasmPlugin(pluginName, fnName);
|
|
343
|
+
console.log(`[WASM] Plugin "${pluginName}.${fnName}" result:`, result);
|
|
344
|
+
setRunCount(c => c + 1);
|
|
345
|
+
setStatus('stopped');
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// ── Path B: JS Worker plugin → PluginSandbox ──
|
|
350
|
+
const { PluginSandbox } = await import('../../../../sdk/src/sandbox/PluginSandbox');
|
|
351
|
+
const sandbox = new PluginSandbox({
|
|
352
|
+
pluginId: widget.pluginId,
|
|
353
|
+
pluginUrl: scriptUrl,
|
|
354
|
+
tenantId: 'default',
|
|
355
|
+
permissions: widget.permissions || [],
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
// Connect RPC callback so Worker→Kernel calls work
|
|
359
|
+
sandbox.onRpcRequest = async (action: string, payload: any) => {
|
|
360
|
+
return new Promise((resolve, reject) => {
|
|
361
|
+
const reqId = `headless-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
362
|
+
const handler = (e: MessageEvent) => {
|
|
363
|
+
if (e.data?.reqId === reqId) {
|
|
364
|
+
window.removeEventListener('message', handler);
|
|
365
|
+
if (e.data.error) reject(new Error(e.data.error));
|
|
366
|
+
else resolve(e.data.result);
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
window.addEventListener('message', handler);
|
|
370
|
+
window.postMessage({
|
|
371
|
+
type: 'DECIDO_RPC',
|
|
372
|
+
reqId,
|
|
373
|
+
action,
|
|
374
|
+
pluginId: widget.pluginId,
|
|
375
|
+
payload,
|
|
376
|
+
}, window.location.origin);
|
|
377
|
+
});
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
sandbox.onError = (error: string) => {
|
|
381
|
+
setLastError(error);
|
|
382
|
+
setStatus('error');
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
sandboxRef.current = sandbox;
|
|
386
|
+
await sandbox.start();
|
|
387
|
+
setRunCount(c => c + 1);
|
|
388
|
+
} catch (err: any) {
|
|
389
|
+
setStatus('error');
|
|
390
|
+
setLastError(typeof err === 'string' ? err : err?.message || String(err));
|
|
391
|
+
console.error('[HeadlessWidget] Error:', err);
|
|
392
|
+
}
|
|
393
|
+
}, [widget]);
|
|
394
|
+
|
|
395
|
+
const handleStop = React.useCallback(() => {
|
|
396
|
+
if (sandboxRef.current) {
|
|
397
|
+
sandboxRef.current.destroy();
|
|
398
|
+
sandboxRef.current = null;
|
|
399
|
+
}
|
|
400
|
+
setStatus('stopped');
|
|
401
|
+
}, []);
|
|
402
|
+
|
|
403
|
+
const statusColors = {
|
|
404
|
+
idle: 'bg-gray-500/20 text-gray-400',
|
|
405
|
+
running: 'bg-emerald-500/20 text-emerald-400',
|
|
406
|
+
stopped: 'bg-amber-500/20 text-amber-400',
|
|
407
|
+
error: 'bg-red-500/20 text-red-400',
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
return (
|
|
411
|
+
<div className={`flex flex-col h-full ${className}`}>
|
|
412
|
+
<WidgetToolbar title={widget.title} label="WORKER" color="violet" onClose={onClose}>
|
|
413
|
+
<div className={`flex items-center gap-1 px-1.5 py-0.5 rounded ${statusColors[status]}`}>
|
|
414
|
+
<span className="text-[9px] font-mono uppercase">{status}</span>
|
|
415
|
+
</div>
|
|
416
|
+
</WidgetToolbar>
|
|
417
|
+
<div className="flex-1 flex flex-col items-center justify-center gap-3 p-4">
|
|
418
|
+
<div className="text-center">
|
|
419
|
+
<p className="text-xs text-text-muted mb-1">Worker Sandbox Plugin</p>
|
|
420
|
+
<p className="text-[10px] text-text-disabled font-mono">{widget.scriptUrl}</p>
|
|
421
|
+
</div>
|
|
422
|
+
<div className="flex gap-2">
|
|
423
|
+
<button
|
|
424
|
+
onClick={handleStart}
|
|
425
|
+
disabled={status === 'running'}
|
|
426
|
+
className="px-3 py-1.5 text-xs rounded bg-emerald-500/20 text-emerald-400 hover:bg-emerald-500/30 disabled:opacity-50 transition-colors"
|
|
427
|
+
>
|
|
428
|
+
▶ Start
|
|
429
|
+
</button>
|
|
430
|
+
<button
|
|
431
|
+
onClick={handleStop}
|
|
432
|
+
disabled={status !== 'running'}
|
|
433
|
+
className="px-3 py-1.5 text-xs rounded bg-red-500/20 text-red-400 hover:bg-red-500/30 disabled:opacity-50 transition-colors"
|
|
434
|
+
>
|
|
435
|
+
■ Stop
|
|
436
|
+
</button>
|
|
437
|
+
</div>
|
|
438
|
+
{runCount > 0 && (
|
|
439
|
+
<p className="text-[10px] text-text-disabled">Runs: {runCount}</p>
|
|
440
|
+
)}
|
|
441
|
+
{lastError && (
|
|
442
|
+
<p className="text-[10px] text-red-400 max-w-[200px] text-center">{lastError}</p>
|
|
443
|
+
)}
|
|
444
|
+
</div>
|
|
445
|
+
</div>
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
450
|
+
// OPTION B: iframe Widget (evolved)
|
|
451
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
452
|
+
|
|
453
|
+
function IframeWidget({ widget, onClose, className = '' }: WidgetRendererProps) {
|
|
454
|
+
const iframeRef = React.useRef<HTMLIFrameElement>(null);
|
|
455
|
+
const bridgeRef = React.useRef<PluginBridge>(new PluginBridge());
|
|
456
|
+
const [isLoading, setIsLoading] = React.useState(true);
|
|
457
|
+
const [hasError, setHasError] = React.useState(false);
|
|
458
|
+
const [isOnline, setIsOnline] = React.useState(true);
|
|
459
|
+
|
|
460
|
+
// Auto-detect plugin availability
|
|
461
|
+
React.useEffect(() => {
|
|
462
|
+
if (!widget.url) return;
|
|
463
|
+
const controller = new AbortController();
|
|
464
|
+
fetch(widget.url, { mode: 'no-cors', signal: controller.signal })
|
|
465
|
+
.then(() => setIsOnline(true))
|
|
466
|
+
.catch(() => setIsOnline(false));
|
|
467
|
+
return () => controller.abort();
|
|
468
|
+
}, [widget.url]);
|
|
469
|
+
|
|
470
|
+
// Attach postMessage bridge
|
|
471
|
+
React.useEffect(() => {
|
|
472
|
+
const bridge = bridgeRef.current;
|
|
473
|
+
// Core handlers
|
|
474
|
+
bridge.onRequest('getTheme', () => {
|
|
475
|
+
return document.documentElement.classList.contains('dark') ? 'dark' : 'light';
|
|
476
|
+
});
|
|
477
|
+
bridge.onRequest('getData', (p: { path: string }) => {
|
|
478
|
+
return widgetDataStore.resolve(p.path);
|
|
479
|
+
});
|
|
480
|
+
bridge.onRequest('navigate', (p: { route: string }) => {
|
|
481
|
+
console.log('[Bridge] Plugin requested navigation:', p.route);
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
// Expanded handlers
|
|
485
|
+
bridge.onRequest('requestAction', (p: { command: string; payload?: any }) => {
|
|
486
|
+
console.log('[Bridge] Action from plugin:', p.command);
|
|
487
|
+
window.dispatchEvent(new CustomEvent('sdui:action', {
|
|
488
|
+
detail: { command: p.command, payload: p.payload }
|
|
489
|
+
}));
|
|
490
|
+
return { dispatched: true };
|
|
491
|
+
});
|
|
492
|
+
bridge.onRequest('getWidgets', () => {
|
|
493
|
+
// Use window-level reference to avoid circular import
|
|
494
|
+
const store = (window as any).__pluginWidgetStore;
|
|
495
|
+
if (!store) return { widgets: [] };
|
|
496
|
+
return { widgets: store.getVisible().map((w: any) => ({ id: w.id, title: w.title, pluginId: w.pluginId })) };
|
|
497
|
+
});
|
|
498
|
+
bridge.onRequest('openExternal', (p: { url: string }) => {
|
|
499
|
+
window.open(p.url, '_blank', 'noopener,noreferrer');
|
|
500
|
+
return { opened: true };
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
return () => bridge.detach();
|
|
504
|
+
}, []);
|
|
505
|
+
|
|
506
|
+
React.useEffect(() => {
|
|
507
|
+
setIsLoading(true);
|
|
508
|
+
setHasError(false);
|
|
509
|
+
}, [widget.url]);
|
|
510
|
+
|
|
511
|
+
const handleIframeLoad = () => {
|
|
512
|
+
setIsLoading(false);
|
|
513
|
+
setHasError(false);
|
|
514
|
+
if (iframeRef.current) {
|
|
515
|
+
bridgeRef.current.attach(iframeRef.current);
|
|
516
|
+
}
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
const handleRefresh = () => {
|
|
520
|
+
if (iframeRef.current) {
|
|
521
|
+
setIsLoading(true);
|
|
522
|
+
setHasError(false);
|
|
523
|
+
iframeRef.current.src = iframeRef.current.src;
|
|
524
|
+
}
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
if (!isOnline) {
|
|
528
|
+
return (
|
|
529
|
+
<div className={`w-full h-full flex flex-col bg-surface-primary ${className}`}>
|
|
530
|
+
<WidgetToolbar label="OPTION B · IFRAME" color="blue" title={widget.title} onClose={onClose} onRefresh={() => setIsOnline(true)} />
|
|
531
|
+
<div className="flex-1 flex flex-col items-center justify-center gap-3">
|
|
532
|
+
<div className="w-14 h-14 rounded-2xl bg-amber-500/10 border border-amber-500/20 flex items-center justify-center">
|
|
533
|
+
<WifiOff className="w-7 h-7 text-amber-400" />
|
|
534
|
+
</div>
|
|
535
|
+
<p className="text-sm font-semibold text-text-primary">Plugin Offline</p>
|
|
536
|
+
<p className="text-xs text-text-muted">{widget.url} no responde</p>
|
|
537
|
+
<button onClick={() => { setIsOnline(true); }} className="px-3 py-1.5 rounded-lg text-xs font-medium bg-surface-glass hover:bg-surface-elevated transition-colors">
|
|
538
|
+
Reintentar
|
|
539
|
+
</button>
|
|
540
|
+
</div>
|
|
541
|
+
</div>
|
|
542
|
+
);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
return (
|
|
546
|
+
<div className={`w-full h-full flex flex-col bg-surface-primary ${className}`}>
|
|
547
|
+
<WidgetToolbar label="OPTION B · IFRAME" color="blue" title={widget.title} onClose={onClose} onRefresh={handleRefresh}>
|
|
548
|
+
<div className="flex items-center gap-1 px-1.5 py-0.5 rounded bg-blue-500/10 border border-blue-500/20">
|
|
549
|
+
<Wifi className="w-2.5 h-2.5 text-blue-400" />
|
|
550
|
+
<span className="text-[9px] text-blue-400 font-mono">BRIDGE</span>
|
|
551
|
+
</div>
|
|
552
|
+
</WidgetToolbar>
|
|
553
|
+
<div className="flex-1 relative rr-block" data-rr-block>
|
|
554
|
+
{isLoading && <LoadingOverlay title={widget.title} color="blue" />}
|
|
555
|
+
{hasError && <ErrorOverlay url={widget.url || ''} onRetry={handleRefresh} />}
|
|
556
|
+
<iframe
|
|
557
|
+
ref={iframeRef}
|
|
558
|
+
src={widget.url}
|
|
559
|
+
className="w-full h-full border-0 rr-block"
|
|
560
|
+
onLoad={handleIframeLoad}
|
|
561
|
+
onError={() => { setIsLoading(false); setHasError(true); }}
|
|
562
|
+
sandbox="allow-scripts allow-same-origin allow-popups allow-forms allow-modals"
|
|
563
|
+
data-rr-block
|
|
564
|
+
title={widget.title}
|
|
565
|
+
/>
|
|
566
|
+
</div>
|
|
567
|
+
</div>
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
572
|
+
// OPTION A: SDUI Widget (evolved)
|
|
573
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
574
|
+
|
|
575
|
+
function SDUIWidget({ widget, onClose, className = '' }: WidgetRendererProps) {
|
|
576
|
+
// Subscribe to data store for reactive updates
|
|
577
|
+
const [, forceRender] = React.useReducer(x => x + 1, 0);
|
|
578
|
+
React.useEffect(() => widgetDataStore.subscribe(forceRender), []);
|
|
579
|
+
|
|
580
|
+
return (
|
|
581
|
+
<div className={`w-full h-full flex flex-col bg-surface-primary overflow-hidden ${className}`}>
|
|
582
|
+
<WidgetToolbar label="OPTION A · SDUI" color="emerald" title={widget.title} onClose={onClose}>
|
|
583
|
+
<div className="flex items-center gap-1 px-1.5 py-0.5 rounded bg-emerald-500/10 border border-emerald-500/20">
|
|
584
|
+
<span className="text-[9px] text-emerald-400 font-mono">REACTIVE</span>
|
|
585
|
+
</div>
|
|
586
|
+
</WidgetToolbar>
|
|
587
|
+
<div className="flex-1 overflow-auto">
|
|
588
|
+
{widget.schema && <SDUINode node={widget.schema} />}
|
|
589
|
+
</div>
|
|
590
|
+
</div>
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
595
|
+
// Shared UI Components
|
|
596
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
597
|
+
|
|
598
|
+
function WidgetToolbar({ label, color, title, onClose, onRefresh, children }: {
|
|
599
|
+
label: string; color: string; title: string;
|
|
600
|
+
onClose?: () => void; onRefresh?: () => void; children?: React.ReactNode;
|
|
601
|
+
}) {
|
|
602
|
+
const colorClasses: Record<string, string> = {
|
|
603
|
+
blue: 'text-blue-400 bg-blue-400',
|
|
604
|
+
emerald: 'text-emerald-400 bg-emerald-400',
|
|
605
|
+
};
|
|
606
|
+
const textColor = colorClasses[color]?.split(' ')[0] || 'text-cyan-400';
|
|
607
|
+
const dotColor = colorClasses[color]?.split(' ')[1] || 'bg-cyan-400';
|
|
608
|
+
|
|
609
|
+
return (
|
|
610
|
+
<div className="flex-none flex items-center justify-between px-3 py-1.5 bg-surface-secondary/80 backdrop-blur-sm border-b border-border-subtle">
|
|
611
|
+
<div className="flex items-center gap-2">
|
|
612
|
+
<div className={`w-2 h-2 rounded-full ${dotColor} animate-pulse`} />
|
|
613
|
+
<span className={`text-[10px] font-bold ${textColor} tracking-wider uppercase`}>{label}</span>
|
|
614
|
+
<span className="text-xs font-medium text-text-primary">{title}</span>
|
|
615
|
+
{children}
|
|
616
|
+
</div>
|
|
617
|
+
<div className="flex items-center gap-1">
|
|
618
|
+
{onRefresh && (
|
|
619
|
+
<button onClick={onRefresh} className="p-1 rounded text-text-muted hover:text-text-primary hover:bg-surface-glass transition-colors" title="Refresh">
|
|
620
|
+
<RefreshCw className="w-3 h-3" />
|
|
621
|
+
</button>
|
|
622
|
+
)}
|
|
623
|
+
{onClose && (
|
|
624
|
+
<button onClick={onClose} className="p-1 rounded text-text-muted hover:text-red-400 hover:bg-red-500/10 transition-colors" title="Close">
|
|
625
|
+
<X className="w-3 h-3" />
|
|
626
|
+
</button>
|
|
627
|
+
)}
|
|
628
|
+
</div>
|
|
629
|
+
</div>
|
|
630
|
+
);
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
function LoadingOverlay({ title, color }: { title: string; color: string }) {
|
|
634
|
+
const borderColor = color === 'blue' ? 'border-blue-400' : 'border-emerald-400';
|
|
635
|
+
const borderFade = color === 'blue' ? 'border-blue-500/20' : 'border-emerald-500/20';
|
|
636
|
+
return (
|
|
637
|
+
<div className="absolute inset-0 flex items-center justify-center bg-surface-primary z-10">
|
|
638
|
+
<div className="flex flex-col items-center gap-2">
|
|
639
|
+
<div className={`w-6 h-6 border-2 ${borderFade} rounded-full relative`}>
|
|
640
|
+
<div className={`absolute inset-0 w-6 h-6 border-2 border-transparent border-t-current ${borderColor} rounded-full animate-spin`} />
|
|
641
|
+
</div>
|
|
642
|
+
<p className="text-[10px] text-text-muted">Loading {title}...</p>
|
|
643
|
+
</div>
|
|
644
|
+
</div>
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
function ErrorOverlay({ url, onRetry }: { url: string; onRetry: () => void }) {
|
|
649
|
+
return (
|
|
650
|
+
<div className="absolute inset-0 flex items-center justify-center bg-surface-primary z-10">
|
|
651
|
+
<div className="text-center space-y-2">
|
|
652
|
+
<X className="w-8 h-8 text-red-400 mx-auto" />
|
|
653
|
+
<p className="text-xs text-text-muted">Failed to load {url}</p>
|
|
654
|
+
<button onClick={onRetry} className="text-[10px] px-2 py-1 rounded bg-surface-glass hover:bg-surface-elevated transition-colors">Retry</button>
|
|
655
|
+
</div>
|
|
656
|
+
</div>
|
|
657
|
+
);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
661
|
+
// SDUI Node Renderer (evolved with data binding + rich components)
|
|
662
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
663
|
+
|
|
664
|
+
const SAFE_TAGS = new Set([
|
|
665
|
+
'div', 'span', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
|
|
666
|
+
'button', 'a', 'img', 'ul', 'ol', 'li', 'table', 'thead',
|
|
667
|
+
'tbody', 'tr', 'th', 'td', 'form', 'input', 'textarea',
|
|
668
|
+
'select', 'option', 'label', 'section', 'article', 'nav',
|
|
669
|
+
'header', 'footer', 'main', 'aside',
|
|
670
|
+
]);
|
|
671
|
+
|
|
672
|
+
/** Rich SDUI components — special `type` values that render React components */
|
|
673
|
+
const RICH_COMPONENTS: Record<string, React.FC<any>> = {
|
|
674
|
+
'MetricCard': MetricCard,
|
|
675
|
+
'ProgressBar': ProgressBar,
|
|
676
|
+
'StatusBadge': StatusBadge,
|
|
677
|
+
'EventList': EventList,
|
|
678
|
+
'ErrorList': ErrorList,
|
|
679
|
+
'Sparkline': Sparkline,
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
function SDUINode({ node }: { node: ServerDrivenNode | string }): React.ReactElement | null {
|
|
683
|
+
if (typeof node === 'string') return <>{node}</>;
|
|
684
|
+
if (!node || !node.type) return null;
|
|
685
|
+
|
|
686
|
+
// Conditional rendering — resolve against data store
|
|
687
|
+
if (node.conditional) {
|
|
688
|
+
const val = widgetDataStore.resolve(node.conditional);
|
|
689
|
+
if (!val) return null;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
// Data binding — resolve `dataSource` and inject into props
|
|
693
|
+
let resolvedProps = { ...node.props };
|
|
694
|
+
if (node.dataSource) {
|
|
695
|
+
const val = widgetDataStore.resolve(node.dataSource);
|
|
696
|
+
if (val !== undefined) {
|
|
697
|
+
// For text nodes, replace children with resolved value
|
|
698
|
+
if (typeof val === 'string' || typeof val === 'number') {
|
|
699
|
+
return React.createElement(node.type, resolvedProps, String(val));
|
|
700
|
+
}
|
|
701
|
+
resolvedProps = { ...resolvedProps, data: val };
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
// Event handlers
|
|
706
|
+
if (node.onAction) {
|
|
707
|
+
const action = node.onAction;
|
|
708
|
+
resolvedProps.onClick = () => {
|
|
709
|
+
console.log(`[SDUI Action] ${action.command}`, action.payload);
|
|
710
|
+
window.dispatchEvent(new CustomEvent('sdui:action', {
|
|
711
|
+
detail: { command: action.command, payload: action.payload }
|
|
712
|
+
}));
|
|
713
|
+
};
|
|
714
|
+
resolvedProps.style = { ...resolvedProps.style, cursor: 'pointer' };
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
// Rich components
|
|
718
|
+
const RichComponent = RICH_COMPONENTS[node.type];
|
|
719
|
+
if (RichComponent) {
|
|
720
|
+
return <RichComponent {...resolvedProps} />;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
// Standard HTML elements
|
|
724
|
+
if (!SAFE_TAGS.has(node.type)) {
|
|
725
|
+
return (
|
|
726
|
+
<div className="p-2 border border-amber-500/20 rounded bg-amber-500/5 text-xs text-amber-400">
|
|
727
|
+
Unknown: {node.type}
|
|
728
|
+
</div>
|
|
729
|
+
);
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
const children = node.children?.map((child: ServerDrivenNode | string, i: number) => (
|
|
733
|
+
<SDUINode key={i} node={child} />
|
|
734
|
+
));
|
|
735
|
+
|
|
736
|
+
return React.createElement(node.type, resolvedProps, children);
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
740
|
+
// Rich SDUI Component Library
|
|
741
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
742
|
+
|
|
743
|
+
function MetricCard({ label, value, unit, color = 'cyan', trend }: {
|
|
744
|
+
label: string; value: string | number; unit?: string; color?: string; trend?: 'up' | 'down' | 'stable';
|
|
745
|
+
}) {
|
|
746
|
+
const colors: Record<string, string> = {
|
|
747
|
+
cyan: 'bg-cyan-900/30 border-cyan-500/20 text-cyan-400',
|
|
748
|
+
emerald: 'bg-emerald-900/30 border-emerald-500/20 text-emerald-400',
|
|
749
|
+
amber: 'bg-amber-900/30 border-amber-500/20 text-amber-400',
|
|
750
|
+
red: 'bg-red-900/30 border-red-500/20 text-red-400',
|
|
751
|
+
blue: 'bg-blue-900/30 border-blue-500/20 text-blue-400',
|
|
752
|
+
};
|
|
753
|
+
const trendIcons = { up: '↑', down: '↓', stable: '→' };
|
|
754
|
+
const trendColors = { up: 'text-emerald-400', down: 'text-red-400', stable: 'text-zinc-500' };
|
|
755
|
+
|
|
756
|
+
return (
|
|
757
|
+
<div className={`p-3 rounded-lg border text-center ${colors[color] || colors.cyan}`}>
|
|
758
|
+
<div className="text-2xl font-bold flex items-center justify-center gap-1">
|
|
759
|
+
{value}{unit && <span className="text-sm opacity-60">{unit}</span>}
|
|
760
|
+
{trend && <span className={`text-sm ${trendColors[trend]}`}>{trendIcons[trend]}</span>}
|
|
761
|
+
</div>
|
|
762
|
+
<div className="text-[10px] text-zinc-400 uppercase tracking-wider mt-1">{label}</div>
|
|
763
|
+
</div>
|
|
764
|
+
);
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
function ProgressBar({ value, max = 100, label, color = 'cyan' }: {
|
|
768
|
+
value: number; max?: number; label?: string; color?: string;
|
|
769
|
+
}) {
|
|
770
|
+
const pct = Math.min(100, (value / max) * 100);
|
|
771
|
+
const barColors: Record<string, string> = {
|
|
772
|
+
cyan: 'bg-cyan-500', emerald: 'bg-emerald-500', amber: 'bg-amber-500', red: 'bg-red-500',
|
|
773
|
+
};
|
|
774
|
+
|
|
775
|
+
return (
|
|
776
|
+
<div className="space-y-1">
|
|
777
|
+
{label && (
|
|
778
|
+
<div className="flex items-center justify-between text-xs">
|
|
779
|
+
<span className="text-zinc-400">{label}</span>
|
|
780
|
+
<span className="text-zinc-300 font-mono">{pct.toFixed(0)}%</span>
|
|
781
|
+
</div>
|
|
782
|
+
)}
|
|
783
|
+
<div className="h-2 rounded-full bg-zinc-800 overflow-hidden">
|
|
784
|
+
<div
|
|
785
|
+
className={`h-full rounded-full transition-all duration-700 ${barColors[color] || barColors.cyan}`}
|
|
786
|
+
style={{ width: `${pct}%` }}
|
|
787
|
+
/>
|
|
788
|
+
</div>
|
|
789
|
+
</div>
|
|
790
|
+
);
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
function StatusBadge({ status, label }: { status: 'online' | 'offline' | 'warning' | 'error'; label?: string }) {
|
|
794
|
+
const styles: Record<string, string> = {
|
|
795
|
+
online: 'bg-emerald-500/10 border-emerald-500/20 text-emerald-400',
|
|
796
|
+
offline: 'bg-zinc-500/10 border-zinc-500/20 text-zinc-400',
|
|
797
|
+
warning: 'bg-amber-500/10 border-amber-500/20 text-amber-400',
|
|
798
|
+
error: 'bg-red-500/10 border-red-500/20 text-red-400',
|
|
799
|
+
};
|
|
800
|
+
const dots: Record<string, string> = {
|
|
801
|
+
online: 'bg-emerald-400', offline: 'bg-zinc-500', warning: 'bg-amber-400', error: 'bg-red-400',
|
|
802
|
+
};
|
|
803
|
+
|
|
804
|
+
return (
|
|
805
|
+
<span className={`inline-flex items-center gap-1.5 px-2 py-0.5 rounded-full border text-[10px] font-semibold uppercase tracking-wider ${styles[status]}`}>
|
|
806
|
+
<span className={`w-1.5 h-1.5 rounded-full ${dots[status]} ${status === 'online' ? 'animate-pulse' : ''}`} />
|
|
807
|
+
{label || status}
|
|
808
|
+
</span>
|
|
809
|
+
);
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
function EventList({ data, maxItems = 6 }: { data?: any[]; maxItems?: number }) {
|
|
813
|
+
const events = Array.isArray(data) ? data.slice(0, maxItems) : [];
|
|
814
|
+
if (events.length === 0) {
|
|
815
|
+
return <p className="text-xs text-zinc-500 italic">No events</p>;
|
|
816
|
+
}
|
|
817
|
+
const typeColors: Record<string, string> = {
|
|
818
|
+
pageload: 'text-cyan-400', api: 'text-blue-400', click: 'text-emerald-400',
|
|
819
|
+
session: 'text-purple-400', error: 'text-red-400',
|
|
820
|
+
};
|
|
821
|
+
|
|
822
|
+
return (
|
|
823
|
+
<div className="space-y-0.5">
|
|
824
|
+
{events.map((ev: any, i: number) => (
|
|
825
|
+
<div key={i} className="flex items-center gap-2 text-xs py-1.5 px-2 rounded hover:bg-white/[0.02] transition-colors">
|
|
826
|
+
<span className="text-zinc-600 font-mono text-[10px] w-16 shrink-0">{ev.time}</span>
|
|
827
|
+
<span className={`text-[10px] font-bold uppercase tracking-wider w-16 shrink-0 ${typeColors[ev.type] || 'text-zinc-500'}`}>{ev.type}</span>
|
|
828
|
+
<span className="text-zinc-400 truncate">{ev.path}</span>
|
|
829
|
+
{ev.duration > 0 && <span className="ml-auto text-zinc-600 font-mono text-[10px] shrink-0">{ev.duration}ms</span>}
|
|
830
|
+
</div>
|
|
831
|
+
))}
|
|
832
|
+
</div>
|
|
833
|
+
);
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
function ErrorList({ data }: { data?: any[] }) {
|
|
837
|
+
const errors = Array.isArray(data) ? data : [];
|
|
838
|
+
if (errors.length === 0) {
|
|
839
|
+
return (
|
|
840
|
+
<div className="flex items-center gap-2 p-3 bg-emerald-900/10 rounded-lg border border-emerald-500/10">
|
|
841
|
+
<span className="text-emerald-400 text-sm">✓</span>
|
|
842
|
+
<span className="text-xs text-emerald-400">No errors detected</span>
|
|
843
|
+
</div>
|
|
844
|
+
);
|
|
845
|
+
}
|
|
846
|
+
return (
|
|
847
|
+
<div className="space-y-2">
|
|
848
|
+
{errors.map((err: any, i: number) => {
|
|
849
|
+
const isError = err.severity === 'error';
|
|
850
|
+
return (
|
|
851
|
+
<div key={i} className={`p-3 rounded-lg border space-y-1 ${isError ? 'bg-red-900/10 border-red-500/20' : 'bg-amber-900/10 border-amber-500/20'}`}>
|
|
852
|
+
<div className="flex items-center justify-between">
|
|
853
|
+
<span className={`text-sm font-medium ${isError ? 'text-red-400' : 'text-amber-400'}`}>{err.message}</span>
|
|
854
|
+
<span className="text-[10px] text-zinc-500">{err.ago}</span>
|
|
855
|
+
</div>
|
|
856
|
+
<div className="text-xs text-zinc-500 font-mono">{err.source}</div>
|
|
857
|
+
</div>
|
|
858
|
+
);
|
|
859
|
+
})}
|
|
860
|
+
</div>
|
|
861
|
+
);
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
function Sparkline({ data, color = 'cyan', height = 32 }: { data?: number[]; color?: string; height?: number }) {
|
|
865
|
+
const values = Array.isArray(data) ? data : [30, 45, 28, 60, 42, 75, 55, 90, 65, 80];
|
|
866
|
+
const max = Math.max(...values);
|
|
867
|
+
const min = Math.min(...values);
|
|
868
|
+
const range = max - min || 1;
|
|
869
|
+
const w = 120;
|
|
870
|
+
const points = values.map((v, i) => {
|
|
871
|
+
const x = (i / (values.length - 1)) * w;
|
|
872
|
+
const y = height - ((v - min) / range) * (height - 4) - 2;
|
|
873
|
+
return `${x},${y}`;
|
|
874
|
+
}).join(' ');
|
|
875
|
+
|
|
876
|
+
const strokeColors: Record<string, string> = {
|
|
877
|
+
cyan: '#22d3ee', emerald: '#34d399', amber: '#fbbf24', red: '#f87171', blue: '#60a5fa',
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
return (
|
|
881
|
+
<svg width={w} height={height} className="inline-block align-middle">
|
|
882
|
+
<polyline
|
|
883
|
+
points={points}
|
|
884
|
+
fill="none"
|
|
885
|
+
stroke={strokeColors[color] || strokeColors.cyan}
|
|
886
|
+
strokeWidth="1.5"
|
|
887
|
+
strokeLinecap="round"
|
|
888
|
+
strokeLinejoin="round"
|
|
889
|
+
/>
|
|
890
|
+
</svg>
|
|
891
|
+
);
|
|
892
|
+
}
|