"use client"; import { useState } from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, DialogPortal } from "@/components/ui/dialog"; import * as DialogPrimitive from "@radix-ui/react-dialog"; import { cn } from "@/lib/utils"; import { Button } from "@/components/ui/button"; import { Textarea } from "@/components/ui/textarea"; import { Input } from "@/components/ui/input"; import { Loader2, X } from "lucide-react"; import { useSession } from "next-auth/react"; import * as React from "react"; // Custom DialogOverlay to override the default black background const DialogOverlay = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )) DialogOverlay.displayName = DialogPrimitive.Overlay.displayName; interface NotesDialogProps { open: boolean; onOpenChange: (open: boolean) => void; } export function NotesDialog({ open, onOpenChange }: NotesDialogProps) { const { data: session } = useSession(); const [title, setTitle] = useState(""); const [content, setContent] = useState(""); const [isSaving, setIsSaving] = useState(false); const [error, setError] = useState(""); const handleSave = async () => { if (!title.trim()) { setError("Please enter a title for your note"); return; } if (!content.trim()) { setError("Please enter content for your note"); return; } try { setIsSaving(true); setError(""); // Construct API payload with lowercase folder name (always "notes" for quick notes) const payload = { id: `user-${session?.user?.id}/notes/${title}${title.endsWith('.md') ? '' : '.md'}`, title: title, content: content, folder: "notes", // Always save to Notes folder mime: "text/markdown" }; // Use direct storage API endpoint const response = await fetch('/api/storage/files', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); if (!response.ok) { throw new Error('Failed to save note'); } // Reset form and close dialog setTitle(""); setContent(""); onOpenChange(false); } catch (err) { console.error('Error saving note:', err); setError("Failed to save your note. Please try again."); } finally { setIsSaving(false); } }; return ( Quick Note
{error && (
{error}
)}
setTitle(e.target.value)} disabled={isSaving} />