'use client' import { useState, useEffect, useRef, useCallback, memo } from 'react'; import { Note } from '@/lib/types'; import { NoteCard } from './note-card'; import { NoteEditor } from './note-editor'; import { updateFullOrder } from '@/app/actions/notes'; import { useResizeObserver } from '@/hooks/use-resize-observer'; interface MasonryGridProps { notes: Note[]; } interface MasonryItemProps { note: Note; onEdit: (note: Note, readOnly?: boolean) => void; onResize: () => void; } function getSizeClasses(size: string = 'small') { switch (size) { case 'medium': return 'w-full sm:w-full lg:w-2/3 xl:w-2/4 2xl:w-2/5'; case 'large': return 'w-full'; case 'small': default: return 'w-full sm:w-1/2 lg:w-1/3 xl:w-1/4 2xl:w-1/5'; } } const MasonryItem = memo(function MasonryItem({ note, onEdit, onResize }: MasonryItemProps) { const resizeRef = useResizeObserver(() => { onResize(); }); const sizeClasses = getSizeClasses(note.size); return (
); }, (prev, next) => { // Custom comparison to avoid re-render on function prop changes if note data is same return prev.note === next.note; }); export function MasonryGrid({ notes }: MasonryGridProps) { const [editingNote, setEditingNote] = useState<{ note: Note; readOnly?: boolean } | null>(null); const pinnedGridRef = useRef(null); const othersGridRef = useRef(null); const pinnedMuuri = useRef(null); const othersMuuri = useRef(null); const isDraggingRef = useRef(false); const pinnedNotes = notes.filter(n => n.isPinned).sort((a, b) => a.order - b.order); const othersNotes = notes.filter(n => !n.isPinned).sort((a, b) => a.order - b.order); const handleDragEnd = async (grid: any) => { if (!grid) return; // Prevent layout refresh during server update isDraggingRef.current = true; const items = grid.getItems(); const ids = items .map((item: any) => item.getElement()?.getAttribute('data-id')) .filter((id: any): id is string => !!id); try { await updateFullOrder(ids); } catch (error) { console.error('Failed to persist order:', error); } finally { // Reset after animation/server roundtrip setTimeout(() => { isDraggingRef.current = false; }, 1000); } }; const refreshLayout = useCallback(() => { // Use requestAnimationFrame for smoother updates requestAnimationFrame(() => { if (pinnedMuuri.current) { pinnedMuuri.current.refreshItems().layout(); } if (othersMuuri.current) { othersMuuri.current.refreshItems().layout(); } }); }, []); useEffect(() => { let isMounted = true; const initMuuri = async () => { // Import web-animations-js polyfill await import('web-animations-js'); // Dynamic import of Muuri to avoid SSR window error const MuuriClass = (await import('muuri')).default; if (!isMounted) return; // Detect if we are on a touch device (mobile behavior) const isMobile = window.matchMedia('(pointer: coarse)').matches; const layoutOptions = { dragEnabled: true, // On mobile, restrict drag to handle to allow scrolling. On desktop, allow drag from anywhere. dragHandle: isMobile ? '.drag-handle' : undefined, dragContainer: document.body, dragStartPredicate: { distance: 10, delay: 0, }, dragPlaceholder: { enabled: true, createElement: (item: any) => { const el = item.getElement().cloneNode(true); el.style.opacity = '0.5'; return el; }, }, dragAutoScroll: { targets: [window], speed: (item: any, target: any, intersection: any) => { return intersection * 20; }, }, }; if (pinnedGridRef.current && !pinnedMuuri.current && pinnedNotes.length > 0) { pinnedMuuri.current = new MuuriClass(pinnedGridRef.current, layoutOptions) .on('dragEnd', () => handleDragEnd(pinnedMuuri.current)); } if (othersGridRef.current && !othersMuuri.current && othersNotes.length > 0) { othersMuuri.current = new MuuriClass(othersGridRef.current, layoutOptions) .on('dragEnd', () => handleDragEnd(othersMuuri.current)); } }; initMuuri(); return () => { isMounted = false; pinnedMuuri.current?.destroy(); othersMuuri.current?.destroy(); pinnedMuuri.current = null; othersMuuri.current = null; }; }, [pinnedNotes.length > 0, othersNotes.length > 0]); // Synchronize items when notes change (e.g. searching, adding) useEffect(() => { if (isDraggingRef.current) return; if (pinnedMuuri.current) { pinnedMuuri.current.refreshItems().layout(); } if (othersMuuri.current) { othersMuuri.current.refreshItems().layout(); } }, [notes]); return (
{pinnedNotes.length > 0 && (

Pinned

{pinnedNotes.map(note => ( setEditingNote({ note, readOnly })} onResize={refreshLayout} /> ))}
)} {othersNotes.length > 0 && (
{pinnedNotes.length > 0 && (

Others

)}
{othersNotes.map(note => ( setEditingNote({ note, readOnly })} onResize={refreshLayout} /> ))}
)} {editingNote && ( setEditingNote(null)} /> )}
); }