-
Notifications
You must be signed in to change notification settings - Fork 650
Expand file tree
/
Copy pathApp.jsx
More file actions
181 lines (153 loc) · 5.1 KB
/
App.jsx
File metadata and controls
181 lines (153 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { useEffect, useState } from 'react';
import { Route, Routes } from 'react-router-dom';
import { BrowserRouter as Router } from 'react-router-dom';
import Layout from './components/Layout';
import LoadingState from './components/LoadingState';
import Dashboard from './components/pages/Dashboard';
import EmbedView from './components/pages/EmbedView';
import { comm } from './utils/websocket';
const App = () => {
const [components, setComponents] = useState({ rows: [] });
const [error, setError] = useState(null);
const [config, setConfig] = useState(null);
const [isConnected, setIsConnected] = useState(false);
const [areComponentsLoading, setAreComponentsLoading] = useState(true);
const [isEmbedMode, setIsEmbedMode] = useState(false);
useEffect(() => {
// Check if in embed mode (either from URL or from window.EMBED_CONFIG)
const isEmbed = window.location.pathname.startsWith('/embed') ||
(window.EMBED_CONFIG && window.EMBED_CONFIG.embed_mode);
setIsEmbedMode(isEmbed);
comm.connect();
const unsubscribe = comm.subscribe(handleMessage);
return () => {
unsubscribe();
comm.disconnect();
};
}, []);
useEffect(() => {
const updateTitle = () => {
const title = config?.project?.title;
if (title) {
document.title = title;
}
};
updateTitle();
document.addEventListener('visibilitychange', updateTitle);
return () => document.removeEventListener('visibilitychange', updateTitle);
}, [config]);
const handleMessage = (message) => {
console.log('[App] Received message:', message);
switch (message.type) {
case 'components':
if (message.components) {
refreshComponentsList(message.components);
}
break;
case 'error':
handleError(message.content);
break;
case 'connection_status':
updateConnectionStatus(message);
break;
case 'config':
setConfig(message.config);
break;
case 'initial_state':
// Handle initial state
console.log('[App] Received initial state:', message);
break;
}
};
const refreshComponentsList = (components) => {
if (!components || !components.rows) {
setAreComponentsLoading(false);
console.warn('[App] Invalid components data received:', components);
setComponents({ rows: [] });
return;
}
try {
const updatedRows = components.rows.map((row) =>
row.map((component) => {
if (!component || !component.id) return component;
const currentState = comm.getComponentState(component.id);
return {
...component,
value: currentState !== undefined ? currentState : component.value,
error: null,
};
})
);
console.log('[App] Updating components with:', { rows: updatedRows });
setAreComponentsLoading(false);
setComponents({ rows: updatedRows });
setError(null);
} catch (error) {
console.error('[App] Error processing components:', error);
setAreComponentsLoading(false);
setError('Error processing components data');
setComponents({ rows: [] });
}
};
const handleError = (errorContent) => {
console.error('[App] Received error:', errorContent);
setAreComponentsLoading(false);
setError(errorContent.message);
if (errorContent.componentId) {
setComponents((prevState) => {
if (!prevState || !prevState.rows) return { rows: [] };
return {
rows: prevState.rows.map((row) =>
row.map((component) =>
component.id === errorContent.componentId
? { ...component, error: errorContent.message }
: component
)
),
};
});
}
};
const handleComponentUpdate = (componentId, value) => {
try {
comm.updateComponentState(componentId, value);
} catch (error) {
console.error('[App] Error updating component state:', error);
setComponents((prevState) => {
if (!prevState || !prevState.rows) return { rows: [] };
return {
rows: prevState.rows.map((row) =>
row.map((component) =>
component.id === componentId ? { ...component, error: error.message } : component
)
),
};
});
}
};
const updateConnectionStatus = (message) => {
setIsConnected(message.connected);
setError(message.connected ? null : 'Lost connection. Attempting to reconnect...');
};
console.log('[App] Rendering with:', { components, isConnected, error, isEmbedMode });
// If in embed mode, render the EmbedView directly without Layout
if (isEmbedMode) {
return <EmbedView />;
}
return (
<Router>
<Layout>
{!isConnected || areComponentsLoading ? (
<LoadingState isConnected={isConnected} />
) : (
<Dashboard
components={components}
error={error}
handleComponentUpdate={handleComponentUpdate}
/>
)}
</Layout>
</Router>
);
};
export default App;