'use client' // Library index (the /pages "All pages" view from the library-sidebar mockup): // a filterable table of every non-folder page with its folder path and last // edit. Folders are content-less Page rows (isFolder) and are counted but not // listed as documents. Client-only after mount to keep relative dates // hydration-safe. import { useCallback, useEffect, useState } from 'react' import { useRouter } from 'next/navigation' import { BookOpen, FileText, Link2, MoreHorizontal, Pin, PinOff, Plus, Trash2 } from 'lucide-react' import { Button } from '@/components/ui/button' import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu' import { toast } from 'sonner' import { PAGES_CHANGED, type PageSummary } from '@/lib/pages' import { SafeDate } from '@/components/safe-format' import { useMounted } from '@/components/client-only' import { cn } from '@/lib/utils' type Filter = 'all' | 'recent' | 'pinned' | 'untagged' const FILTERS: { key: Filter; label: string }[] = [ { key: 'all', label: 'All' }, { key: 'recent', label: 'Recent' }, { key: 'pinned', label: 'Pinned' }, { key: 'untagged', label: 'Untagged' }, ] export function LibraryIndex() { const router = useRouter() const mounted = useMounted() const [pages, setPages] = useState([]) const [recentIds, setRecentIds] = useState>(new Set()) const [filter, setFilter] = useState('all') const load = useCallback(() => { fetch('/api/pages').then((r) => (r.ok ? r.json() : [])).then((d) => { const list: PageSummary[] = Array.isArray(d) ? d : [] setPages(list) // Recency is computed here (in the fetch callback), not during render, // so the render path stays pure and hydration-safe. const now = Date.now() setRecentIds(new Set(list.filter((p) => p.updatedAt && now - new Date(p.updatedAt).getTime() < 2 * 24 * 60 * 60 * 1000).map((p) => p.id))) }).catch(() => {}) }, []) useEffect(() => { load() window.addEventListener(PAGES_CHANGED, load) return () => window.removeEventListener(PAGES_CHANGED, load) }, [load]) const folders = pages.filter((p) => p.isFolder) const leaves = pages.filter((p) => !p.isFolder) const folderById = new Map(folders.map((f) => [f.id, f])) const folderPath = (p: PageSummary): string => { const parts: string[] = [] let cur = p.parentId const seen = new Set() while (cur && folderById.has(cur) && !seen.has(cur)) { seen.add(cur) parts.unshift(folderById.get(cur)!.title) cur = folderById.get(cur)!.parentId } return parts.join(' / ') } const rows = leaves.filter((p) => filter === 'all' ? true : filter === 'pinned' ? p.pinned : filter === 'untagged' ? !(p.tags && p.tags.length) : recentIds.has(p.id)) const createPage = async () => { const res = await fetch('/api/pages', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title: 'Untitled page' }) }) if (!res.ok) { toast.error('Could not create page'); return } const p = await res.json() window.dispatchEvent(new Event(PAGES_CHANGED)) router.push(`/pages?id=${p.id}`) } const patch = async (id: string, body: Record) => { const res = await fetch(`/api/pages/${id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }) if (!res.ok) { toast.error('Could not update page'); return } load() window.dispatchEvent(new Event(PAGES_CHANGED)) } const trash = async (p: PageSummary) => { if (!confirm(`Move page "${p.title}" to the trash? Bookmarks are kept and it can be restored.`)) return const res = await fetch(`/api/pages/${p.id}`, { method: 'DELETE' }) if (!res.ok) { toast.error('Could not delete page'); return } load() window.dispatchEvent(new Event(PAGES_CHANGED)) } if (!mounted) return
return (

Library

{leaves.length} pages · {folders.length} folders
{FILTERS.map((f) => ( ))}
NameFolderEdited
{rows.length === 0 ? (

No pages match this filter.

) : rows.map((p) => (
{folderPath(p) || '—'} {p.updatedAt ? : '—'} router.push(`/pages?id=${p.id}`)}>Open patch(p.id, { pinned: !p.pinned })}>{p.pinned ? : }{p.pinned ? 'Unpin' : 'Pin'} { void navigator.clipboard?.writeText(`${window.location.origin}/pages?id=${p.id}`); toast.success('Link copied') }}>Copy link trash(p)}>Move to trash
))}
) }