import { useMemo, type ComponentType, type SVGAttributes } from 'react' import type { VaultEntry, NoteStatus } from '../types' import { cn } from '@/lib/utils' import { Wrench, Flask, Target, ArrowsClockwise, Users, CalendarBlank, Tag, FileText, StackSimple, File, FileDashed, } from '@phosphor-icons/react' import { getTypeColor, getTypeLightColor } from '../utils/typeColors' import { resolveIcon } from '../utils/iconRegistry' import { relativeDate, getDisplayDate } from '../utils/noteListHelpers' import { isEmoji } from '../utils/emoji' import { wikilinkDisplay } from '../utils/wikilink' const TYPE_ICON_MAP: Record>> = { Project: Wrench, Experiment: Flask, Responsibility: Target, Procedure: ArrowsClockwise, Person: Users, Event: CalendarBlank, Topic: Tag, Type: StackSimple, } // eslint-disable-next-line react-refresh/only-export-components -- utility co-located with component export function getTypeIcon(isA: string | null, customIcon?: string | null): ComponentType> { if (customIcon) return resolveIcon(customIcon) return (isA && TYPE_ICON_MAP[isA]) || FileText } const NOTE_STATUS_DOT: Record = { pendingSave: { color: 'var(--accent-green)', testId: 'pending-save-indicator', title: 'Saving to disk…' }, new: { color: 'var(--accent-green)', testId: 'new-indicator', title: 'New (uncommitted)' }, modified: { color: 'var(--accent-orange)', testId: 'modified-indicator', title: 'Modified (uncommitted)' }, } function StatusDot({ noteStatus }: { noteStatus: NoteStatus }) { const dot = NOTE_STATUS_DOT[noteStatus] if (!dot) return null return ( ) } function StateBadge({ archived }: { archived: boolean }) { if (archived) { return ( ARCHIVED ) } return null } function formatChipValue(value: unknown): string | null { if (value === null || value === undefined || value === '') return null const s = String(value) // URL: show only hostname try { if (s.startsWith('http://') || s.startsWith('https://')) return new URL(s).hostname } catch { /* not a URL */ } return s.length > 40 ? s.slice(0, 37) + '…' : s } function resolveChipValues(entry: VaultEntry, propName: string): string[] { // Check relationships first (wikilink values) const relKey = Object.keys(entry.relationships).find((k) => k.toLowerCase() === propName.toLowerCase()) if (relKey) { return entry.relationships[relKey].map((ref) => wikilinkDisplay(ref)).filter(Boolean) } // Check scalar properties const propKey = Object.keys(entry.properties).find((k) => k.toLowerCase() === propName.toLowerCase()) if (!propKey) return [] const val = entry.properties[propKey] if (Array.isArray(val)) return val.map((v) => formatChipValue(v)).filter((v): v is string => v !== null) const formatted = formatChipValue(val) return formatted ? [formatted] : [] } function PropertyChips({ entry, displayProps }: { entry: VaultEntry; displayProps: string[] }) { const chips = useMemo(() => { const result: { key: string; values: string[] }[] = [] for (const prop of displayProps) { const values = resolveChipValues(entry, prop) if (values.length > 0) result.push({ key: prop, values }) } return result }, [entry, displayProps]) if (chips.length === 0) return null return (
{chips.map(({ key, values }) => values.map((v, i) => ( {v} )) )}
) } const CHANGE_STATUS_DISPLAY: Record = { modified: { label: 'Modified', color: 'var(--accent-orange, #f59e0b)', symbol: '·' }, added: { label: 'Added', color: 'var(--accent-green, #22c55e)', symbol: '+' }, untracked: { label: 'Added', color: 'var(--accent-green, #22c55e)', symbol: '+' }, deleted: { label: 'Deleted', color: 'var(--destructive, #ef4444)', symbol: '−' }, renamed: { label: 'Renamed', color: 'var(--accent-orange, #f59e0b)', symbol: 'R' }, } function ChangeStatusIcon({ status }: { status: string }) { const display = CHANGE_STATUS_DISPLAY[status] ?? CHANGE_STATUS_DISPLAY.modified return ( {display.symbol} ) } function noteItemStyle(isSelected: boolean, isMultiSelected: boolean, typeColor: string, typeLightColor: string): React.CSSProperties { const base: React.CSSProperties = { padding: isSelected && !isMultiSelected ? '14px 16px 14px 13px' : '14px 16px' } if (isMultiSelected) base.backgroundColor = 'color-mix(in srgb, var(--accent-blue) 10%, transparent)' else if (isSelected) { base.borderLeftColor = typeColor; base.backgroundColor = typeLightColor } return base } function getFileKindIcon(fileKind: string | undefined): ComponentType> { if (fileKind === 'text') return File if (fileKind === 'binary') return FileDashed return FileText } export function NoteItem({ entry, isSelected, isMultiSelected = false, isHighlighted = false, noteStatus = 'clean', changeStatus, typeEntryMap, onClickNote, onPrefetch, onContextMenu }: { entry: VaultEntry isSelected: boolean isMultiSelected?: boolean isHighlighted?: boolean noteStatus?: NoteStatus /** When set, renders in Changes-view style: filename + change type icon */ changeStatus?: 'modified' | 'added' | 'deleted' | 'untracked' | 'renamed' typeEntryMap: Record onClickNote: (entry: VaultEntry, e: React.MouseEvent) => void onPrefetch?: (path: string) => void onContextMenu?: (entry: VaultEntry, e: React.MouseEvent) => void }) { const isBinary = entry.fileKind === 'binary' const isNonMarkdown = !!entry.fileKind && entry.fileKind !== 'markdown' const isDeletedChange = changeStatus === 'deleted' const te = typeEntryMap[entry.isA ?? ''] const typeColor = isBinary ? 'var(--muted-foreground)' : getTypeColor(entry.isA ?? 'Note', te?.color) const typeLightColor = getTypeLightColor(entry.isA ?? 'Note', te?.color) const TypeIcon = useMemo(() => { if (isNonMarkdown) return getFileKindIcon(entry.fileKind) return getTypeIcon(entry.isA, te?.icon) }, [entry.isA, te?.icon, entry.fileKind, isNonMarkdown]) const handleClick = isBinary ? (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation() } : (e: React.MouseEvent) => onClickNote(entry, e) return (
onContextMenu(entry, e) : undefined} onMouseEnter={!isBinary && onPrefetch ? () => onPrefetch(entry.path) : undefined} data-testid={isMultiSelected ? 'multi-selected-item' : isBinary ? 'binary-file-item' : undefined} data-highlighted={isHighlighted || undefined} data-note-path={entry.path} data-change-status={changeStatus} title={isBinary ? 'Cannot open this file type' : undefined} > {changeStatus ? ( <>
{entry.filename}
) : ( <> {/* eslint-disable-next-line react-hooks/static-components -- icon lookup from static map, no internal state */}
{noteStatus !== 'clean' && !isBinary && } {entry.icon && isEmoji(entry.icon) && {entry.icon}} {entry.title} {!isBinary && }
{entry.snippet && !isBinary && (
{entry.snippet}
)} {!isBinary && te?.listPropertiesDisplay && te.listPropertiesDisplay.length > 0 && ( )} {!isBinary && (
{relativeDate(getDisplayDate(entry))}
)} )}
) }