From 8767b1cf1e093c9c4e0111ab089d19a2cfba12fb Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 22 Feb 2026 13:12:34 +0100 Subject: [PATCH] feat: add AI Copilot Zustand store for managing product insights and analysis. --- .../ai-copilot/store/use-ai-copilot-store.ts | 84 ++++++++++--------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/src/features/ai-copilot/store/use-ai-copilot-store.ts b/src/features/ai-copilot/store/use-ai-copilot-store.ts index eab3ce9..cbb8091 100644 --- a/src/features/ai-copilot/store/use-ai-copilot-store.ts +++ b/src/features/ai-copilot/store/use-ai-copilot-store.ts @@ -1,6 +1,7 @@ "use client"; import { create } from "zustand"; +import { persist } from "zustand/middleware"; interface AiInsight { codein: string; @@ -24,49 +25,56 @@ interface AiCopilotState { }) => Promise; } -export const useAiCopilotStore = create((set) => ({ - insights: {}, +export const useAiCopilotStore = create()( + persist( + (set, get) => ({ + insights: {}, - setInsight: (codein, insight, isDuplicate = false) => { - set((s) => ({ - insights: { - ...s.insights, - [codein]: { codein, insight, status: "done", isDuplicate }, + setInsight: (codein, insight, isDuplicate = false) => { + set((s) => ({ + insights: { + ...s.insights, + [codein]: { codein, insight, status: "done", isDuplicate }, + }, + })); }, - })); - }, - analyzeProduct: async (payload) => { - const { codein, score } = payload; + analyzeProduct: async (payload) => { + const { codein } = payload; - // Mark as loading - set((s) => ({ - insights: { ...s.insights, [codein]: { codein, insight: "", status: "loading" } }, - })); + // Mark as loading + set((s) => ({ + insights: { ...s.insights, [codein]: { codein, insight: "", status: "loading" } }, + })); - try { - const res = await fetch("/api/ai/analyze", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(payload), - }); + try { + const res = await fetch("/api/ai/analyze", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(payload), + }); - if (!res.ok) throw new Error(`HTTP ${res.status}`); - const data = await res.json(); + if (!res.ok) throw new Error(`HTTP ${res.status}`); + const data = await res.json(); - set((s) => ({ - insights: { - ...s.insights, - [codein]: { codein, insight: data.insight, status: "done" }, - }, - })); - } catch { - set((s) => ({ - insights: { - ...s.insights, - [codein]: { codein, insight: "Erreur lors de l'analyse.", status: "error" }, - }, - })); + set((s) => ({ + insights: { + ...s.insights, + [codein]: { codein, insight: data.insight, status: "done" }, + }, + })); + } catch { + set((s) => ({ + insights: { + ...s.insights, + [codein]: { codein, insight: "Erreur lors de l'analyse.", status: "error" }, + }, + })); + } + }, + }), + { + name: "collectflow-ai-storage", } - }, -})); + ) +);