'use client' import { Note } from '@/lib/types' import { NoteCard } from './note-card' import { useState } from 'react' import { NoteEditor } from './note-editor' import { reorderNotes } from '@/app/actions/notes' interface NoteGridProps { notes: Note[] } export function NoteGrid({ notes }: NoteGridProps) { const [editingNote, setEditingNote] = useState(null) const [draggedNote, setDraggedNote] = useState(null) const [dragOverNote, setDragOverNote] = useState(null) const pinnedNotes = notes.filter(note => note.isPinned).sort((a, b) => a.order - b.order) const unpinnedNotes = notes.filter(note => !note.isPinned).sort((a, b) => a.order - b.order) const handleDragStart = (note: Note) => { setDraggedNote(note) } const handleDragEnd = async () => { if (draggedNote && dragOverNote && draggedNote.id !== dragOverNote.id) { // Reorder notes const sourceIndex = notes.findIndex(n => n.id === draggedNote.id) const targetIndex = notes.findIndex(n => n.id === dragOverNote.id) await reorderNotes(draggedNote.id, dragOverNote.id) } setDraggedNote(null) setDragOverNote(null) } const handleDragOver = (note: Note) => { if (draggedNote && draggedNote.id !== note.id) { setDragOverNote(note) } } return ( <>
{pinnedNotes.length > 0 && (

Pinned

{pinnedNotes.map(note => (
))}
)} {unpinnedNotes.length > 0 && (
{pinnedNotes.length > 0 && (

Others

)}
{unpinnedNotes.map(note => (
))}
)} {notes.length === 0 && (

No notes yet

Create your first note to get started

)}
{editingNote && ( setEditingNote(null)} /> )} ) }