import { useMemo, useState, useCallback, useRef } from 'react' import type { VaultEntry } from '../types' import type { FrontmatterValue } from './Inspector' import type { ParsedFrontmatter } from '../utils/frontmatter' import { EditableValue, TagPillList, UrlValue } from './EditableValue' import { isUrlValue } from '../utils/url' import { Button } from '@/components/ui/button' import { Calendar } from '@/components/ui/calendar' import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover' import { Select, SelectContent, SelectItem, SelectSeparator, SelectTrigger, SelectValue } from '@/components/ui/select' import { CalendarIcon, XIcon, Check, X, Type, ToggleLeft, Circle, Link } from 'lucide-react' import { getTypeColor, getTypeLightColor } from '../utils/typeColors' import { countWords } from '../utils/wikilinks' import { type PropertyDisplayMode, getEffectiveDisplayMode, formatDateValue, toISODate, loadDisplayModeOverrides, saveDisplayModeOverride, removeDisplayModeOverride, detectPropertyType, } from '../utils/propertyTypes' import { StatusPill, StatusDropdown } from './StatusDropdown' // Keys that are relationships (contain wikilinks) export const RELATIONSHIP_KEYS = new Set([ 'Belongs to', 'Related to', 'Events', 'Has Data', 'Owner', 'Advances', 'Parent', 'Children', 'Has', 'Notes', ]) // Keys to skip showing in Properties (handled by dedicated UI or internal) const SKIP_KEYS = new Set(['aliases', 'notion_id', 'workspace', 'title', 'type', 'is_a', 'Is A']) // eslint-disable-next-line react-refresh/only-export-components -- utility co-located with component export function containsWikilinks(value: FrontmatterValue): boolean { if (typeof value === 'string') return /^\[\[.*\]\]$/.test(value) if (Array.isArray(value)) return value.some(v => typeof v === 'string' && /^\[\[.*\]\]$/.test(v)) return false } function formatDate(timestamp: number | null): string { if (!timestamp) return '\u2014' const d = new Date(timestamp * 1000) return d.toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' }) } function coerceValue(raw: string): FrontmatterValue { if (raw.toLowerCase() === 'true') return true if (raw.toLowerCase() === 'false') return false if (!isNaN(Number(raw)) && raw.trim() !== '') return Number(raw) return raw } function parseNewValue(rawValue: string): FrontmatterValue { if (!rawValue.includes(',')) return rawValue.trim() || '' const items = rawValue.split(',').map(s => s.trim()).filter(s => s) return items.length === 1 ? items[0] : items } function formatFileSize(bytes: number): string { if (bytes < 1024) return `${bytes} B` const kb = bytes / 1024 if (kb < 1024) return `${kb.toFixed(1)} KB` const mb = kb / 1024 return `${mb.toFixed(1)} MB` } function StatusValue({ propKey, value, isEditing, vaultStatuses, onSave, onStartEdit }: { propKey: string; value: FrontmatterValue; isEditing: boolean; vaultStatuses: string[] onSave: (key: string, value: string) => void; onStartEdit: (key: string | null) => void }) { const statusStr = String(value) return ( onStartEdit(propKey)} data-testid="status-badge" > {isEditing && ( onSave(propKey, newValue)} onCancel={() => onStartEdit(null)} /> )} ) } function BooleanToggle({ value, onToggle }: { value: boolean; onToggle: () => void }) { return ( ) } function parseDateValue(value: string): Date | undefined { const iso = toISODate(value) const d = new Date(iso + 'T00:00:00') return isNaN(d.getTime()) ? undefined : d } function DateValue({ value, onSave }: { value: string; onSave: (newValue: string) => void }) { const [open, setOpen] = useState(false) const formatted = formatDateValue(value) const selectedDate = parseDateValue(value) const handleSelect = (day: Date | undefined) => { if (day) { const yyyy = day.getFullYear() const mm = String(day.getMonth() + 1).padStart(2, '0') const dd = String(day.getDate()).padStart(2, '0') onSave(`${yyyy}-${mm}-${dd}`) } setOpen(false) } const handleClear = (e: React.MouseEvent) => { e.stopPropagation() onSave('') setOpen(false) } return ( {selectedDate && (
)}
) } const DISPLAY_MODE_OPTIONS: { value: PropertyDisplayMode; label: string }[] = [ { value: 'text', label: 'Text' }, { value: 'date', label: 'Date' }, { value: 'boolean', label: 'Boolean' }, { value: 'status', label: 'Status' }, { value: 'url', label: 'URL' }, ] function DisplayModeSelector({ propKey, currentMode, autoMode, onSelect }: { propKey: string; currentMode: PropertyDisplayMode; autoMode: PropertyDisplayMode onSelect: (key: string, mode: PropertyDisplayMode | null) => void }) { const [open, setOpen] = useState(false) const containerRef = useRef(null) const handleSelect = (mode: PropertyDisplayMode) => { if (mode === autoMode) { onSelect(propKey, null) } else { onSelect(propKey, mode) } setOpen(false) } return (
{open && ( <>
setOpen(false)} />
{DISPLAY_MODE_OPTIONS.map(opt => ( ))}
)}
) } const DISPLAY_MODE_ICONS: Record = { text: Type, date: CalendarIcon, boolean: ToggleLeft, status: Circle, url: Link, } function AddPropertyForm({ onAdd, onCancel }: { onAdd: (key: string, value: string, displayMode: PropertyDisplayMode) => void; onCancel: () => void }) { const [newKey, setNewKey] = useState('') const [newValue, setNewValue] = useState('') const [displayMode, setDisplayMode] = useState('text') const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && newKey.trim()) onAdd(newKey, newValue, displayMode) else if (e.key === 'Escape') onCancel() } return (
setNewKey(e.target.value)} onKeyDown={handleKeyDown} autoFocus /> setNewValue(e.target.value)} onKeyDown={handleKeyDown} />
) } const TYPE_NONE = '__none__' function ReadOnlyType({ isA, customColorKey, onNavigate }: { isA?: string | null; customColorKey?: string | null; onNavigate?: (target: string) => void }) { if (!isA) return null return (
Type {onNavigate ? ( ) : ( {isA} )}
) } function TypeSelector({ isA, customColorKey, availableTypes, onUpdateProperty, onNavigate }: { isA?: string | null; customColorKey?: string | null; availableTypes: string[] onUpdateProperty?: (key: string, value: FrontmatterValue) => void onNavigate?: (target: string) => void }) { if (!onUpdateProperty) return const currentValue = isA || TYPE_NONE const options = isA && !availableTypes.includes(isA) ? [...availableTypes, isA].sort((a, b) => a.localeCompare(b)) : availableTypes return (
Type
) } function SmartPropertyValueCell({ propKey, value, displayMode, isEditing, vaultStatuses, onStartEdit, onSave, onSaveList, onUpdate }: { propKey: string; value: FrontmatterValue; displayMode: PropertyDisplayMode; isEditing: boolean vaultStatuses: string[] onStartEdit: (key: string | null) => void; onSave: (key: string, value: string) => void onSaveList: (key: string, items: string[]) => void; onUpdate?: (key: string, value: FrontmatterValue) => void }) { const editProps = { value: String(value ?? ''), isEditing, onStartEdit: () => onStartEdit(propKey), onSave: (v: string) => onSave(propKey, v), onCancel: () => onStartEdit(null) } if (value === null || value === undefined) return if (Array.isArray(value)) return onSaveList(propKey, items)} label={propKey} /> switch (displayMode) { case 'status': return case 'date': if (typeof value === 'string') { return onSave(propKey, v)} /> } return case 'boolean': if (typeof value === 'boolean') { return onUpdate?.(propKey, !value)} /> } return case 'url': if (typeof value === 'string' && isUrlValue(value)) { return } return default: if (typeof value === 'boolean') { return onUpdate?.(propKey, !value)} /> } if (typeof value === 'string' && isUrlValue(value)) { return } return } } function PropertyRow({ propKey, value, editingKey, displayMode, autoMode, vaultStatuses, onStartEdit, onSave, onSaveList, onUpdate, onDelete, onDisplayModeChange }: { propKey: string; value: FrontmatterValue; editingKey: string | null displayMode: PropertyDisplayMode; autoMode: PropertyDisplayMode vaultStatuses: string[] onStartEdit: (key: string | null) => void; onSave: (key: string, value: string) => void onSaveList: (key: string, items: string[]) => void onUpdate?: (key: string, value: FrontmatterValue) => void; onDelete?: (key: string) => void onDisplayModeChange: (key: string, mode: PropertyDisplayMode | null) => void }) { return (
{propKey} {onDelete && ( )}
) } function InfoRow({ label, value }: { label: string; value: string }) { return (
{label} {value}
) } function AddPropertyButton({ onClick, disabled }: { onClick: () => void; disabled: boolean }) { return ( ) } function NoteInfoSection({ entry, wordCount }: { entry: VaultEntry; wordCount: number }) { return (

Info

) } function reconcileListUpdate( newItems: string[], onUpdate: (key: string, value: FrontmatterValue) => void, onDelete: ((key: string) => void) | undefined, key: string, ) { if (newItems.length === 0) onDelete?.(key) else if (newItems.length === 1) onUpdate(key, newItems[0]) else onUpdate(key, newItems) } export function DynamicPropertiesPanel({ entry, content, frontmatter, entries, onUpdateProperty, onDeleteProperty, onAddProperty, onNavigate, }: { entry: VaultEntry content: string | null frontmatter: ParsedFrontmatter entries?: VaultEntry[] onUpdateProperty?: (key: string, value: FrontmatterValue) => void onDeleteProperty?: (key: string) => void onAddProperty?: (key: string, value: FrontmatterValue) => void onNavigate?: (target: string) => void }) { const [editingKey, setEditingKey] = useState(null) const [showAddDialog, setShowAddDialog] = useState(false) const [displayOverrides, setDisplayOverrides] = useState(() => loadDisplayModeOverrides()) const wordCount = countWords(content ?? '') const { availableTypes, customColorKey } = useMemo(() => { const typeEntries = (entries ?? []).filter(e => e.isA === 'Type') return { availableTypes: typeEntries.map(e => e.title).sort((a, b) => a.localeCompare(b)), customColorKey: entry.isA ? (typeEntries.find(e => e.title === entry.isA)?.color ?? null) : null, } }, [entries, entry.isA]) const vaultStatuses = useMemo(() => { const seen = new Set() for (const e of entries ?? []) { if (e.status) seen.add(e.status) } return Array.from(seen).sort((a, b) => a.localeCompare(b)) }, [entries]) const propertyEntries = useMemo(() => { return Object.entries(frontmatter) .filter(([key, value]) => !SKIP_KEYS.has(key) && !RELATIONSHIP_KEYS.has(key) && !containsWikilinks(value)) }, [frontmatter]) const handleSaveValue = useCallback((key: string, newValue: string) => { setEditingKey(null) if (onUpdateProperty) onUpdateProperty(key, coerceValue(newValue)) }, [onUpdateProperty]) const handleSaveList = useCallback((key: string, newItems: string[]) => { if (!onUpdateProperty) return reconcileListUpdate(newItems, onUpdateProperty, onDeleteProperty, key) }, [onUpdateProperty, onDeleteProperty]) const handleAdd = useCallback((rawKey: string, rawValue: string, mode: PropertyDisplayMode) => { if (!rawKey.trim() || !onAddProperty) return onAddProperty(rawKey.trim(), parseNewValue(rawValue)) if (mode !== 'text') { saveDisplayModeOverride(rawKey.trim(), mode) setDisplayOverrides(loadDisplayModeOverrides()) } setShowAddDialog(false) }, [onAddProperty]) const handleDisplayModeChange = useCallback((key: string, mode: PropertyDisplayMode | null) => { if (mode === null) { removeDisplayModeOverride(key) } else { saveDisplayModeOverride(key, mode) } setDisplayOverrides(loadDisplayModeOverrides()) }, []) return (
{/* Editable properties section */}
{propertyEntries.map(([key, value]) => { const autoMode = detectPropertyType(key, value) const effectiveMode = getEffectiveDisplayMode(key, value, displayOverrides) return ( ) })}
{showAddDialog ? setShowAddDialog(false)} /> : setShowAddDialog(true)} disabled={!onAddProperty} /> }
) }