import { useState, useMemo, useCallback, memo } from 'react' import { Virtuoso } from 'react-virtuoso' 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' import { getTypeColor, getTypeLightColor } from '../utils/typeColors' 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[] } 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 function NoteListInner({ entries, selection, selectedNote, 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 = useMemo( () => isEntityView ? buildRelationshipGroups(selection.entry, entries) : [], [isEntityView, selection, entries] ) const filtered = useMemo( () => isEntityView ? [] : filterEntries(entries, selection, modifiedFiles), [entries, selection, modifiedFiles, isEntityView] ) const sorted = useMemo( () => isEntityView ? [] : [...filtered].sort(sortByModified), [filtered, isEntityView] ) const query = search.trim().toLowerCase() const searched = useMemo( () => query ? sorted.filter((e) => e.title.toLowerCase().includes(query)) : sorted, [sorted, query] ) const searchedGroups = useMemo( () => query ? entityGroups .map((g) => ({ ...g, entries: g.entries.filter((e) => e.title.toLowerCase().includes(query)), })) .filter((g) => g.entries.length > 0) : entityGroups, [entityGroups, query] ) const typeCounts = useMemo(() => { const counts = new Map() counts.set(null, searched.length) for (const entry of searched) { if (entry.isA) { counts.set(entry.isA, (counts.get(entry.isA) ?? 0) + 1) } } return counts }, [searched]) const displayed = useMemo( () => typeFilter ? searched.filter((e) => e.isA === typeFilter) : searched, [searched, typeFilter] ) const renderItem = useCallback((entry: VaultEntry, isPinned = false) => { const isSelected = selectedNote?.path === entry.path && !isPinned const typeColor = getTypeColor(entry.isA) const typeLightColor = getTypeLightColor(entry.isA) return (
onSelectNote(entry)} >
{entry.title}
{relativeDate(getDisplayDate(entry))}
{entry.snippet}
) }, [selectedNote?.path, onSelectNote]) 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 const pillColor = type ? getTypeColor(type) : 'var(--accent-blue)' const pillLightColor = type ? getTypeLightColor(type) : 'var(--accent-blue-light)' 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
) : ( { const entry = displayed[index] return entry ? renderItem(entry) : null }} /> ) )}
) } export const NoteList = memo(NoteListInner)