diff --git a/src/app/(dashboard)/dashboard/memory/components/EditMemoryModal.tsx b/src/app/(dashboard)/dashboard/memory/components/EditMemoryModal.tsx new file mode 100644 index 0000000000..e6111e8b44 --- /dev/null +++ b/src/app/(dashboard)/dashboard/memory/components/EditMemoryModal.tsx @@ -0,0 +1,161 @@ +"use client"; + +import { useState, useEffect } from "react"; +import { Modal, Button, Input, Select } from "@/shared/components"; +import { useTranslations } from "next-intl"; + +interface Memory { + id: string; + type: "factual" | "episodic" | "procedural" | "semantic"; + key: string; + content: string; + metadata: Record; +} + +interface Props { + memory: Memory | null; + isOpen: boolean; + onClose: () => void; + onSaved: () => void; +} + +export default function EditMemoryModal({ memory, isOpen, onClose, onSaved }: Props) { + const t = useTranslations("memory"); + const [type, setType] = useState<"factual" | "episodic" | "procedural" | "semantic">("factual"); + const [key, setKey] = useState(""); + const [content, setContent] = useState(""); + const [metadataStr, setMetadataStr] = useState("{}"); + const [metadataError, setMetadataError] = useState(""); + const [isSaving, setIsSaving] = useState(false); + const [error, setError] = useState(""); + + useEffect(() => { + if (memory && isOpen) { + setType(memory.type); + setKey(memory.key); + setContent(memory.content); + setMetadataStr(JSON.stringify(memory.metadata ?? {}, null, 2)); + setMetadataError(""); + setError(""); + } + }, [memory, isOpen]); + + const handleMetadataChange = (value: string) => { + setMetadataStr(value); + try { + JSON.parse(value); + setMetadataError(""); + } catch { + setMetadataError(t("editModal.metadataInvalid")); + } + }; + + const handleSave = async () => { + if (!memory) return; + if (metadataError) return; + setIsSaving(true); + setError(""); + try { + let metadata: Record = {}; + try { + metadata = JSON.parse(metadataStr); + } catch { + setError(t("editModal.metadataInvalid")); + return; + } + const res = await fetch(`/api/memory/${memory.id}`, { + method: "PUT", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ type, key, content, metadata }), + }); + if (res.ok) { + onSaved(); + onClose(); + } else { + const data = await res.json().catch(() => null); + setError(data?.error?.message ?? t("editModal.saveFailed")); + } + } catch { + setError(t("editModal.saveFailed")); + } finally { + setIsSaving(false); + } + }; + + return ( + + + + + } + > +
+ {error && ( +
+ {error} +
+ )} +
+ + +
+
+ + setKey(e.target.value)} + placeholder={t("keyPlaceholder")} + className="w-full" + /> +
+
+ +