Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring
TypeScript diagramhomelabhomelab-infrastructure-observabilitynetworkproxmox-veself-hostedzigbeezwavezwave-js
feat(docs): write a wiki-link from a picker instead of from memory
Pouzord7924d5ed034
diff --git a/frontend/src/documentation/__tests__/DocEditorLinks.test.tsx b/frontend/src/documentation/__tests__/DocEditorLinks.test.tsxnew file mode 100644index 0000000..89230e0--- /dev/null+++ b/frontend/src/documentation/__tests__/DocEditorLinks.test.tsx@@ -0,0 +1,164 @@+/**+ * The `[[` picker.+ *+ * Wiki-links were unusable unless you remembered a title or a slug exactly.+ * What matters here is the arithmetic: the two brackets stay in the body while+ * the menu is open, and choosing a target replaces exactly those two.+ */+import { useState } from 'react'+import { render, screen, waitFor } from '@testing-library/react'+import userEvent from '@testing-library/user-event'+import { describe, expect, it, vi } from 'vitest'++import { DocEditor } from '../components/DocEditor'+import type { LinkableDevice, LinkableDoc } from '../wikilinks'++vi.mock('@/api/client', () => ({+ documentsApi: { block: vi.fn() },+}))++const DOCS: LinkableDoc[] = [+ { id: 'd1', slug: 'vlan-plan', title: 'VLAN plan' },+ { id: 'd2', slug: 'nas-01', title: 'nas-01', device_id: 'dev-1' },+ { id: 'd3', slug: 'backup-runbook', title: 'Backup runbook' },+]++const DEVICES: LinkableDevice[] = [{ id: 'dev-1', label: 'nas-01' }]++/** A stateful host, so the brackets really land in the textarea's value. */+function setup(initial = '', docs: LinkableDoc[] = DOCS, devices = DEVICES) {+ const onChange = vi.fn()+ function Host() {+ const [body, setBody] = useState(initial)+ return (+ <DocEditor+ body={body}+ onChange={(next) => {+ onChange(next)+ setBody(next)+ }}+ onSave={vi.fn()}+ onCancel={vi.fn()}+ dirty={false}+ saving={false}+ docs={docs}+ devices={devices}+ />+ )+ }+ render(<Host />)+ return { onChange, textarea: screen.getByLabelText('Document source') as HTMLTextAreaElement }+}++// userEvent reads `[…]` as a key code, so a literal bracket is doubled: the+// four characters below type exactly `[[`.+const TWO_BRACKETS = '[[[['++async function openPicker(textarea: HTMLTextAreaElement, user = userEvent.setup()) {+ await user.click(textarea)+ await user.keyboard(TWO_BRACKETS)+ return user+}++describe('DocEditor — the [[ picker', () => {+ it('opens on the second bracket and lists the documents', async () => {+ const { textarea } = setup('Runs on ')++ await openPicker(textarea)++ expect(screen.getByLabelText('Link to a document')).toBeTruthy()+ expect(screen.getByText('VLAN plan')).toBeTruthy()+ expect(screen.getByText('Backup runbook')).toBeTruthy()+ })++ it('holds the second bracket back while it is open', async () => {+ const { textarea } = setup('Runs on ')++ await openPicker(textarea)++ // The picker owns the second bracket until it inserts or is cancelled.+ expect(textarea.value).toBe('Runs on [')+ })++ it('does not open on a single bracket', async () => {+ const { textarea } = setup('')+ const user = userEvent.setup()++ await user.click(textarea)+ await user.keyboard('[[')++ expect(textarea.value).toBe('[')+ expect(screen.queryByLabelText('Link to a document')).toBeNull()+ })++ it('replaces both brackets with the finished link', async () => {+ const { textarea } = setup('Runs on ')+ const user = await openPicker(textarea)++ await user.click(screen.getByText('VLAN plan'))++ await waitFor(() => expect(textarea.value).toBe('Runs on [[VLAN plan]]'))+ })++ it('filters as you type, and Enter takes the first match', async () => {+ const { textarea } = setup('')+ const user = await openPicker(textarea)++ await user.type(screen.getByLabelText('Link to a document'), 'backup')+ expect(screen.queryByText('VLAN plan')).toBeNull()+ await user.keyboard('{Enter}')++ await waitFor(() => expect(textarea.value).toBe('[[Backup runbook]]'))+ })++ it('says which document belongs to a device', async () => {+ const { textarea } = setup('')++ await openPicker(textarea)++ expect(screen.getByText('Device · nas-01')).toBeTruthy()+ })++ it('addresses a document by id when its title is ambiguous', async () => {+ const { textarea } = setup('', [+ { id: 'd1', slug: 'incident-1', title: 'Incident' },+ { id: 'd2', slug: 'incident-2', title: 'Incident' },+ ])+ const user = await openPicker(textarea)++ await user.click(screen.getAllByText('Incident')[0])++ // A bare `[[Incident]]` resolves by title and would pick whichever came+ // first, so an ambiguous title is written as an id instead.+ await waitFor(() => expect(textarea.value).toBe('[[doc:d1]]'))+ })++ it('closes on Escape and leaves what was typed', async () => {+ const { textarea } = setup('Runs on ')+ const user = await openPicker(textarea)++ await user.type(screen.getByLabelText('Link to a document'), '{Escape}')++ expect(screen.queryByLabelText('Link to a document')).toBeNull()+ expect(textarea.value).toBe('Runs on [[')+ })++ it('says so when there is nothing to link to yet', async () => {+ const { textarea } = setup('', [])++ await openPicker(textarea)++ expect(screen.getByText('No document to link to yet.')).toBeTruthy()+ })++ it('still opens the slash menu on a new line', async () => {+ const { textarea } = setup('')+ const user = userEvent.setup()++ await user.click(textarea)+ await user.keyboard('/')++ expect(screen.getByLabelText('Insert a block')).toBeTruthy()+ expect(screen.queryByLabelText('Link to a document')).toBeNull()+ })+})diff --git a/frontend/src/documentation/components/DocEditor.tsx b/frontend/src/documentation/components/DocEditor.tsxindex d32aac8..050769e 100644--- a/frontend/src/documentation/components/DocEditor.tsx+++ b/frontend/src/documentation/components/DocEditor.tsx@@ -8,6 +8,7 @@ import { Button } from '@/components/ui/button'import { cn } from '@/lib/utils'import { Markdown } from '../markdown/Markdown'import type { LinkableDevice, LinkableDoc } from '../wikilinks'+import { EditorMenu, type EditorMenuItem } from './EditorMenu'/*** Source on the left, rendered on the right.@@ -34,6 +35,8 @@ interface Props {saving: boolean/** Set when the document describes a device — enables the generated blocks. */deviceId?: string | null+ /** The document being edited, so `[[` does not offer a link to itself. */+ currentDocId?: string | nulldocs?: LinkableDoc[]devices?: LinkableDevice[]}@@ -64,6 +67,7 @@ export function DocEditor({dirty,saving,deviceId,+ currentDocId,docs = [],devices = [],}: Props) {@@ -78,6 +82,13 @@ export function DocEditor({const [slashOpen, setSlashOpen] = useState(false)const [slashQuery, setSlashQuery] = useState('')const [inserting, setInserting] = useState(false)+ // The `[[` picker. Same machinery as the slash menu, anchored on the first+ // of the two brackets, because both of them are replaced by the finished link.+ const linkIndex = useRef<number | null>(null)+ const linkMenu = useRef<HTMLDivElement>(null)+ const [linkOpen, setLinkOpen] = useState(false)+ const [linkQuery, setLinkQuery] = useState('')+ const [linkAt, setLinkAt] = useState<Placement | null>(null)// Null until measured: the menu is rendered to be measured, and placing it// needs its height, so the first paint would otherwise flash at 0,0.const [slashAt, setSlashAt] = useState<Placement | null>(null)@@ -104,21 +115,61 @@ export function DocEditor({return needle ? commands.filter((c) => c.label.toLowerCase().includes(needle)) : commands}, [commands, slashQuery])+ /**+ * What `[[` can point at: the documents that exist.+ *+ * Only documents, deliberately. A device with no document is not a link+ * target yet — writing `[[device:nas-01]]` at one would render red — and+ * creating a document from inside an unsaved editor is a different feature.+ * A device that *is* documented is in this list through its document.+ *+ * The text inserted is the title when no other document shares it, and+ * `[[doc:<id>]]` when one does: a bare link resolves by title, so an+ * ambiguous one would silently point at whichever came first.+ */+ const linkTargets = useMemo<(EditorMenuItem & { text: string })[]>(() => {+ const byTitle = new Map<string, number>()+ for (const doc of docs) {+ const key = doc.title.toLowerCase()+ byTitle.set(key, (byTitle.get(key) ?? 0) + 1)+ }+ const deviceLabels = new Map(devices.map((device) => [device.id, device.label]))+ return docs.filter((doc) => doc.id !== currentDocId).map((doc) => {+ const unique = (byTitle.get(doc.title.toLowerCase()) ?? 0) < 2+ const device = doc.device_id ? deviceLabels.get(doc.device_id) : undefined+ return {+ id: doc.id,+ label: doc.title,+ hint: device ? `Device · ${device}` : doc.slug,+ text: unique ? `[[${doc.title}]]` : `[[doc:${doc.id}]]`,+ }+ })+ }, [docs, devices, currentDocId])++ const linkVisible = useMemo(() => {+ const needle = linkQuery.trim().toLowerCase()+ if (!needle) return linkTargets.slice(0, 50)+ return linkTargets+ .filter((item) => `${item.label} ${item.hint}`.toLowerCase().includes(needle))+ .slice(0, 50)+ }, [linkTargets, linkQuery])+/*** Insert `text`, either at the live caret or over the `/` at `replacing`.*- * The `/` is the only thing the menu ever put in the document — the query was- * typed into the menu's own field — so replacing it consumes exactly that one- * character. The index is passed in rather than read from the textarea:+ * The `/` is the only thing the slash menu ever put in the document — the+ * query was typed into the menu's own field — so replacing it consumes+ * exactly that one character; the `[[` picker passes 2 for its two brackets.+ * The index is passed in rather than read from the textarea:* opening the menu moves focus, which freezes the textarea's selection* wherever it happened to be, and every later keystroke widens the gap.*/const insertAtCursor = useCallback(- (text: string, replacing: number | null) => {+ (text: string, replacing: number | null, replacedLength = 1) => {const el = textarea.currentif (!el) returnconst start = replacing ?? el.selectionStart- const end = replacing === null ? el.selectionEnd : Math.min(replacing + 1, body.length)+ const end = replacing === null ? el.selectionEnd : Math.min(replacing + replacedLength, body.length)const next = `${body.slice(0, start)}${text}${body.slice(end)}`history.record('edit')onChange(next)@@ -147,6 +198,26 @@ export function DocEditor({[insertAtCursor],)+ const runLink = useCallback(+ (item: EditorMenuItem) => {+ const target = linkTargets.find((entry) => entry.id === item.id)+ // Read before closing: the reset effect clears the index.+ const at = linkIndex.current+ setLinkOpen(false)+ setLinkQuery('')+ if (target) insertAtCursor(target.text, at)+ },+ [insertAtCursor, linkTargets],+ )++ /** Close without picking, giving back the bracket the menu held. */+ const cancelLink = useCallback(() => {+ const at = linkIndex.current+ setLinkOpen(false)+ setLinkQuery('')+ if (at !== null) insertAtCursor('[[', at)+ }, [insertAtCursor])+const wrapSelection = useCallback((before: string, after = before) => {const el = textarea.current@@ -185,15 +256,34 @@ export function DocEditor({return}if (event.key === 'Escape') {- if (slashOpen) {+ if (slashOpen || linkOpen) {event.preventDefault()setSlashOpen(false)+ if (linkOpen) cancelLink()return}event.preventDefault()onCancel()return}+ if (event.key === '[') {+ const el = event.currentTarget+ // Typing over a selection replaces it; that is not an opening bracket.+ if (el.selectionStart !== el.selectionEnd) return+ if (!body.slice(0, el.selectionStart).endsWith('[')) return+ // The second bracket is swallowed on purpose. Opening the menu moves+ // focus to its filter field, and a character typed as focus moves is not+ // reliably delivered to either box — so the body is left holding exactly+ // one `[`, which is what the insertion replaces. Cancelling puts the+ // second one back, so what the user typed survives either way.+ event.preventDefault()+ caretRef.current = caretPoint(el, el.selectionStart)+ linkIndex.current = el.selectionStart - 1+ setSlashOpen(false)+ setLinkOpen(true)+ setLinkQuery('')+ return+ }if (event.key === '/') {const el = event.currentTargetconst before = body.slice(0, el.selectionStart)@@ -217,6 +307,29 @@ export function DocEditor({}}, [slashOpen])+ useEffect(() => {+ if (!linkOpen) {+ setLinkQuery('')+ setLinkAt(null)+ linkIndex.current = null+ }+ }, [linkOpen])++ useEffect(() => {+ if (!linkOpen) return+ const caret = caretRef.current+ const paneEl = pane.current+ const menuEl = linkMenu.current+ if (!caret || !paneEl || !menuEl) return+ setLinkAt(+ placeMenu({+ caret,+ pane: { width: paneEl.clientWidth, height: paneEl.clientHeight },+ menu: { width: menuEl.offsetWidth, height: menuEl.offsetHeight },+ }),+ )+ }, [linkOpen, linkVisible.length])+// Place the menu once it (and the filtered list) have a height. Re-runs as the// query narrows the list, so a menu that shrank stops hanging off the bottom.useEffect(() => {@@ -256,7 +369,8 @@ export function DocEditor({<Link2 /></Button><span className="ml-2 text-[10px] text-muted-foreground/70">- Type <kbd className="rounded border border-border px-1">/</kbd> on a new line to insert+ <kbd className="rounded border border-border px-1">/</kbd> on a new line to insert,{' '}+ <kbd className="rounded border border-border px-1">[[</kbd> to link</span><div className="ml-auto flex items-center gap-2">{dirty && <span className="text-[10px] text-muted-foreground">Unsaved</span>}@@ -284,48 +398,36 @@ export function DocEditor({className="h-full w-full resize-none bg-transparent p-4 font-mono text-xs leading-relaxed outline-none"/>{slashOpen && (- <div- ref={menu}- style={slashAt ? { top: slashAt.top, left: slashAt.left } : undefined}- className={cn(- 'absolute z-20 w-72 overflow-hidden rounded-lg border border-border bg-popover shadow-lg',- // Hidden, not unmounted, for the frame it takes to measure it —- // placing it needs the height it only has once rendered.- !slashAt && 'invisible',- )}- >- <input- autoFocus- value={slashQuery}- onChange={(event) => setSlashQuery(event.target.value)}- onKeyDown={(event) => {- if (event.key === 'Escape') setSlashOpen(false)- if (event.key === 'Enter' && visible[0]) {- event.preventDefault()- void runCommand(visible[0])- }- }}- placeholder="Insert…"- aria-label="Insert a block"- className="w-full border-b border-border bg-transparent px-3 py-2 text-xs outline-none"- />- <div className="max-h-56 overflow-y-auto">- {visible.length === 0 && (- <p className="px-3 py-2 text-xs text-muted-foreground">Nothing matches.</p>- )}- {visible.map((command) => (- <button- key={command.id}- type="button"- onClick={() => void runCommand(command)}- className="flex w-full cursor-pointer flex-col items-start px-3 py-1.5 text-left hover:bg-muted"- >- <span className="font-mono text-xs">{command.label}</span>- <span className="text-[10px] text-muted-foreground">{command.hint}</span>- </button>- ))}- </div>- </div>+ <EditorMenu+ items={visible}+ query={slashQuery}+ onQuery={setSlashQuery}+ placeholder="Insert…"+ ariaLabel="Insert a block"+ emptyText="Nothing matches."+ at={slashAt}+ menuRef={menu}+ monoLabels+ onPick={(item) => {+ const command = commands.find((entry) => entry.id === item.id)+ if (command) void runCommand(command)+ }}+ onClose={() => setSlashOpen(false)}+ />+ )}+ {linkOpen && (+ <EditorMenu+ items={linkVisible}+ query={linkQuery}+ onQuery={setLinkQuery}+ placeholder="Link to…"+ ariaLabel="Link to a document"+ emptyText="No document to link to yet."+ at={linkAt}+ menuRef={linkMenu}+ onPick={runLink}+ onClose={cancelLink}+ />)}{inserting && (<span className="absolute bottom-2 right-3 text-[10px] text-muted-foreground">Inserting…</span>diff --git a/frontend/src/documentation/components/DocumentationView.tsx b/frontend/src/documentation/components/DocumentationView.tsxindex 28ad085..0c25384 100644--- a/frontend/src/documentation/components/DocumentationView.tsx+++ b/frontend/src/documentation/components/DocumentationView.tsx@@ -552,6 +552,7 @@ export function DocumentationView() {dirty={dirty}saving={saving}deviceId={openDoc.device_id}+ currentDocId={openDoc.id}docs={docs}devices={linkableDevices}/>diff --git a/frontend/src/documentation/components/EditorMenu.tsx b/frontend/src/documentation/components/EditorMenu.tsxnew file mode 100644index 0000000..9dc4570--- /dev/null+++ b/frontend/src/documentation/components/EditorMenu.tsx@@ -0,0 +1,92 @@+import { type RefObject } from 'react'++import { cn } from '@/lib/utils'+import type { Placement } from '@/documentation/caret'++/**+ * The popover the editor opens at the caret.+ *+ * Extracted when the `[[` link picker joined the `/` insert menu: same shape —+ * a filter field over a labelled list, placed at the caret and measured before+ * it is shown — and two copies of that would have drifted apart.+ */++export interface EditorMenuItem {+ id: string+ label: string+ hint: string+}++interface Props {+ items: EditorMenuItem[]+ query: string+ onQuery: (query: string) => void+ placeholder: string+ /** Names the filter field, since the popover has no visible title. */+ ariaLabel: string+ emptyText: string+ /** Null until measured — the menu is rendered hidden to get its height. */+ at: Placement | null+ menuRef: RefObject<HTMLDivElement | null>+ /** Monospace labels suit the `/command` list; document titles are prose. */+ monoLabels?: boolean+ onPick: (item: EditorMenuItem) => void+ onClose: () => void+}++export function EditorMenu({+ items,+ query,+ onQuery,+ placeholder,+ ariaLabel,+ emptyText,+ at,+ menuRef,+ monoLabels = false,+ onPick,+ onClose,+}: Props) {+ return (+ <div+ ref={menuRef}+ style={at ? { top: at.top, left: at.left } : undefined}+ className={cn(+ 'absolute z-20 w-72 overflow-hidden rounded-lg border border-border bg-popover shadow-lg',+ // Hidden, not unmounted, for the frame it takes to measure it —+ // placing it needs the height it only has once rendered.+ !at && 'invisible',+ )}+ >+ <input+ autoFocus+ value={query}+ onChange={(event) => onQuery(event.target.value)}+ onKeyDown={(event) => {+ if (event.key === 'Escape') onClose()+ if (event.key === 'Enter' && items[0]) {+ event.preventDefault()+ onPick(items[0])+ }+ }}+ placeholder={placeholder}+ aria-label={ariaLabel}+ className="w-full border-b border-border bg-transparent px-3 py-2 text-xs outline-none"+ />+ <div className="max-h-56 overflow-y-auto">+ {items.length === 0 && <p className="px-3 py-2 text-xs text-muted-foreground">{emptyText}</p>}+ {items.map((item) => (+ <button+ key={item.id}+ type="button"+ onClick={() => onPick(item)}+ className="flex w-full cursor-pointer flex-col items-start px-3 py-1.5 text-left hover:bg-muted"+ >+ <span className={cn('text-xs', monoLabels ? 'font-mono' : 'truncate')}>{item.label}</span>+ <span className="text-[10px] text-muted-foreground">{item.hint}</span>+ </button>+ ))}+ </div>+ </div>+ )+}