import { useEffect, useState } from "react"; import { Brain, RefreshCw, Save } from "lucide-react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { useAppStore } from "@/stores/app-store"; import { useConnections } from "@/hooks/use-connections"; import { useMemory, useSaveMemory } from "@/hooks/use-memory"; export function MemoryPanel() { const activeConnectionId = useAppStore((s) => s.activeConnectionId); const { data: connections } = useConnections(); const activeConn = connections?.find((c) => c.id === activeConnectionId); const { data: serverContent, isFetching, refetch } = useMemory(activeConnectionId); const saveMutation = useSaveMemory(); const [draft, setDraft] = useState(""); const [dirty, setDirty] = useState(false); // Sync local textarea when the active connection changes or server reloads. // Only overwrite if the user hasn't edited. useEffect(() => { if (!dirty) { setDraft(serverContent ?? ""); } }, [serverContent, activeConnectionId, dirty]); if (!activeConnectionId) { return (

Connect to a database to view its memory.

); } const noteCount = (draft.match(/^## /gm) ?? []).length; const isEmpty = draft.trim().length === 0; const handleSave = () => { saveMutation.mutate( { connectionId: activeConnectionId, content: draft }, { onSuccess: () => { setDirty(false); toast.success("Memory saved"); }, onError: (err) => toast.error("Save failed", { description: String(err) }), } ); }; const handleReload = () => { setDirty(false); refetch(); }; return (
Memory {activeConn && ( · {activeConn.name} )} {!isEmpty && ( {noteCount} note{noteCount === 1 ? "" : "s"} )}
{isEmpty ? (

No notes yet for this connection.

The agent will populate this as it learns about your database. You can also edit notes here directly — anything you type is loaded into the agent's context on its next turn.