@bravostudioai/react 0.1.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/bin/encore-lib.js +3 -0
- package/dist/_virtual/_commonjsHelpers.js +7 -0
- package/dist/_virtual/_commonjsHelpers.js.map +1 -0
- package/dist/_virtual/main.js +8 -0
- package/dist/_virtual/main.js.map +1 -0
- package/dist/_virtual/main2.js +5 -0
- package/dist/_virtual/main2.js.map +1 -0
- package/dist/app.js +9 -0
- package/dist/app.js.map +1 -0
- package/dist/cli/commands/download.js +82 -0
- package/dist/cli/commands/download.js.map +1 -0
- package/dist/cli/commands/generate.js +1526 -0
- package/dist/cli/commands/generate.js.map +1 -0
- package/dist/cli.js +25 -0
- package/dist/cli.js.map +1 -0
- package/dist/components/DynamicComponent.js +24 -0
- package/dist/components/DynamicComponent.js.map +1 -0
- package/dist/components/EncoreApp.js +259 -0
- package/dist/components/EncoreApp.js.map +1 -0
- package/dist/components/EncoreErrorBoundary.js +33 -0
- package/dist/components/EncoreErrorBoundary.js.map +1 -0
- package/dist/components/EncoreLoadingFallback.js +20 -0
- package/dist/components/EncoreLoadingFallback.js.map +1 -0
- package/dist/components.js +1454 -0
- package/dist/components.js.map +1 -0
- package/dist/constants.d.ts +3 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/contexts/EncoreActionContext.js +6 -0
- package/dist/contexts/EncoreActionContext.js.map +1 -0
- package/dist/contexts/EncoreAppContext.js +9 -0
- package/dist/contexts/EncoreAppContext.js.map +1 -0
- package/dist/contexts/EncoreBindingContext.js +6 -0
- package/dist/contexts/EncoreBindingContext.js.map +1 -0
- package/dist/contexts/EncoreComponentIdContext.js +8 -0
- package/dist/contexts/EncoreComponentIdContext.js.map +1 -0
- package/dist/contexts/EncoreRepeatingContainerContext.js +6 -0
- package/dist/contexts/EncoreRepeatingContainerContext.js.map +1 -0
- package/dist/hooks/usePusherUpdates.js +60 -0
- package/dist/hooks/usePusherUpdates.js.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/dynamicModules.js +132 -0
- package/dist/lib/dynamicModules.js.map +1 -0
- package/dist/lib/fetcher.js +58 -0
- package/dist/lib/fetcher.js.map +1 -0
- package/dist/lib/localMode.js +21 -0
- package/dist/lib/localMode.js.map +1 -0
- package/dist/lib/packages.js +18 -0
- package/dist/lib/packages.js.map +1 -0
- package/dist/node_modules/dotenv/lib/main.js +198 -0
- package/dist/node_modules/dotenv/lib/main.js.map +1 -0
- package/dist/node_modules/dotenv/package.json.js +8 -0
- package/dist/node_modules/dotenv/package.json.js.map +1 -0
- package/dist/packages/encore-lib/constants.js +6 -0
- package/dist/packages/encore-lib/constants.js.map +1 -0
- package/dist/src/app.d.ts +5 -0
- package/dist/src/app.d.ts.map +1 -0
- package/dist/src/cli/commands/download.d.ts +2 -0
- package/dist/src/cli/commands/download.d.ts.map +1 -0
- package/dist/src/cli/commands/generate.d.ts +2 -0
- package/dist/src/cli/commands/generate.d.ts.map +1 -0
- package/dist/src/cli/index.d.ts +2 -0
- package/dist/src/cli/index.d.ts.map +1 -0
- package/dist/src/components/DynamicComponent.d.ts +12 -0
- package/dist/src/components/DynamicComponent.d.ts.map +1 -0
- package/dist/src/components/EncoreApp.d.ts +27 -0
- package/dist/src/components/EncoreApp.d.ts.map +1 -0
- package/dist/src/components/EncoreErrorBoundary.d.ts +17 -0
- package/dist/src/components/EncoreErrorBoundary.d.ts.map +1 -0
- package/dist/src/components/EncoreLoadingFallback.d.ts +4 -0
- package/dist/src/components/EncoreLoadingFallback.d.ts.map +1 -0
- package/dist/src/components.d.ts +4 -0
- package/dist/src/components.d.ts.map +1 -0
- package/dist/src/contexts/EncoreActionContext.d.ts +13 -0
- package/dist/src/contexts/EncoreActionContext.d.ts.map +1 -0
- package/dist/src/contexts/EncoreAppContext.d.ts +8 -0
- package/dist/src/contexts/EncoreAppContext.d.ts.map +1 -0
- package/dist/src/contexts/EncoreBindingContext.d.ts +5 -0
- package/dist/src/contexts/EncoreBindingContext.d.ts.map +1 -0
- package/dist/src/contexts/EncoreComponentIdContext.d.ts +8 -0
- package/dist/src/contexts/EncoreComponentIdContext.d.ts.map +1 -0
- package/dist/src/contexts/EncoreRepeatingContainerContext.d.ts +21 -0
- package/dist/src/contexts/EncoreRepeatingContainerContext.d.ts.map +1 -0
- package/dist/src/hooks/useAuthRedirect.d.ts +3 -0
- package/dist/src/hooks/useAuthRedirect.d.ts.map +1 -0
- package/dist/src/hooks/usePusherUpdates.d.ts +18 -0
- package/dist/src/hooks/usePusherUpdates.d.ts.map +1 -0
- package/dist/src/index.d.ts +8 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/lib/dynamicModules.d.ts +8 -0
- package/dist/src/lib/dynamicModules.d.ts.map +1 -0
- package/dist/src/lib/fetcher.d.ts +5 -0
- package/dist/src/lib/fetcher.d.ts.map +1 -0
- package/dist/src/lib/localMode.d.ts +3 -0
- package/dist/src/lib/localMode.d.ts.map +1 -0
- package/dist/src/lib/packages.d.ts +6 -0
- package/dist/src/lib/packages.d.ts.map +1 -0
- package/dist/src/stores/useEncoreState.d.ts +33 -0
- package/dist/src/stores/useEncoreState.d.ts.map +1 -0
- package/dist/stores/useEncoreState.js +70 -0
- package/dist/stores/useEncoreState.js.map +1 -0
- package/package.json +60 -0
- package/src/AGENTS.md +161 -0
- package/src/README.md +110 -0
- package/src/app.ts +5 -0
- package/src/cli/commands/download.ts +133 -0
- package/src/cli/commands/generate.ts +3045 -0
- package/src/cli/index.ts +35 -0
- package/src/components/DynamicComponent.tsx +40 -0
- package/src/components/EncoreApp.tsx +759 -0
- package/src/components/EncoreErrorBoundary.tsx +49 -0
- package/src/components/EncoreLoadingFallback.tsx +25 -0
- package/src/components.tsx +3155 -0
- package/src/contexts/EncoreActionContext.ts +18 -0
- package/src/contexts/EncoreAppContext.ts +13 -0
- package/src/contexts/EncoreBindingContext.ts +6 -0
- package/src/contexts/EncoreComponentIdContext.ts +12 -0
- package/src/contexts/EncoreRepeatingContainerContext.ts +30 -0
- package/src/hooks/useAuthRedirect.ts +63 -0
- package/src/hooks/usePusherUpdates.ts +156 -0
- package/src/index.ts +16 -0
- package/src/lib/dynamicModules.ts +193 -0
- package/src/lib/fetcher.ts +108 -0
- package/src/lib/localMode.ts +30 -0
- package/src/lib/moduleRegistry.ts +24 -0
- package/src/lib/packages.ts +33 -0
- package/src/stores/useEncoreState.ts +121 -0
|
@@ -0,0 +1,759 @@
|
|
|
1
|
+
import useSWR from "swr";
|
|
2
|
+
import fetcher from "../lib/fetcher";
|
|
3
|
+
import useEncoreState from "../stores/useEncoreState";
|
|
4
|
+
import React, {
|
|
5
|
+
Suspense,
|
|
6
|
+
useEffect,
|
|
7
|
+
useRef,
|
|
8
|
+
useState,
|
|
9
|
+
useCallback,
|
|
10
|
+
useMemo,
|
|
11
|
+
} from "react";
|
|
12
|
+
import { isLocalMode, setLocalModeOverride } from "../lib/localMode";
|
|
13
|
+
import EncoreBindingContext from "../contexts/EncoreBindingContext";
|
|
14
|
+
import EncoreComponentIdContext from "../contexts/EncoreComponentIdContext";
|
|
15
|
+
import EncoreActionContext, {
|
|
16
|
+
type EncoreActionPayload,
|
|
17
|
+
} from "../contexts/EncoreActionContext";
|
|
18
|
+
import EncoreRepeatingContainerContext, {
|
|
19
|
+
type RepeatingContainerControl,
|
|
20
|
+
} from "../contexts/EncoreRepeatingContainerContext";
|
|
21
|
+
import DynamicComponent from "./DynamicComponent";
|
|
22
|
+
import { Link } from "react-router-dom";
|
|
23
|
+
import { usePusherUpdates } from "../hooks/usePusherUpdates";
|
|
24
|
+
|
|
25
|
+
type Props = {
|
|
26
|
+
appId: string;
|
|
27
|
+
pageId?: string;
|
|
28
|
+
componentId?: string;
|
|
29
|
+
fallback?: React.ReactNode;
|
|
30
|
+
onSizeChange?: (size: { width: number; height: number }) => void;
|
|
31
|
+
onAction?: (payload: EncoreActionPayload) => void | Promise<void>;
|
|
32
|
+
data?: Record<string, string | number | any[]>;
|
|
33
|
+
// When provided, force the runtime to load from either remote or local sources
|
|
34
|
+
source?: "remote" | "local";
|
|
35
|
+
// Control repeating containers (sliders, lists, etc.) by container ID
|
|
36
|
+
repeatingContainerControls?: Record<
|
|
37
|
+
string,
|
|
38
|
+
{ currentIndex?: number; onIndexChange?: (index: number) => void }
|
|
39
|
+
>;
|
|
40
|
+
// Control input groups - maps group name to active element name
|
|
41
|
+
inputGroups?: Record<string, string>;
|
|
42
|
+
// Base URL for the Encore service API (e.g., "https://api.example.com")
|
|
43
|
+
// If not provided, defaults to "https://apps-service-dev.bravostudio.app"
|
|
44
|
+
baseURL?: string;
|
|
45
|
+
appDefinition?: any;
|
|
46
|
+
pageDefinition?: any;
|
|
47
|
+
componentCode?: string;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
type EncoreAssetsById = Record<string, { url?: string }>;
|
|
51
|
+
type EncoreState = {
|
|
52
|
+
setApp: (app: unknown) => void;
|
|
53
|
+
setAppId: (id: string) => void;
|
|
54
|
+
setPageId: (id: string) => void;
|
|
55
|
+
assetsById: EncoreAssetsById;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const setAppSelector = (state: EncoreState) => state.setApp;
|
|
59
|
+
const setAppIdSelector = (state: EncoreState) => state.setAppId;
|
|
60
|
+
const setPageIdSelector = (state: EncoreState) => state.setPageId;
|
|
61
|
+
const assetsByIdSelector = (state: EncoreState) => state.assetsById;
|
|
62
|
+
|
|
63
|
+
const EncoreApp = ({
|
|
64
|
+
appId,
|
|
65
|
+
pageId,
|
|
66
|
+
componentId,
|
|
67
|
+
fallback,
|
|
68
|
+
onSizeChange,
|
|
69
|
+
onAction,
|
|
70
|
+
data,
|
|
71
|
+
source,
|
|
72
|
+
repeatingContainerControls,
|
|
73
|
+
inputGroups,
|
|
74
|
+
baseURL,
|
|
75
|
+
appDefinition,
|
|
76
|
+
pageDefinition,
|
|
77
|
+
componentCode,
|
|
78
|
+
}: Props) => {
|
|
79
|
+
console.log("Render: EncoreApp");
|
|
80
|
+
console.log("🔥 ENCORE-LIB SOURCE CODE IS ACTIVE 🔥");
|
|
81
|
+
console.log("✨ ENCORE-LIB UPDATED - TEST MESSAGE ✨");
|
|
82
|
+
|
|
83
|
+
// CRITICAL: Set baseURL BEFORE any hooks that might trigger fetches
|
|
84
|
+
// This must happen synchronously, not in useEffect, because useSWR will fetch immediately
|
|
85
|
+
if (baseURL) {
|
|
86
|
+
const currentBaseURL = useEncoreState.getState().baseURL;
|
|
87
|
+
if (currentBaseURL !== baseURL) {
|
|
88
|
+
useEncoreState.getState().setBaseURL(baseURL);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Apply source override immediately so hooks below observe correct mode
|
|
93
|
+
if (source) {
|
|
94
|
+
setLocalModeOverride(source === "local" ? "local" : "remote");
|
|
95
|
+
}
|
|
96
|
+
// const [searchParams] = useSearchParams();
|
|
97
|
+
// const noRedirect = searchParams.get("noRedirect");
|
|
98
|
+
const noRedirect = false;
|
|
99
|
+
|
|
100
|
+
const setApp = useEncoreState(setAppSelector);
|
|
101
|
+
const setAppId = useEncoreState(setAppIdSelector);
|
|
102
|
+
const setPageId = useEncoreState(setPageIdSelector);
|
|
103
|
+
const assetsById = useEncoreState(assetsByIdSelector);
|
|
104
|
+
const containerRef = useRef<HTMLDivElement | null>(null);
|
|
105
|
+
|
|
106
|
+
// State to force DynamicComponent reload when updates are received
|
|
107
|
+
const [reloadKey, setReloadKey] = useState<string | number>(0);
|
|
108
|
+
// Set up Pusher to listen for component updates
|
|
109
|
+
const handleUpdate = useCallback(() => {
|
|
110
|
+
// Increment reloadKey to force DynamicComponent to reload
|
|
111
|
+
setReloadKey((prev) => (typeof prev === "number" ? prev + 1 : Date.now()));
|
|
112
|
+
}, []);
|
|
113
|
+
|
|
114
|
+
// Only enable Pusher in remote mode - it doesn't make sense in local mode
|
|
115
|
+
usePusherUpdates({
|
|
116
|
+
appId,
|
|
117
|
+
pageId: pageId || undefined,
|
|
118
|
+
enabled: !isLocalMode() && !appDefinition,
|
|
119
|
+
onUpdate: handleUpdate,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// usePusherUpdates({
|
|
123
|
+
// appId,
|
|
124
|
+
// pageId: pageId || undefined,
|
|
125
|
+
// enabled: false, // DISABLED FOR DEBUGGING
|
|
126
|
+
// onUpdate: handleUpdate,
|
|
127
|
+
// });
|
|
128
|
+
|
|
129
|
+
const useLocalFlag = source === "local" || isLocalMode();
|
|
130
|
+
// If appDefinition is provided, disable SWR fetch by setting url to null
|
|
131
|
+
const appUrl = appDefinition
|
|
132
|
+
? null
|
|
133
|
+
: appId && `/devices/apps/${appId}${useLocalFlag ? "?useLocal=1" : ""}`;
|
|
134
|
+
|
|
135
|
+
const appSWR = useSWR(appUrl, fetcher, {
|
|
136
|
+
suspense: !!appUrl, // Only use suspense if we are fetching
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const app = appDefinition ? { data: appDefinition } : appSWR;
|
|
140
|
+
|
|
141
|
+
useEffect(() => {
|
|
142
|
+
setApp(app.data);
|
|
143
|
+
}, [app.data, setApp]);
|
|
144
|
+
|
|
145
|
+
// Load fonts declared in app.json using the FontFace API
|
|
146
|
+
useEffect(() => {
|
|
147
|
+
type EncoreFont = {
|
|
148
|
+
id?: string;
|
|
149
|
+
url?: string;
|
|
150
|
+
fontName?: { family?: string; postScriptName?: string };
|
|
151
|
+
};
|
|
152
|
+
type AppDataWithFonts = { app?: { fonts?: EncoreFont[] } };
|
|
153
|
+
const fonts: EncoreFont[] =
|
|
154
|
+
(app?.data as AppDataWithFonts | undefined)?.app?.fonts ?? [];
|
|
155
|
+
if (!fonts || fonts.length === 0) return;
|
|
156
|
+
if (typeof window === "undefined" || !("FontFace" in window)) return;
|
|
157
|
+
fonts.forEach((f) => {
|
|
158
|
+
try {
|
|
159
|
+
const family = f?.fontName?.family;
|
|
160
|
+
const url = f?.url;
|
|
161
|
+
const postScriptName = f?.fontName?.postScriptName;
|
|
162
|
+
if (!family || !url) return;
|
|
163
|
+
|
|
164
|
+
// Infer font weight from postScriptName if available
|
|
165
|
+
// This ensures the browser uses the correct font-face variant
|
|
166
|
+
let weight: string | number | undefined = undefined;
|
|
167
|
+
if (postScriptName) {
|
|
168
|
+
const weightMatch = postScriptName.match(
|
|
169
|
+
/(?:^|-)(Thin|ExtraLight|Light|Regular|Medium|SemiBold|Bold|ExtraBold|Black)(?:-|$)/i
|
|
170
|
+
);
|
|
171
|
+
if (weightMatch) {
|
|
172
|
+
const weightName = weightMatch[1].toLowerCase();
|
|
173
|
+
const weightMap: Record<string, number> = {
|
|
174
|
+
thin: 100,
|
|
175
|
+
extralight: 200,
|
|
176
|
+
light: 300,
|
|
177
|
+
regular: 400,
|
|
178
|
+
medium: 500,
|
|
179
|
+
semibold: 600,
|
|
180
|
+
bold: 700,
|
|
181
|
+
extrabold: 800,
|
|
182
|
+
black: 900,
|
|
183
|
+
};
|
|
184
|
+
weight = weightMap[weightName] || 400;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Create FontFace with weight descriptor if we inferred it
|
|
189
|
+
const fontFace =
|
|
190
|
+
weight !== undefined
|
|
191
|
+
? new FontFace(family, `url(${url})`, { weight: weight.toString() })
|
|
192
|
+
: new FontFace(family, `url(${url})`);
|
|
193
|
+
|
|
194
|
+
fontFace
|
|
195
|
+
.load()
|
|
196
|
+
.then((ff) => {
|
|
197
|
+
document.fonts.add(ff);
|
|
198
|
+
})
|
|
199
|
+
.catch(() => {
|
|
200
|
+
// Ignore font load failures to avoid disrupting rendering
|
|
201
|
+
});
|
|
202
|
+
} catch {
|
|
203
|
+
// Best-effort; ignore
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}, [app?.data]);
|
|
207
|
+
|
|
208
|
+
useEffect(() => {
|
|
209
|
+
setAppId(appId);
|
|
210
|
+
}, [appId, setAppId]);
|
|
211
|
+
|
|
212
|
+
useEffect(() => {
|
|
213
|
+
if (!pageId) return;
|
|
214
|
+
setPageId(pageId);
|
|
215
|
+
}, [pageId, setPageId]);
|
|
216
|
+
|
|
217
|
+
// FIXME: Asset data should be embedded into & preloaded by component, not looked up like this
|
|
218
|
+
useEffect(() => {
|
|
219
|
+
if (Object.keys(assetsById).length === 0) return;
|
|
220
|
+
(async () => {
|
|
221
|
+
// Preload images in the browser
|
|
222
|
+
await Promise.allSettled(
|
|
223
|
+
Object.keys(assetsById).map((id) => {
|
|
224
|
+
if (assetsById[id].url) {
|
|
225
|
+
return new Promise((resolve) => {
|
|
226
|
+
const img = new Image();
|
|
227
|
+
img.onload = resolve;
|
|
228
|
+
img.onerror = resolve; // tolerate failures to avoid unhandled rejections
|
|
229
|
+
img.src = assetsById[id].url!;
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
return Promise.resolve();
|
|
233
|
+
})
|
|
234
|
+
);
|
|
235
|
+
})();
|
|
236
|
+
}, [assetsById]);
|
|
237
|
+
|
|
238
|
+
const pageUrl = pageDefinition
|
|
239
|
+
? null
|
|
240
|
+
: appId &&
|
|
241
|
+
pageId &&
|
|
242
|
+
`/devices/apps/${appId}/node/${pageId}${
|
|
243
|
+
useLocalFlag ? "?useLocal=1" : ""
|
|
244
|
+
}`;
|
|
245
|
+
const pageSWR = useSWR(pageUrl, fetcher, { suspense: !!pageUrl });
|
|
246
|
+
const pageData = pageDefinition ? { data: pageDefinition } : pageSWR;
|
|
247
|
+
|
|
248
|
+
// Debug logging commented out to prevent console flooding
|
|
249
|
+
/*
|
|
250
|
+
useEffect(() => {
|
|
251
|
+
if (pageData.data) {
|
|
252
|
+
console.log("=== PAGE DATA STRUCTURE ===");
|
|
253
|
+
console.log("Full pageData.data:", pageData.data);
|
|
254
|
+
console.log("pageData.data keys:", Object.keys(pageData.data));
|
|
255
|
+
console.log("Client data:", pageData.data.clientData);
|
|
256
|
+
console.log("Client data type:", typeof pageData.data.clientData);
|
|
257
|
+
console.log(
|
|
258
|
+
"Client data keys:",
|
|
259
|
+
pageData.data.clientData ? Object.keys(pageData.data.clientData) : "N/A"
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
// Try to find the actual node data structure
|
|
263
|
+
const allKeys = Object.keys(pageData.data);
|
|
264
|
+
console.log("All top-level keys in pageData.data:", allKeys);
|
|
265
|
+
|
|
266
|
+
// Log the full structure more deeply
|
|
267
|
+
console.log(
|
|
268
|
+
"Full pageData.data structure:",
|
|
269
|
+
JSON.stringify(pageData.data, null, 2)
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
const clientData = pageData.data.clientData || pageData.data;
|
|
273
|
+
|
|
274
|
+
// Helper to recursively search for sliders in tree structure
|
|
275
|
+
const findSlidersInTree = (nodeData: any, path = ""): any[] => {
|
|
276
|
+
const sliders: any[] = [];
|
|
277
|
+
if (!nodeData || typeof nodeData !== "object") return sliders;
|
|
278
|
+
|
|
279
|
+
// Check if this node is a slider
|
|
280
|
+
const isSlider =
|
|
281
|
+
nodeData.type === "container:slider" ||
|
|
282
|
+
nodeData.type === "component:slider" ||
|
|
283
|
+
nodeData.name?.toLowerCase().includes("slider") ||
|
|
284
|
+
(Array.isArray(nodeData.tags) &&
|
|
285
|
+
nodeData.tags.some((tag: string) =>
|
|
286
|
+
tag.toLowerCase().includes("slider")
|
|
287
|
+
));
|
|
288
|
+
|
|
289
|
+
if (isSlider) {
|
|
290
|
+
sliders.push({
|
|
291
|
+
path,
|
|
292
|
+
id: nodeData.id,
|
|
293
|
+
name: nodeData.name,
|
|
294
|
+
type: nodeData.type,
|
|
295
|
+
tags: nodeData.tags,
|
|
296
|
+
data: nodeData.data,
|
|
297
|
+
style: nodeData.style,
|
|
298
|
+
fullNodeData: nodeData,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Recursively search children
|
|
303
|
+
if (nodeData.children) {
|
|
304
|
+
const children = Array.isArray(nodeData.children)
|
|
305
|
+
? nodeData.children
|
|
306
|
+
: [nodeData.children];
|
|
307
|
+
children.forEach((child: any, index: number) => {
|
|
308
|
+
if (child && typeof child === "object") {
|
|
309
|
+
sliders.push(...findSlidersInTree(child, `${path}/${index}`));
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return sliders;
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
// Check if clientData is a flat object keyed by ID (most likely)
|
|
318
|
+
let sliders: any[] = [];
|
|
319
|
+
if (
|
|
320
|
+
clientData &&
|
|
321
|
+
typeof clientData === "object" &&
|
|
322
|
+
!Array.isArray(clientData)
|
|
323
|
+
) {
|
|
324
|
+
// Check if it's a flat structure (keyed by IDs)
|
|
325
|
+
const entries = Object.entries(clientData);
|
|
326
|
+
console.log(`Client data has ${entries.length} entries`);
|
|
327
|
+
|
|
328
|
+
// Search all entries for sliders
|
|
329
|
+
entries.forEach(([key, value]: [string, any]) => {
|
|
330
|
+
if (value && typeof value === "object") {
|
|
331
|
+
// Check if this entry itself is a slider
|
|
332
|
+
const isSlider =
|
|
333
|
+
value.type === "container:slider" ||
|
|
334
|
+
value.type === "component:slider" ||
|
|
335
|
+
value.name?.toLowerCase().includes("slider") ||
|
|
336
|
+
(Array.isArray(value.tags) &&
|
|
337
|
+
value.tags.some((tag: string) =>
|
|
338
|
+
tag.toLowerCase().includes("slider")
|
|
339
|
+
));
|
|
340
|
+
|
|
341
|
+
if (isSlider) {
|
|
342
|
+
sliders.push({
|
|
343
|
+
id: key,
|
|
344
|
+
name: value.name,
|
|
345
|
+
type: value.type,
|
|
346
|
+
tags: value.tags,
|
|
347
|
+
data: value.data,
|
|
348
|
+
style: value.style,
|
|
349
|
+
fullNodeData: value,
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// Also search recursively in case it's nested
|
|
354
|
+
sliders.push(...findSlidersInTree(value, key));
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
// If no sliders found in flat structure, try treating it as a tree
|
|
359
|
+
if (sliders.length === 0) {
|
|
360
|
+
sliders = findSlidersInTree(clientData, "root");
|
|
361
|
+
}
|
|
362
|
+
} else {
|
|
363
|
+
// Treat as tree structure
|
|
364
|
+
sliders = findSlidersInTree(clientData, "root");
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
console.log("=== SLIDER COMPONENTS FOUND ===");
|
|
368
|
+
console.log(`Found ${sliders.length} slider(s):`, sliders);
|
|
369
|
+
|
|
370
|
+
sliders.forEach((slider, index) => {
|
|
371
|
+
console.log(`\n--- Slider ${index + 1} ---`);
|
|
372
|
+
console.log("ID:", slider.id);
|
|
373
|
+
console.log("Name:", slider.name);
|
|
374
|
+
console.log("Type:", slider.type);
|
|
375
|
+
console.log("Tags:", slider.tags);
|
|
376
|
+
console.log("Data:", slider.data);
|
|
377
|
+
console.log("Full node data:", slider.fullNodeData);
|
|
378
|
+
|
|
379
|
+
// Look for data binding tags
|
|
380
|
+
if (Array.isArray(slider.tags)) {
|
|
381
|
+
const bindingTags = slider.tags.filter(
|
|
382
|
+
(tag: string) =>
|
|
383
|
+
tag.includes("PROP:") ||
|
|
384
|
+
tag.includes("LIST:") ||
|
|
385
|
+
tag.includes("DATA:")
|
|
386
|
+
);
|
|
387
|
+
if (bindingTags.length > 0) {
|
|
388
|
+
console.log("🔍 Data binding tags found:", bindingTags);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
// Also log all unique tags found across all components for reference
|
|
394
|
+
const allTags = new Set<string>();
|
|
395
|
+
const allEntries = Object.entries(clientData);
|
|
396
|
+
allEntries.forEach(([_, value]: [string, any]) => {
|
|
397
|
+
if (value?.tags && Array.isArray(value.tags)) {
|
|
398
|
+
value.tags.forEach((tag: string) => allTags.add(tag));
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
console.log("\n=== ALL UNIQUE TAGS IN PAGE ===");
|
|
402
|
+
console.log(Array.from(allTags).sort());
|
|
403
|
+
}
|
|
404
|
+
}, [pageData.data]);
|
|
405
|
+
*/
|
|
406
|
+
|
|
407
|
+
// Memoize the context object to prevent infinite re-renders
|
|
408
|
+
// Only recreate when the actual data changes, not on every render
|
|
409
|
+
const context = useMemo(() => {
|
|
410
|
+
let clientData = pageData.data?.clientData;
|
|
411
|
+
|
|
412
|
+
// --- GENERIC DATA PATCHING START ---
|
|
413
|
+
// Recursively patch the data to fix layout and slider issues based on heuristics
|
|
414
|
+
const patchPageData = (node: any) => {
|
|
415
|
+
if (!node || typeof node !== "object") return;
|
|
416
|
+
|
|
417
|
+
// 1. Layout Heuristic: If children widths sum to ~100% or ~375px, force HORIZONTAL layout
|
|
418
|
+
// RELAXED: No longer requires layoutSizingHorizontal: "FIXED" - width data alone is sufficient
|
|
419
|
+
if (
|
|
420
|
+
node.children &&
|
|
421
|
+
Array.isArray(node.children) &&
|
|
422
|
+
node.children.length > 1
|
|
423
|
+
) {
|
|
424
|
+
let totalWidth = 0;
|
|
425
|
+
let childrenWithWidth = 0;
|
|
426
|
+
|
|
427
|
+
node.children.forEach((child: any) => {
|
|
428
|
+
if (child.style?.width) {
|
|
429
|
+
// Width might be a percentage or pixel value.
|
|
430
|
+
// Based on logs, we saw "width: 52" and "width: 48" which sum to 100.
|
|
431
|
+
// If it's percentage, we check if it sums to 100.
|
|
432
|
+
// If it's pixels, we check if it sums to ~375.
|
|
433
|
+
totalWidth += child.style.width;
|
|
434
|
+
childrenWithWidth++;
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
// Check for percentage sum ~100 or pixel sum ~375
|
|
439
|
+
// Only apply if we have at least 2 children with width data
|
|
440
|
+
const isFullWidthRow =
|
|
441
|
+
(Math.abs(totalWidth - 100) < 1 || Math.abs(totalWidth - 375) < 5) &&
|
|
442
|
+
childrenWithWidth >= 2;
|
|
443
|
+
|
|
444
|
+
if (isFullWidthRow) {
|
|
445
|
+
if (!node.style) node.style = {};
|
|
446
|
+
if (!node.style.layout) node.style.layout = {};
|
|
447
|
+
|
|
448
|
+
// Only apply if mode is missing or undefined
|
|
449
|
+
if (!node.style.layout.mode) {
|
|
450
|
+
console.log(
|
|
451
|
+
`[PATCH] Forcing HORIZONTAL layout for node ${node.id} (${childrenWithWidth} children, widths sum: ${totalWidth})`
|
|
452
|
+
);
|
|
453
|
+
node.style.layout.mode = "HORIZONTAL";
|
|
454
|
+
node.style.layout.primaryAxisAlignItems = "flex-start";
|
|
455
|
+
node.style.layout.counterAxisAlignItems = "flex-start";
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// 2. Slider Heuristic: If slider is taller than wide, force VERTICAL animation
|
|
461
|
+
// DISABLED: This is too aggressive and breaks horizontal sliders that happen to be tall (e.g. 50% width columns)
|
|
462
|
+
/*
|
|
463
|
+
const isSlider =
|
|
464
|
+
node.type === "container:slider" ||
|
|
465
|
+
node.type === "component:slider" ||
|
|
466
|
+
(node.tags &&
|
|
467
|
+
Array.isArray(node.tags) &&
|
|
468
|
+
node.tags.some((t: string) => t.toLowerCase().includes("slider")));
|
|
469
|
+
|
|
470
|
+
if (isSlider) {
|
|
471
|
+
const width = node.style?.width || 0;
|
|
472
|
+
const height = node.style?.height || 0;
|
|
473
|
+
// Check aspect ratio. If height > width, it's likely vertical.
|
|
474
|
+
// Note: width/height might be percentages or pixels.
|
|
475
|
+
// If both are numbers, we can compare.
|
|
476
|
+
// From logs: width: 52, height: 85.27. This is clearly vertical shape.
|
|
477
|
+
if (height > width) {
|
|
478
|
+
if (!node.data) node.data = {};
|
|
479
|
+
if (!node.data.params) node.data.params = {};
|
|
480
|
+
|
|
481
|
+
// Only apply if animation is default or missing
|
|
482
|
+
if (
|
|
483
|
+
!node.data.params.animation ||
|
|
484
|
+
node.data.params.animation === "default" ||
|
|
485
|
+
node.data.params.animation === "horizontal"
|
|
486
|
+
) {
|
|
487
|
+
console.log(
|
|
488
|
+
`[PATCH] Forcing VERTICAL animation for slider ${node.id} (W:${width}, H:${height})`
|
|
489
|
+
);
|
|
490
|
+
node.data.params.animation = "vertical";
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
*/
|
|
495
|
+
|
|
496
|
+
// Recurse
|
|
497
|
+
if (node.children) {
|
|
498
|
+
if (Array.isArray(node.children)) {
|
|
499
|
+
node.children.forEach(patchPageData);
|
|
500
|
+
} else {
|
|
501
|
+
patchPageData(node.children);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
if (clientData) {
|
|
507
|
+
// Clone to avoid mutating SWR cache directly if possible, though deep clone might be expensive.
|
|
508
|
+
// For now, patching in place as it's a fix.
|
|
509
|
+
patchPageData(clientData);
|
|
510
|
+
}
|
|
511
|
+
// --- GENERIC DATA PATCHING END ---
|
|
512
|
+
|
|
513
|
+
return {
|
|
514
|
+
nodeData: undefined,
|
|
515
|
+
// Allow overriding specific values by element id.
|
|
516
|
+
// For now, this is used to override TextComponent content when the node has the PROP:TEXT_VAR tag.
|
|
517
|
+
textOverridesById: data,
|
|
518
|
+
// Support for encore:data:array tags - provide array data by component ID
|
|
519
|
+
arrayDataById: data,
|
|
520
|
+
// Support for standalone component data binding (encore:data tags at root level)
|
|
521
|
+
rootData: data,
|
|
522
|
+
};
|
|
523
|
+
}, [pageData.data?.clientData, data]);
|
|
524
|
+
|
|
525
|
+
// Manage repeating container controls
|
|
526
|
+
const [containerControls, setContainerControls] = useState<
|
|
527
|
+
Map<string, RepeatingContainerControl>
|
|
528
|
+
>(new Map());
|
|
529
|
+
const [controlPropsMap, setControlPropsMap] = useState<
|
|
530
|
+
Map<
|
|
531
|
+
string,
|
|
532
|
+
{ currentIndex?: number; onIndexChange?: (index: number) => void }
|
|
533
|
+
>
|
|
534
|
+
>(new Map());
|
|
535
|
+
|
|
536
|
+
// Update control props from prop
|
|
537
|
+
useEffect(() => {
|
|
538
|
+
if (repeatingContainerControls) {
|
|
539
|
+
setControlPropsMap((prev) => {
|
|
540
|
+
// Check if content actually changed to avoid unnecessary updates
|
|
541
|
+
let changed = false;
|
|
542
|
+
if (prev.size !== Object.keys(repeatingContainerControls).length) {
|
|
543
|
+
changed = true;
|
|
544
|
+
} else {
|
|
545
|
+
for (const [id, props] of Object.entries(
|
|
546
|
+
repeatingContainerControls
|
|
547
|
+
)) {
|
|
548
|
+
const prevProps = prev.get(id);
|
|
549
|
+
if (!prevProps) {
|
|
550
|
+
changed = true;
|
|
551
|
+
break;
|
|
552
|
+
}
|
|
553
|
+
if (
|
|
554
|
+
prevProps.currentIndex !== props.currentIndex ||
|
|
555
|
+
prevProps.onIndexChange !== props.onIndexChange
|
|
556
|
+
) {
|
|
557
|
+
changed = true;
|
|
558
|
+
break;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
if (!changed) return prev;
|
|
564
|
+
|
|
565
|
+
const newMap = new Map();
|
|
566
|
+
Object.entries(repeatingContainerControls).forEach(([id, props]) => {
|
|
567
|
+
newMap.set(id, props);
|
|
568
|
+
});
|
|
569
|
+
return newMap;
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
}, [repeatingContainerControls]);
|
|
573
|
+
|
|
574
|
+
// Sync input groups from props to store
|
|
575
|
+
useEffect(() => {
|
|
576
|
+
if (inputGroups) {
|
|
577
|
+
const setInputGroupValue = useEncoreState.getState().setInputGroupValue;
|
|
578
|
+
Object.entries(inputGroups).forEach(([groupName, elementName]) => {
|
|
579
|
+
setInputGroupValue(groupName, elementName);
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
}, [inputGroups]);
|
|
583
|
+
|
|
584
|
+
const registerContainer = useCallback(
|
|
585
|
+
(id: string, control: RepeatingContainerControl) => {
|
|
586
|
+
setContainerControls((prev) => {
|
|
587
|
+
const next = new Map(prev);
|
|
588
|
+
next.set(id, control);
|
|
589
|
+
return next;
|
|
590
|
+
});
|
|
591
|
+
},
|
|
592
|
+
[]
|
|
593
|
+
);
|
|
594
|
+
|
|
595
|
+
const unregisterContainer = useCallback((id: string) => {
|
|
596
|
+
setContainerControls((prev) => {
|
|
597
|
+
const next = new Map(prev);
|
|
598
|
+
next.delete(id);
|
|
599
|
+
return next;
|
|
600
|
+
});
|
|
601
|
+
// Do NOT delete from controlPropsMap here.
|
|
602
|
+
// controlPropsMap contains props passed from the parent (repeatingContainerControls).
|
|
603
|
+
// If we delete them, we lose the configuration passed down to us.
|
|
604
|
+
// The props should persist even if the component temporarily unregisters.
|
|
605
|
+
}, []);
|
|
606
|
+
|
|
607
|
+
const getControl = useCallback(
|
|
608
|
+
(id: string) => {
|
|
609
|
+
return containerControls.get(id);
|
|
610
|
+
},
|
|
611
|
+
[containerControls]
|
|
612
|
+
);
|
|
613
|
+
|
|
614
|
+
const setControlProps = useCallback(
|
|
615
|
+
(
|
|
616
|
+
id: string,
|
|
617
|
+
props:
|
|
618
|
+
| { currentIndex?: number; onIndexChange?: (index: number) => void }
|
|
619
|
+
| ((prev: {
|
|
620
|
+
currentIndex?: number;
|
|
621
|
+
onIndexChange?: (index: number) => void;
|
|
622
|
+
}) => {
|
|
623
|
+
currentIndex?: number;
|
|
624
|
+
onIndexChange?: (index: number) => void;
|
|
625
|
+
})
|
|
626
|
+
) => {
|
|
627
|
+
setControlPropsMap((prev) => {
|
|
628
|
+
const next = new Map(prev);
|
|
629
|
+
const current = next.get(id) || {};
|
|
630
|
+
const newProps = typeof props === "function" ? props(current) : props;
|
|
631
|
+
next.set(id, newProps);
|
|
632
|
+
return next;
|
|
633
|
+
});
|
|
634
|
+
},
|
|
635
|
+
[]
|
|
636
|
+
);
|
|
637
|
+
|
|
638
|
+
const getControlProps = useCallback(
|
|
639
|
+
(id: string) => {
|
|
640
|
+
return controlPropsMap.get(id);
|
|
641
|
+
},
|
|
642
|
+
[controlPropsMap]
|
|
643
|
+
);
|
|
644
|
+
|
|
645
|
+
// Control props are automatically passed to registered containers via context
|
|
646
|
+
// Components read props via getControlProps() and update when props change
|
|
647
|
+
|
|
648
|
+
// Create context value - this object changes when controlPropsMap changes,
|
|
649
|
+
// causing all consumers to re-render and get updated props
|
|
650
|
+
const repeatingContainerContextValue = React.useMemo(
|
|
651
|
+
() => ({
|
|
652
|
+
registerContainer,
|
|
653
|
+
unregisterContainer,
|
|
654
|
+
getControl,
|
|
655
|
+
setControlProps,
|
|
656
|
+
getControlProps,
|
|
657
|
+
// Include controlPropsMap size in the value to trigger re-renders when it changes
|
|
658
|
+
_propsVersion: controlPropsMap.size,
|
|
659
|
+
}),
|
|
660
|
+
[
|
|
661
|
+
registerContainer,
|
|
662
|
+
unregisterContainer,
|
|
663
|
+
getControl,
|
|
664
|
+
setControlProps,
|
|
665
|
+
getControlProps,
|
|
666
|
+
controlPropsMap.size,
|
|
667
|
+
]
|
|
668
|
+
);
|
|
669
|
+
|
|
670
|
+
// Observe size changes of the dynamic content container and notify consumer
|
|
671
|
+
useEffect(() => {
|
|
672
|
+
if (!onSizeChange) return;
|
|
673
|
+
const element = containerRef.current;
|
|
674
|
+
if (!element) return;
|
|
675
|
+
const notify = (entry: ResizeObserverEntry) => {
|
|
676
|
+
const cr = entry.contentRect;
|
|
677
|
+
onSizeChange({ width: cr.width, height: cr.height });
|
|
678
|
+
};
|
|
679
|
+
const observer = new ResizeObserver((entries) => {
|
|
680
|
+
for (const entry of entries) {
|
|
681
|
+
notify(entry);
|
|
682
|
+
}
|
|
683
|
+
});
|
|
684
|
+
// Emit initial size as soon as possible
|
|
685
|
+
const rect = element.getBoundingClientRect();
|
|
686
|
+
onSizeChange({ width: rect.width, height: rect.height });
|
|
687
|
+
observer.observe(element);
|
|
688
|
+
return () => {
|
|
689
|
+
observer.disconnect();
|
|
690
|
+
};
|
|
691
|
+
}, [onSizeChange]);
|
|
692
|
+
|
|
693
|
+
// Per-instance source override
|
|
694
|
+
useEffect(() => {
|
|
695
|
+
if (!source) return;
|
|
696
|
+
setLocalModeOverride(source === "local" ? "local" : "remote");
|
|
697
|
+
return () => {
|
|
698
|
+
// Clear override when this instance unmounts
|
|
699
|
+
setLocalModeOverride(null);
|
|
700
|
+
};
|
|
701
|
+
}, [source]);
|
|
702
|
+
|
|
703
|
+
if (!pageId) {
|
|
704
|
+
return (
|
|
705
|
+
<div style={{ padding: "30px" }}>
|
|
706
|
+
<div style={{ overflowY: "auto" }}>
|
|
707
|
+
{(isLocalMode()
|
|
708
|
+
? // Local mode: app.json provides pages under app.data.pages
|
|
709
|
+
((app?.data as any)?.app?.data?.pages || []).map(
|
|
710
|
+
(pg: any) => pg?.id
|
|
711
|
+
)
|
|
712
|
+
: app?.data?.app.pageIds || []
|
|
713
|
+
).map((p: string) => (
|
|
714
|
+
<Link
|
|
715
|
+
key={p}
|
|
716
|
+
to={`/apps/${appId}/pages/${p}?noRedirect=${noRedirect}`}
|
|
717
|
+
style={{
|
|
718
|
+
fontSize: 20,
|
|
719
|
+
display: "block",
|
|
720
|
+
marginBottom: "10px",
|
|
721
|
+
}}
|
|
722
|
+
>
|
|
723
|
+
{p}
|
|
724
|
+
</Link>
|
|
725
|
+
))}
|
|
726
|
+
</div>
|
|
727
|
+
</div>
|
|
728
|
+
);
|
|
729
|
+
}
|
|
730
|
+
return (
|
|
731
|
+
<div
|
|
732
|
+
ref={containerRef}
|
|
733
|
+
style={{ width: "100%", height: "100%", position: "relative" }}
|
|
734
|
+
>
|
|
735
|
+
<Suspense fallback={fallback || <div />}>
|
|
736
|
+
<EncoreComponentIdContext.Provider value={{ componentId }}>
|
|
737
|
+
<EncoreActionContext.Provider value={{ onAction }}>
|
|
738
|
+
<EncoreRepeatingContainerContext.Provider
|
|
739
|
+
value={repeatingContainerContextValue}
|
|
740
|
+
>
|
|
741
|
+
<EncoreBindingContext.Provider value={context}>
|
|
742
|
+
<DynamicComponent
|
|
743
|
+
name={`${appId}/draft/components/${pageId}`}
|
|
744
|
+
fallback={fallback}
|
|
745
|
+
reloadKey={reloadKey}
|
|
746
|
+
componentCode={componentCode}
|
|
747
|
+
>
|
|
748
|
+
{" "}
|
|
749
|
+
</DynamicComponent>
|
|
750
|
+
</EncoreBindingContext.Provider>
|
|
751
|
+
</EncoreRepeatingContainerContext.Provider>
|
|
752
|
+
</EncoreActionContext.Provider>
|
|
753
|
+
</EncoreComponentIdContext.Provider>
|
|
754
|
+
</Suspense>
|
|
755
|
+
</div>
|
|
756
|
+
);
|
|
757
|
+
};
|
|
758
|
+
|
|
759
|
+
export default EncoreApp;
|