import { type Note, useDeleteNote, useUpdateNote } from "@/hooks/use-notes"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Pin, Archive, Trash2, Edit, History } from "lucide-react"; import { format } from "date-fns"; import { toast } from "sonner"; import { useState } from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { NoteForm } from "./note-form"; import ReactMarkdown from "react-markdown"; import { getNoteColor } from "@/lib/constants"; import clsx from "clsx"; import { VersionHistoryDialog } from "./version-history-dialog"; import { NoteViewDialog } from "./note-view-dialog"; interface NoteCardProps { note: Note; } export function NoteCard({ note }: NoteCardProps) { const { mutate: deleteNote } = useDeleteNote(); const { mutate: updateNote } = useUpdateNote(); const [editing, setEditing] = useState(false); const [historyOpen, setHistoryOpen] = useState(false); const [viewOpen, setViewOpen] = useState(false); // Archive toggle const toggleArchive = (e: React.MouseEvent) => { e.stopPropagation(); updateNote({ id: note.id, is_archived: !note.is_archived }); }; // Pin toggle const togglePin = (e: React.MouseEvent) => { e.stopPropagation(); updateNote({ id: note.id, is_pinned: !note.is_pinned }); }; const handleDelete = (e: React.MouseEvent) => { e.stopPropagation(); if (confirm("Are you sure?")) { deleteNote(note.id); } } const handleEdit = (data: any) => { const tags = data.tags ? data.tags.split(",").map((t: string) => t.trim()).filter(Boolean) : []; updateNote({ id: note.id, ...data, tags, }, { onSuccess: () => { setEditing(false); toast.success("Note updated"); } }); } const colorClass = getNoteColor(note.color); return ( <> setViewOpen(true)} >
{note.title} {note.is_pinned && }
{format(new Date(note.created_at), "MMM d, yyyy")}
{note.content}
{note.tags.map(tag => ( {tag.name} ))}
Edit Note t.name).join(", "), }} onSubmit={handleEdit} submitLabel="Update" /> setEditing(true)} /> ); }