"use client"; import { useState, useEffect } from "react"; import { useAuth } from "@/components/AuthProvider"; interface Note { id: number; title: string; content: string; color: string; is_pinned: boolean; created_at: string; } interface NotesListProps { userId: number | null; } export default function NotesList({ userId }: NotesListProps) { const [notes, setNotes] = useState([]); const [showAddForm, setShowAddForm] = useState(false); const [formData, setFormData] = useState({ title: "", content: "", color: "#fef08a", isPinned: false, }); const { authenticated, user } = useAuth(); const colors = [ "#fef08a", // yellow "#86efac", // green "#93c5fd", // blue "#fda4af", // pink "#c4b5fd", // purple "#fb923c", // orange ]; useEffect(() => { if (userId) { fetchNotes(); } }, [userId]); const fetchNotes = async () => { try { const res = await fetch("/api/notes"); const data = await res.json(); if (data.notes) { setNotes(data.notes); } } catch (error) { console.error("Failed to fetch notes:", error); } }; const handleAddNote = async () => { try { const res = await fetch("/api/notes", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(formData), }); const result = await res.json(); if (result.success) { setFormData({ title: "", content: "", color: "#fef08a", isPinned: false }); setShowAddForm(false); fetchNotes(); } } catch (error) { console.error("Failed to add note:", error); } }; const handleDeleteNote = async (noteId: number) => { try { const res = await fetch(`/api/notes?id=${noteId}`, { method: "DELETE" }); const result = await res.json(); if (result.success) { fetchNotes(); } } catch (error) { console.error("Failed to delete note:", error); } }; const handleTogglePin = async (noteId: number) => { try { const res = await fetch("/api/notes/toggle-pin", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ id: noteId }), }); const result = await res.json(); if (result.success) { fetchNotes(); } } catch (error) { console.error("Failed to toggle pin:", error); } }; if (!userId) { return (
请先登录以查看便签
); } return (

便签

便签

{authenticated && ( )}
{showAddForm && (
setFormData({ ...formData, title: e.target.value })} className="w-full px-4 py-2 bg-white dark:bg-zinc-900 border border-zinc-300 dark:border-zinc-700 rounded-lg focus:outline-none focus:border-violet-500" placeholder="便签标题" />