import { StoredSpace } from "@repo/db/schema"; import { useEffect, useMemo, useState } from "react"; import { createMemory, createSpace } from "../actions/doers"; import ComboboxWithCreate from "@repo/ui/shadcn/combobox"; import { toast } from "sonner"; import { getSpaces } from "../actions/fetchers"; import { MinusIcon, PlusCircleIcon } from "lucide-react"; import { DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@repo/ui/shadcn/dialog"; import { Label } from "@repo/ui/shadcn/label"; import { Textarea } from "@repo/ui/shadcn/textarea"; import { Button } from "@repo/ui/shadcn/button"; export function DialogContentContainer({ DialogClose, }: { DialogClose: () => void; }) { const [spaces, setSpaces] = useState([]); const [content, setContent] = useState(""); const [selectedSpaces, setSelectedSpaces] = useState([]); const options = useMemo( () => spaces.map((x) => ({ label: x.name, value: x.id.toString(), })), [spaces], ); const autoDetectedType = useMemo(() => { if (content.length === 0) { return "none"; } if ( content.match(/https?:\/\/(x\.com|twitter\.com)\/[\w]+\/[\w]+\/[\d]+/) ) { return "tweet"; } else if (content.match(/https?:\/\/[\w\.]+/)) { return "page"; } else if (content.match(/https?:\/\/www\.[\w\.]+/)) { return "page"; } else { return "note"; } }, [content]); const handleSubmit = async (content?: string, spaces?: number[]) => { DialogClose(); toast.info("Creating memory...", { icon: , duration: 7500, }); if (!content || content.length === 0) { toast.error("Content is required"); return; } console.log(spaces); const cont = await createMemory({ content: content, spaces: spaces ?? undefined, }); setContent(""); setSelectedSpaces([]); if (cont.success) { toast.success("Memory queued", { richColors: true, }); } else { toast.error(`Memory creation failed: ${cont.error}`); } }; useEffect(() => { (async () => { let spaces = await getSpaces(); if (!spaces.success || !spaces.data) { toast.warning("Unable to get spaces", { richColors: true, }); setSpaces([]); return; } setSpaces(spaces.data); })(); }, []); return (
{ const content = e.get("content")?.toString(); await handleSubmit(content, selectedSpaces); }} className="flex flex-col gap-4 " > Add memory A "Memory" is a bookmark, something you want to remember.