feat: add AI Copilot Zustand store for managing product insights and analysis.

This commit is contained in:
Michael committed 2026-02-22 13:12:34 +01:00
1 parent c23f74d32d
commit 8767b1cf1e
1 file changed
+46 -38
@@ -1,6 +1,7 @@
"use client"; "use client";
import { create } from "zustand"; import { create } from "zustand";
import { persist } from "zustand/middleware";
interface AiInsight { interface AiInsight {
codein: string; codein: string;
@@ -24,49 +25,56 @@ interface AiCopilotState {
}) => Promise<void>; }) => Promise<void>;
} }
export const useAiCopilotStore = create<AiCopilotState>((set) => ({ export const useAiCopilotStore = create<AiCopilotState>()(
insights: {}, persist(
(set, get) => ({
insights: {},
setInsight: (codein, insight, isDuplicate = false) => { setInsight: (codein, insight, isDuplicate = false) => {
set((s) => ({ set((s) => ({
insights: { insights: {
...s.insights, ...s.insights,
[codein]: { codein, insight, status: "done", isDuplicate }, [codein]: { codein, insight, status: "done", isDuplicate },
},
}));
}, },
}));
},
analyzeProduct: async (payload) => { analyzeProduct: async (payload) => {
const { codein, score } = payload; const { codein } = payload;
// Mark as loading // Mark as loading
set((s) => ({ set((s) => ({
insights: { ...s.insights, [codein]: { codein, insight: "", status: "loading" } }, insights: { ...s.insights, [codein]: { codein, insight: "", status: "loading" } },
})); }));
try { try {
const res = await fetch("/api/ai/analyze", { const res = await fetch("/api/ai/analyze", {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload), body: JSON.stringify(payload),
}); });
if (!res.ok) throw new Error(`HTTP ${res.status}`); if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json(); const data = await res.json();
set((s) => ({ set((s) => ({
insights: { insights: {
...s.insights, ...s.insights,
[codein]: { codein, insight: data.insight, status: "done" }, [codein]: { codein, insight: data.insight, status: "done" },
}, },
})); }));
} catch { } catch {
set((s) => ({ set((s) => ({
insights: { insights: {
...s.insights, ...s.insights,
[codein]: { codein, insight: "Erreur lors de l'analyse.", status: "error" }, [codein]: { codein, insight: "Erreur lors de l'analyse.", status: "error" },
}, },
})); }));
}
},
}),
{
name: "collectflow-ai-storage",
} }
}, )
})); );