Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | 3x 30x 42x 28x 28x 28x 28x 56x 29x 29x 29x 28x 28x 10x 10x 10x 10x 10x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 1x 16x 16x 16x 16x 16x 16x 118x 42x 42x 42x 42x 16x 16x 16x 16x 28x 41x 40x 16x 16x 16x 16x 16x 6x 2x 2x 2x 2x 16x 2x 2x 2x 2x 1x 9x 9x 9x 9x 9x 9x 16x 9x 1x 24x 41x 41x 41x 41x 41x 41x | import { useMemo, useCallback, useState, useRef } from 'react'
import type { ComponentType, SVGAttributes } from 'react'
import type { VaultEntry, GitCommit } from '../types'
import { cn } from '@/lib/utils'
import {
SlidersHorizontal, X, Wrench, Flask, Target, ArrowsClockwise,
Users, CalendarBlank, Tag, FileText, StackSimple, Trash,
} from '@phosphor-icons/react'
import { parseFrontmatter, type ParsedFrontmatter } from '../utils/frontmatter'
import { DynamicPropertiesPanel, RELATIONSHIP_KEYS, containsWikilinks } from './DynamicPropertiesPanel'
import { getTypeColor, getTypeLightColor } from '../utils/typeColors'
const TYPE_ICON_MAP: Record<string, ComponentType<SVGAttributes<SVGSVGElement>>> = {
Project: Wrench,
Experiment: Flask,
Responsibility: Target,
Procedure: ArrowsClockwise,
Person: Users,
Event: CalendarBlank,
Topic: Tag,
Type: StackSimple,
}
function getTypeIcon(isA: string | undefined): ComponentType<SVGAttributes<SVGSVGElement>> {
return (isA && TYPE_ICON_MAP[isA]) || FileText
}
interface InspectorProps {
collapsed: boolean
onToggle: () => void
entry: VaultEntry | null
content: string | null
entries: VaultEntry[]
allContent: Record<string, string>
gitHistory: GitCommit[]
onNavigate: (target: string) => void
onViewCommitDiff?: (commitHash: string) => void
onUpdateFrontmatter?: (path: string, key: string, value: FrontmatterValue) => Promise<void>
onDeleteProperty?: (path: string, key: string) => Promise<void>
onAddProperty?: (path: string, key: string, value: FrontmatterValue) => Promise<void>
}
export type FrontmatterValue = string | number | boolean | string[] | null
/** Check if a string is a wikilink */
function isWikilink(value: string): boolean {
return /^\[\[.*\]\]$/.test(value)
}
/** Extract display name from a wikilink */
function wikilinkDisplay(ref: string): string {
const inner = ref.replace(/^\[\[|\]\]$/g, '')
const pipeIdx = inner.indexOf('|')
Iif (pipeIdx !== -1) return inner.slice(pipeIdx + 1)
const last = inner.split('/').pop() ?? inner
return last.replace(/-/g, ' ').replace(/\b\w/g, (c) => c.toUpperCase())
}
/** Extract the target path for navigation from a wikilink ref */
function wikilinkTarget(ref: string): string {
const inner = ref.replace(/^\[\[|\]\]$/g, '')
const pipeIdx = inner.indexOf('|')
return pipeIdx !== -1 ? inner.slice(0, pipeIdx) : inner
}
function resolveRef(ref: string, entries: VaultEntry[]): VaultEntry | undefined {
const target = wikilinkTarget(ref)
return entries.find((e) => {
const stem = e.path.replace(/^.*\/Laputa\//, '').replace(/\.md$/, '')
Iif (stem === target) return true
const fileStem = e.filename.replace(/\.md$/, '')
Iif (fileStem === target.split('/').pop()) return true
return false
})
}
function RelationshipGroup({ label, refs, entries, onNavigate }: { label: string; refs: string[]; entries: VaultEntry[]; onNavigate: (target: string) => void }) {
Iif (refs.length === 0) return null
return (
<div className="mb-2.5">
<span className="font-mono-overline mb-1 block text-muted-foreground">{label}</span>
<div className="flex flex-col gap-1">
{refs.map((ref, idx) => {
const resolved = resolveRef(ref, entries)
const refType = resolved?.isA ?? undefined
const isArchived = resolved?.archived ?? false
const isTrashed = resolved?.trashed ?? false
const isDimmed = isArchived || isTrashed
const color = refType ? getTypeColor(refType) : 'var(--accent-blue)'
const bgColor = refType ? getTypeLightColor(refType) : 'var(--accent-blue-light)'
const TypeIcon = getTypeIcon(refType)
return (
<button
key={`${ref}-${idx}`}
className="flex items-center justify-between gap-2 border-none text-left cursor-pointer hover:opacity-80"
style={{
background: isDimmed ? 'var(--muted)' : bgColor,
color: isDimmed ? 'var(--muted-foreground)' : color,
borderRadius: 6, padding: '6px 10px', fontSize: 12, fontWeight: 500,
opacity: isDimmed ? 0.7 : 1,
}}
onClick={() => onNavigate(wikilinkTarget(ref))}
title={isTrashed ? 'Trashed' : isArchived ? 'Archived' : undefined}
>
<span className="flex items-center gap-1 flex-1 truncate">
{isTrashed && <Trash size={12} className="shrink-0" />}
{wikilinkDisplay(ref)}
{isTrashed && <span style={{ fontSize: 10, opacity: 0.8 }}>(trashed)</span>}
{isArchived && !isTrashed && <span style={{ marginLeft: 4, fontSize: 10, opacity: 0.8 }}>(archived)</span>}
</span>
<TypeIcon width={14} height={14} className="shrink-0" style={{ color: isDimmed ? 'var(--muted-foreground)' : color }} />
</button>
)
})}
</div>
</div>
)
}
function DynamicRelationshipsPanel({
frontmatter, entries, onNavigate, onAddProperty,
}: {
frontmatter: ParsedFrontmatter
entries: VaultEntry[]
onNavigate: (target: string) => void
onAddProperty?: (key: string, value: FrontmatterValue) => void
}) {
const [showForm, setShowForm] = useState(false)
const [relKey, setRelKey] = useState('')
const [relTarget, setRelTarget] = useState('')
const keyInputRef = useRef<HTMLInputElement>(null)
const relationshipEntries = useMemo(() => {
return Object.entries(frontmatter)
.filter(([key, value]) => key !== 'Type' && (RELATIONSHIP_KEYS.has(key) || containsWikilinks(value)))
.map(([key, value]) => {
const refs: string[] = []
if (typeof value === 'string' && isWikilink(value)) refs.push(valueI)
else if (Array.isArray(value)) value.forEach(v => { if (typeof v === 'string' && isWikilink(v)) refs.push(v) })
return { key, refs }
})
.filter(({ refs }) => refs.length > 0)
}, [frontmatter])
// All note titles for datalist autocomplete
const noteTitles = useMemo(() => entries.map(e => e.title), [entries])
const handleAdd = useCallback(() => {
const key = relKey.trim()
const target = relTarget.trim()
if (!key || !target || !onAddProperty) return
onAddProperty(key, `[[${target}]]`)
setRelKey('')
setRelTarget('')
setShowForm(false)
}, [relKey, relTarget, onAddProperty])
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Enter') handleAdd()
else if (e.key === 'Escape') { setShowForm(false); setRelKey(''); setRelTarget('') }
}
return (
<div>
{relationshipEntries.length === 0 ? (
<p className="m-0 text-[13px] text-muted-foreground">No relationships</p>
) : (
relationshipEntries.map(({ key, refs }) => (
<RelationshipGroup key={key} label={key} refs={refs} entries={entries} onNavigate={onNavigate} />
))
)}
{showForm ? (
<div className="mt-2 flex flex-col gap-1.5" onKeyDown={handleKeyDown}>
<datalist id="rel-note-titles">
{noteTitles.map(t => <option key={t} value={t} />)}
</datalist>
<input
ref={keyInputRef}
autoFocus
className="w-full border border-border bg-transparent px-2 py-1 text-xs text-foreground"
style={{ borderRadius: 4, outline: 'none' }}
placeholder="Relationship name (e.g. Has, Related to)"
value={relKey}
onChange={e => setRelKey(e.target.value)}
/>
<input
className="w-full border border-border bg-transparent px-2 py-1 text-xs text-foreground"
style={{ borderRadius: 4, outline: 'none' }}
placeholder="Note title"
list="rel-note-titles"
value={relTarget}
onChange={e => setRelTarget(e.target.value)}
/>
<div className="flex gap-1.5">
<button
className="flex-1 border border-border bg-transparent text-xs text-foreground"
style={{ borderRadius: 4, padding: '4px 0' }}
onClick={handleAdd}
disabled={!relKey.trim() || !relTarget.trim()}
>
Add
</button>
<button
className="border border-border bg-transparent text-xs text-muted-foreground"
style={{ borderRadius: 4, padding: '4px 8px' }}
onClick={() => { setShowForm(false); setRelKey(''); setRelTarget('') }}
>
Cancel
</button>
</div>
</div>
) : (
<button
className="mt-2 w-full border border-border bg-transparent text-center text-muted-foreground"
style={{ borderRadius: 6, padding: '6px 12px', fontSize: 12, opacity: onAddProperty ? 1 : 0.5, cursor: onAddProperty ? 'pointer' : 'not-allowed' }}
disabled={!onAddProperty}
onClick={() => { setShowForm(true); setTimeout(() => keyInputRef.current?.focus(), 0) }}
>
+ Link existing
</button>
)}
</div>
)
}
function useBacklinks(entry: VaultEntry | null, entries: VaultEntry[], allContent: Record<string, string>): VaultEntry[] {
return useMemo(() => {
if (!entry) return []
const title = entry.title
const stem = entry.filename.replace(/\.md$/, '')
const targets = [title, ...entry.aliases]
const pathStem = entry.path.replace(/^.*\/Laputa\//, '').replace(/\.md$/, '')
return entries.filter((e) => {
if (e.path === entry.path) return false
const content = allContent[e.path]
Iif (!content) return false
for (const t of targets) {
Eif (content.includes(`[[${t}]]`)) return true
}
if (content.includes(`[[${stem}]]`)) return true
if (content.includes(`[[${pathStem}]]`)) return true
if (content.includes(`[[${pathStem}|`)) return true
return false
})
}, [entry, entries, allContent])
}
function BacklinksPanel({ backlinks, onNavigate }: { backlinks: VaultEntry[]; onNavigate: (target: string) => void }) {
return (
<div>
<h4 className="font-mono-overline mb-2 text-muted-foreground">
Backlinks {backlinks.length > 0 && <span className="ml-1" style={{ fontWeight: 400 }}>{backlinks.length}</span>}
</h4>
{backlinks.length === 0 ? (
<p className="m-0 text-[13px] text-muted-foreground">No backlinks</p>
) : (
<div className="flex flex-col gap-0.5">
{backlinks.map((e) => {
const color = e.isA ? getTypeColor(e.isA) : 'var(--accent-blue)'
const TypeIcon = getTypeIcon(e.isA ?? undefined)
const isDimmed = e.archived || e.trashed
return (
<button
key={e.path}
className="flex items-center justify-between gap-2 border-none bg-transparent p-0 py-1 text-left text-[13px] cursor-pointer hover:opacity-80"
style={{ color: isDimmed ? 'var(--muted-foreground)' : color, opacity: isDimmed ? 0.7 : 1 }}
onClick={() => onNavigate(e.title)}
title={e.trashed ? 'Trashed' : e.archived ? 'Archived' : undefined}
>
<span className="flex items-center gap-1 flex-1 truncate">
{e.trashed && <Trash size={12} className="shrink-0" />}
{e.title}
{e.trashed && <span style={{ fontSize: 10, opacity: 0.8 }}>(trashed)</span>}
{e.archived && !e.trashed && <span style={{ marginLeft: 4, fontSize: 10, opacity: 0.8 }}>(archived)</span>}
</span>
{e.isA && <TypeIcon width={14} height={14} className="shrink-0" style={{ color: isDimmed ? 'var(--muted-foreground)' : color }} />}
</button>
)
})}
</div>
)}
</div>
)
}
function formatRelativeDate(timestamp: number): string {
const now = Math.floor(Date.now() / 1000)
const diff = now - timestamp
Iif (diff < 86400) return 'today'
const days = Math.floor(diff / 86400)
Iif (days === 1) return 'yesterday'
Eif (days < 30) return `${days}d ago`
const months = Math.floor(days / 30)
if (months === 1) return '1mo ago'
return `${months}mo ago`
}
function GitHistoryPanel({ commits, onViewCommitDiff }: { commits: GitCommit[]; onViewCommitDiff?: (commitHash: string) => void }) {
return (
<div>
<h4 className="font-mono-overline mb-2 text-muted-foreground">History</h4>
{commits.length === 0 ? (
<p className="m-0 text-[13px] text-muted-foreground">No revision history</p>
) : (
<div className="flex flex-col gap-2.5">
{commits.map((c) => (
<div key={c.hash} style={{ borderLeft: '2px solid var(--border)', paddingLeft: 10 }}>
<div className="mb-0.5 flex items-center justify-between">
<button
className="border-none bg-transparent p-0 font-mono text-primary cursor-pointer hover:underline"
style={{ fontSize: 11 }}
onClick={() => onViewCommitDiff?.(c.hash)}
title={`View diff for ${c.shortHash}`}
>
{c.shortHash}
</button>
<span className="text-muted-foreground" style={{ fontSize: 10 }}>{formatRelativeDate(c.date)}</span>
</div>
<div className="truncate text-xs text-secondary-foreground">{c.message}</div>
{c.author && (
<div className="truncate text-muted-foreground" style={{ fontSize: 10, marginTop: 1 }}>{c.author}</div>
)}
</div>
))}
</div>
)}
</div>
)
}
function EmptyInspector() {
return (
<>
<div>
<p className="m-0 text-[13px] text-muted-foreground">No note selected</p>
</div>
<div>
<p className="m-0 text-[13px] text-muted-foreground">No relationships</p>
</div>
<div>
<h4 className="font-mono-overline mb-2 text-muted-foreground">Backlinks</h4>
<p className="m-0 text-[13px] text-muted-foreground">No backlinks</p>
</div>
<div>
<h4 className="font-mono-overline mb-2 text-muted-foreground">History</h4>
<p className="m-0 text-[13px] text-muted-foreground">No revision history</p>
</div>
</>
)
}
export function Inspector({
collapsed, onToggle, entry, content, entries, allContent, gitHistory, onNavigate,
onViewCommitDiff, onUpdateFrontmatter, onDeleteProperty, onAddProperty,
}: InspectorProps) {
const backlinks = useBacklinks(entry, entries, allContent)
const frontmatter = useMemo(() => parseFrontmatter(content), [content])
const handleUpdateProperty = useCallback((key: string, value: FrontmatterValue) => {
if (entry && onUpdateFrontmatter) onUpdateFrontmatter(entry.path, key, value)
}, [entry, onUpdateFrontmatter])
const handleDeleteProperty = useCallback((key: string) => {
if (entry && onDeleteProperty) onDeleteProperty(entry.path, key)
}, [entry, onDeleteProperty])
const handleAddProperty = useCallback((key: string, value: FrontmatterValue) => {
if (entry && onAddProperty) onAddProperty(entry.path, key, value)
}, [entry, onAddProperty])
return (
<aside className={cn(
"flex flex-1 flex-col overflow-y-auto border-l border-border bg-background text-foreground transition-[width] duration-200",
collapsed && "!w-10 !min-w-10"
)}>
<div className="flex items-center border-b border-border" style={{ height: 45, padding: '0 12px', gap: 8 }} data-tauri-drag-region>
{collapsed ? (
<button className="shrink-0 border-none bg-transparent p-1 text-muted-foreground cursor-pointer hover:text-foreground" onClick={onToggle} style={{ WebkitAppRegion: 'no-drag' } as React.CSSProperties}>
<SlidersHorizontal size={16} />
</button>
) : (
<>
<SlidersHorizontal size={16} className="shrink-0 text-muted-foreground" />
<span className="flex-1 text-muted-foreground" style={{ fontSize: 13, fontWeight: 600 }}>Properties</span>
<button className="shrink-0 border-none bg-transparent p-1 text-muted-foreground cursor-pointer hover:text-foreground" onClick={onToggle} style={{ WebkitAppRegion: 'no-drag' } as React.CSSProperties}>
<X size={16} />
</button>
</>
)}
</div>
{!collapsed && (
<div className="flex flex-col gap-4 p-3">
{entry ? (
<>
<DynamicPropertiesPanel
entry={entry} content={content} frontmatter={frontmatter}
onUpdateProperty={onUpdateFrontmatter ? handleUpdateProperty : undefined}
onDeleteProperty={onDeleteProperty ? handleDeleteProperty : undefined}
onAddProperty={onAddProperty ? handleAddProperty : undefined}
onNavigate={onNavigate}
/>
<DynamicRelationshipsPanel frontmatter={frontmatter} entries={entries} onNavigate={onNavigate} onAddProperty={onAddProperty ? handleAddProperty : undefined} />
<BacklinksPanel backlinks={backlinks} onNavigate={onNavigate} />
<GitHistoryPanel commits={gitHistory} onViewCommitDiff={onViewCommitDiff} />
</>
) : (
<EmptyInspector />
)}
</div>
)}
</aside>
)
}
|