import { create } from 'zustand'; import Resync from 'resync-javascript'; import type { ContentView, FunctionRegistry, NavigationRegistry, LogAnalytics, } from '../types'; // Per-instance content state export interface ContentViewItem { content: ContentView | null; loading: boolean; error: any; functionRegistry?: FunctionRegistry; navigationRegistry?: NavigationRegistry; logAnalytics?: LogAnalytics; } export interface ContentViewState { contentViews: Record; loadContentView: ( contentViewName: string, customProps?: { functionRegistry?: FunctionRegistry; navigationRegistry?: NavigationRegistry; logAnalytics?: LogAnalytics; isCampaign?: boolean; } ) => Promise; } const initialContentViewState: ContentViewState = { contentViews: {}, loadContentView: async () => Promise.resolve(), }; export const contentModel = create((set, get) => ({ ...initialContentViewState, loadContentView: async (contentViewName, customProps) => { // check if content view is already loaded if (get().contentViews[contentViewName]) { return; } try { // Use the Resync singleton directly const allContent = await Resync.getContent(); if (allContent) { const foundContent = (allContent || [])?.find( (item) => item.name === contentViewName ); if (foundContent) { set({ contentViews: { ...get().contentViews, [contentViewName]: { content: foundContent as ContentView, loading: false, error: null, functionRegistry: customProps?.functionRegistry, navigationRegistry: customProps?.navigationRegistry, logAnalytics: customProps?.logAnalytics, // isCampaign: customProps?.isCampaign || false, }, }, }); } else { throw new Error(`Content view "${contentViewName}" not found`); } } } catch (error) { console.error(error); } }, }));