mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-11 17:26:32 +02:00
61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
"use client";
|
|
|
|
import { create } from "zustand";
|
|
|
|
interface AiInsight {
|
|
codein: string;
|
|
insight: string;
|
|
status: "idle" | "loading" | "done" | "error";
|
|
}
|
|
|
|
interface AiCopilotState {
|
|
insights: Record<string, AiInsight>;
|
|
analyzeProduct: (payload: {
|
|
codein: string;
|
|
libelle1: string;
|
|
totalCa: number;
|
|
tauxMarge: number;
|
|
totalQuantite: number;
|
|
sales12m: Record<string, number>;
|
|
codeGamme: string | null;
|
|
}) => Promise<void>;
|
|
}
|
|
|
|
export const useAiCopilotStore = create<AiCopilotState>((set) => ({
|
|
insights: {},
|
|
|
|
analyzeProduct: async (payload) => {
|
|
const { codein } = payload;
|
|
|
|
// 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),
|
|
});
|
|
|
|
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" },
|
|
},
|
|
}));
|
|
}
|
|
},
|
|
}));
|