From 13050a6541898f57db671b91ddd7d0bf49fd3dcd Mon Sep 17 00:00:00 2001 From: "Najjar\\NajjarV02" Date: Fri, 17 Apr 2026 15:58:00 +0400 Subject: [PATCH] feat: fetch and auto-register personas from server pricing items --- src/store/usePersonaStore.ts | 47 ++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/store/usePersonaStore.ts b/src/store/usePersonaStore.ts index d728707..db84e4b 100644 --- a/src/store/usePersonaStore.ts +++ b/src/store/usePersonaStore.ts @@ -134,6 +134,53 @@ export const personaStore = createStore((set, get) => ({ } else { set({ personas: [...DEFAULT_PERSONAS], isHydrated: true }); } + + // Fetch pricing items from server DB and auto-register personas for items with a modelPath + if (typeof window !== 'undefined') { + fetch('/api/admin/pricing/') + .then((r) => r.json()) + .then((data) => { + const serverItems: { id: string; label: string; modelPath: string | null }[] = data.items ?? []; + const current = get().personas; + const currentIds = new Set(current.map((p) => p.id)); + const newPersonas: PersonaOption[] = []; + + serverItems.forEach(({ id, label, modelPath }) => { + if (!modelPath || id === 'base') return; + if (currentIds.has(id)) { + // Update modelPath if it changed + const existing = current.find((p) => p.id === id); + if (existing && existing.modelPath !== modelPath) { + set((state) => ({ + personas: state.personas.map((p) => + p.id === id ? { ...p, modelPath } : p + ), + })); + } + } else { + newPersonas.push({ + id, + label, + description: label, + colors: { torso: '#3b82f6', legs: '#3b82f6' }, + modelPath, + }); + } + }); + + if (newPersonas.length > 0) { + set((state) => { + const updated = [...state.personas, ...newPersonas]; + saveToStorage(updated); + return { personas: updated }; + }); + } else { + // Save current state to localStorage so it persists + saveToStorage(get().personas); + } + }) + .catch(() => {}); // silent — use local data as fallback + } }, }));