"use client"; import { useState } from "react"; import { Bookmark, Check } from "lucide-react"; import { useToast } from "@/components/ui"; import { defaultAnalysisName, saveAnalysis, type AnalysisType, type SaveAnalysisPayload } from "@/lib/analyses"; // Einheitlicher Speichern-Knopf für die Analysewerkzeuge. `build` wird erst beim Klick // aufgerufen -- so wird das (evtl. grosse) Ergebnis nicht bei jedem Render zusammengebaut, // sondern nur, wenn wirklich gespeichert wird. export function SaveAnalysisButton({ planId, type, scenarioName, versionLabel, disabled, build, }: { planId: string; type: AnalysisType; scenarioName: string | null; versionLabel: string | null; disabled?: boolean; build: () => Omit | null; }) { const [open, setOpen] = useState(false); const [name, setName] = useState(""); const [busy, setBusy] = useState(false); const [done, setDone] = useState(false); const toast = useToast(); function start() { setName(defaultAnalysisName(type, scenarioName, versionLabel)); setOpen(true); } async function save() { const payload = build(); if (!payload) { toast("error", "Es gibt noch kein Ergebnis zum Speichern."); return; } setBusy(true); try { await saveAnalysis(planId, { ...payload, name: name.trim() || defaultAnalysisName(type, scenarioName, versionLabel), type }); setOpen(false); setDone(true); toast("success", "Analyse gespeichert."); setTimeout(() => setDone(false), 2000); } catch (e) { toast("error", e instanceof Error ? e.message : "Speichern fehlgeschlagen."); } finally { setBusy(false); } } if (open) { return (
setName(e.target.value)} onKeyDown={(e) => e.key === "Enter" && save()} className="min-w-0 flex-1 rounded border border-border bg-surface px-2 py-1 text-sm text-fg" />
); } return ( ); }