import { useState } from 'react' import type { VaultEntry, SidebarSelection, ModifiedFile } from '../types' import { cn } from '@/lib/utils' import { Input } from '@/components/ui/input' import { MagnifyingGlass, Plus } from '@phosphor-icons/react' interface NoteListProps { entries: VaultEntry[] selection: SidebarSelection selectedNote: VaultEntry | null allContent: Record modifiedFiles?: ModifiedFile[] onSelectNote: (entry: VaultEntry) => void onCreateNote: () => void } interface RelationshipGroup { label: string entries: VaultEntry[] } /** Extract first ~80 chars of content after the title heading */ function getSnippet(content: string | undefined): string { if (!content) return '' const withoutFm = content.replace(/^---[\s\S]*?---\s*/, '') const withoutH1 = withoutFm.replace(/^#\s+.*\n+/, '') const clean = withoutH1 .replace(/[#*_`\[\]]/g, '') .replace(/\n+/g, ' ') .trim() return clean.slice(0, 160) + (clean.length > 160 ? '...' : '') } function relativeDate(ts: number | null): string { if (!ts) return '' const now = Math.floor(Date.now() / 1000) const diff = now - ts if (diff < 0) { const date = new Date(ts * 1000) return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }) } if (diff < 60) return 'just now' if (diff < 3600) return `${Math.floor(diff / 60)}m ago` if (diff < 86400) return `${Math.floor(diff / 3600)}h ago` if (diff < 604800) return `${Math.floor(diff / 86400)}d ago` const date = new Date(ts * 1000) return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }) } function getDisplayDate(entry: VaultEntry): number | null { return entry.modifiedAt ?? entry.createdAt } function refsMatch(refs: string[], entry: VaultEntry): boolean { const stem = entry.path.replace(/^.*\/Laputa\//, '').replace(/\.md$/, '') return refs.some((ref) => { const inner = ref.replace(/^\[\[/, '').replace(/\]\]$/, '') return inner === stem }) } function resolveRefs(refs: string[], entries: VaultEntry[]): VaultEntry[] { return refs .map((ref) => { const inner = ref.replace(/^\[\[/, '').replace(/\]\]$/, '') return entries.find((e) => { const stem = e.path.replace(/^.*\/Laputa\//, '').replace(/\.md$/, '') if (stem === inner) return true const fileStem = e.filename.replace(/\.md$/, '') if (fileStem === inner.split('/').pop()) return true return false }) }) .filter((e): e is VaultEntry => e !== undefined) } function sortByModified(a: VaultEntry, b: VaultEntry): number { return (getDisplayDate(b) ?? 0) - (getDisplayDate(a) ?? 0) } function buildRelationshipGroups(entity: VaultEntry, allEntries: VaultEntry[]): RelationshipGroup[] { const groups: RelationshipGroup[] = [] const seen = new Set([entity.path]) const children = allEntries .filter((e) => !seen.has(e.path) && e.isA !== 'Event' && refsMatch(e.belongsTo, entity)) .sort(sortByModified) if (children.length > 0) { groups.push({ label: 'Children', entries: children }) children.forEach((e) => seen.add(e.path)) } const events = allEntries .filter( (e) => !seen.has(e.path) && e.isA === 'Event' && (refsMatch(e.belongsTo, entity) || refsMatch(e.relatedTo, entity)) ) .sort(sortByModified) if (events.length > 0) { groups.push({ label: 'Events', entries: events }) events.forEach((e) => seen.add(e.path)) } const referencedBy = allEntries .filter((e) => !seen.has(e.path) && e.isA !== 'Event' && refsMatch(e.relatedTo, entity)) .sort(sortByModified) if (referencedBy.length > 0) { groups.push({ label: 'Referenced By', entries: referencedBy }) referencedBy.forEach((e) => seen.add(e.path)) } const belongsTo = resolveRefs(entity.belongsTo, allEntries).filter((e) => !seen.has(e.path)) if (belongsTo.length > 0) { groups.push({ label: 'Belongs To', entries: belongsTo }) belongsTo.forEach((e) => seen.add(e.path)) } const relatedTo = resolveRefs(entity.relatedTo, allEntries).filter((e) => !seen.has(e.path)) if (relatedTo.length > 0) { groups.push({ label: 'Related To', entries: relatedTo }) relatedTo.forEach((e) => seen.add(e.path)) } return groups } function filterEntries(entries: VaultEntry[], selection: SidebarSelection, _modifiedFiles?: ModifiedFile[]): VaultEntry[] { switch (selection.kind) { case 'filter': switch (selection.filter) { case 'all': return entries case 'favorites': return [] } break case 'sectionGroup': return entries.filter((e) => e.isA === selection.type) case 'entity': return [] case 'topic': { const topic = selection.entry return entries.filter((e) => refsMatch(e.relatedTo, topic)) } } } const TYPE_PILLS = [ { label: 'All', type: null }, { label: 'Projects', type: 'Project' }, { label: 'Notes', type: 'Note' }, { label: 'Events', type: 'Event' }, { label: 'People', type: 'Person' }, { label: 'Experiments', type: 'Experiment' }, { label: 'Procedures', type: 'Procedure' }, { label: 'Responsibilities', type: 'Responsibility' }, ] as const export function NoteList({ entries, selection, selectedNote, allContent, modifiedFiles, onSelectNote, onCreateNote }: NoteListProps) { const [search, setSearch] = useState('') const [searchVisible, setSearchVisible] = useState(false) const [typeFilter, setTypeFilter] = useState(null) const isEntityView = selection.kind === 'entity' const entityGroups = isEntityView ? buildRelationshipGroups(selection.entry, entries) : [] const modifiedStatusMap = new Map() if (modifiedFiles) { for (const f of modifiedFiles) { modifiedStatusMap.set(f.path, f.status) } } const filtered = isEntityView ? [] : filterEntries(entries, selection, modifiedFiles) const sorted = isEntityView ? [] : [...filtered].sort(sortByModified) const query = search.trim().toLowerCase() const searchedGroups = query ? entityGroups .map((g) => ({ ...g, entries: g.entries.filter((e) => e.title.toLowerCase().includes(query)), })) .filter((g) => g.entries.length > 0) : entityGroups const searched = query ? sorted.filter((e) => e.title.toLowerCase().includes(query)) : sorted const typeCounts = new Map() typeCounts.set(null, searched.length) for (const entry of searched) { if (entry.isA) { typeCounts.set(entry.isA, (typeCounts.get(entry.isA) ?? 0) + 1) } } const displayed = typeFilter ? searched.filter((e) => e.isA === typeFilter) : searched const renderItem = (entry: VaultEntry, isPinned = false) => { const isSelected = selectedNote?.path === entry.path && !isPinned return (
onSelectNote(entry)} >
{entry.title}
{relativeDate(getDisplayDate(entry))}
{getSnippet(allContent[entry.path])}
) } return (
{/* Header */}

{isEntityView ? selection.entry.title : 'Notes'}

{/* Search (toggle on icon click) */} {searchVisible && (
setSearch(e.target.value)} className="h-8 text-[13px]" autoFocus />
)} {/* Type filter pills */} {!isEntityView && (
{TYPE_PILLS.filter(({ type }) => { const count = typeCounts.get(type) ?? 0 return type === null || count > 0 }).map(({ label, type }) => { const count = typeCounts.get(type) ?? 0 const isActive = typeFilter === type return ( ) })}
)} {/* Items */}
{isEntityView ? ( <> {renderItem(selection.entry, true)} {searchedGroups.length === 0 ? (
{query ? 'No matching items' : 'No related items'}
) : ( searchedGroups.map((group) => (
{group.label} {group.entries.length}
{group.entries.map((entry) => renderItem(entry))}
)) )} ) : ( displayed.length === 0 ? (
No notes found
) : ( displayed.map((entry) => renderItem(entry)) ) )}
) }