feat: fetch and auto-register personas from server pricing items
Some checks are pending
CI/CD / test-and-build (push) Waiting to run
CI/CD / deploy (push) Blocked by required conditions

This commit is contained in:
Najjar\NajjarV02 2026-04-17 15:58:00 +04:00
parent 26770e070c
commit 13050a6541

View File

@ -134,6 +134,53 @@ export const personaStore = createStore<PersonaStore>((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
}
},
}));