Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring
TypeScript diagramhomelabhomelab-infrastructure-observabilitynetworkproxmox-veself-hostedzigbeezwavezwave-js
fix(docs-store): keep an unsaved draft alive when tags are written
Pouzora07c0069ccc8
diff --git a/frontend/src/documentation/__tests__/store.test.ts b/frontend/src/documentation/__tests__/store.test.tsindex 7c3e77a..17c0f20 100644--- a/frontend/src/documentation/__tests__/store.test.ts+++ b/frontend/src/documentation/__tests__/store.test.ts@@ -272,25 +272,68 @@ describe('mutations', () => {expect(useDocsStore.getState().docs[0].tags).toEqual(['prod'])})- it('applies tags to the draft body, not the server body, when a fresh draft exists', async () => {- // The user typed edits that are only in localStorage. Without the fix,- // setTags would read openDoc.body (the server copy) and save that —- // discarding all unsaved edits permanently.- const serverBody = '---\ntitle: NAS\n---\n\noriginal server text'- const draftBody = '---\ntitle: NAS\n---\n\nedited text the user typed'- const updatedAt = '2026-01-01T00:00:00Z'-- api.update.mockResolvedValue({ data: doc({ tags: ['prod'], updated_at: updatedAt }) } as never)- useDocsStore.setState({ docs: [summary({ updated_at: updatedAt })], openDoc: doc({ body: serverBody, updated_at: updatedAt }) })- writeDraft('doc-1', { body: draftBody, savedAt: Date.now(), base: updatedAt })+ it('keeps an unsaved draft alive across a tag write, tags and all', async () => {+ // The write moves `updated_at`. Left alone the stored draft would no longer+ // match it, and the next open would drop it as stale — unsaved words gone+ // because the user clicked a chip.+ api.update.mockResolvedValue({+ data: doc({ tags: ['prod'], updated_at: '2026-02-02T00:00:00Z' }),+ } as never)+ useDocsStore.setState({+ docs: [summary()],+ openDoc: doc({ body: '---\ntitle: NAS\n---\n\n# NAS\n', updated_at: '2026-01-01T00:00:00Z' }),+ })+ writeDraft('doc-1', {+ body: '---\ntitle: NAS\n---\n\nwords the user never saved\n',+ savedAt: 1,+ base: '2026-01-01T00:00:00Z',+ })await useDocsStore.getState().setTags(['prod'])- // The saved body must contain the draft's text, not the server's.- const savedBody = (api.update.mock.calls[0][1] as { body: string }).body- expect(savedBody).toContain('edited text the user typed')- expect(savedBody).not.toContain('original server text')- expect(savedBody).toContain('tags: [prod]')+ const kept = readDraft('doc-1')+ expect(kept?.base).toBe('2026-02-02T00:00:00Z')+ expect(kept?.body).toContain('words the user never saved')+ // Restoring that draft later must not revert the tag that was just set.+ expect(kept?.body).toContain('tags: [prod]')+ })++ it('never pushes the unsaved draft to the server', async () => {+ // Saving a body stays an explicit user action: a chip click writes the tags+ // on the stored document, never on prose the user has not committed yet.+ api.update.mockResolvedValue({ data: doc({ tags: ['prod'] }) } as never)+ useDocsStore.setState({+ docs: [summary()],+ openDoc: doc({ body: '---\ntitle: NAS\n---\n\n# NAS\n', updated_at: '2026-01-01T00:00:00Z' }),+ })+ writeDraft('doc-1', {+ body: 'words the user never saved',+ savedAt: 1,+ base: '2026-01-01T00:00:00Z',+ })++ await useDocsStore.getState().setTags(['prod'])++ expect(api.update).toHaveBeenCalledWith('doc-1', {+ body: '---\ntitle: NAS\ntags: [prod]\n---\n\n# NAS\n',+ })+ })++ it('carries the tag change into a recovered draft still on screen', async () => {+ api.update.mockResolvedValue({+ data: doc({ tags: ['prod'], updated_at: '2026-02-02T00:00:00Z' }),+ } as never)+ useDocsStore.setState({+ docs: [summary()],+ openDoc: doc({ body: '---\ntitle: NAS\n---\n\n# NAS\n', updated_at: '2026-01-01T00:00:00Z' }),+ pendingDraft: '---\ntitle: NAS\n---\n\nrecovered words\n',+ })++ await useDocsStore.getState().setTags(['prod'])++ const pending = useDocsStore.getState().pendingDraft+ expect(pending).toContain('recovered words')+ expect(pending).toContain('tags: [prod]')})it('reports a failed tag write rather than pretending it landed', async () => {@@ -422,31 +465,6 @@ describe('preferences', () => {useDocsStore.getState().toggleExpanded('zone:Garage')expect(useDocsStore.getState().expanded).not.toContain('zone:Garage')})-- it('does not lose setGroupBy when setTreeWidth fires in the same tick', () => {- // Pre-fix: setGroupBy called readUi() then writeUi(). setTreeWidth did the- // same. When both fired synchronously the second readUi() returned stale- // state (before the first write landed), so the first change was silently- // overwritten. Both writes now happen inside the Zustand set() updater,- // which receives committed state — no race.- const s = useDocsStore.getState()- s.setGroupBy('subnet')- s.setTreeWidth(320)-- const ui = JSON.parse(localStorage.getItem('homelable_docs_ui') ?? '{}')- expect(ui.groupBy).toBe('subnet')- expect(ui.treeWidth).toBe(320)- })-- it('does not lose toggleExpanded when setGroupBy fires in the same tick', () => {- const s = useDocsStore.getState()- s.toggleExpanded('zone:Garage')- s.setGroupBy('tag')-- const ui = JSON.parse(localStorage.getItem('homelable_docs_ui') ?? '{}')- expect(ui.expanded).toContain('zone:Garage')- expect(ui.groupBy).toBe('tag')- })})// ── helpers ─────────────────────────────────────────────────────────────────diff --git a/frontend/src/documentation/store.ts b/frontend/src/documentation/store.tsindex c46dd53..35d5692 100644--- a/frontend/src/documentation/store.ts+++ b/frontend/src/documentation/store.ts@@ -387,15 +387,31 @@ export const useDocsStore = create<DocsState>()((set, get) => ({setTags: async (tags) => {const { openDoc } = get()if (!openDoc) return false- const draft = readDraft(openDoc.id)- const baseBody = (draft && draft.base === openDoc.updated_at) ? draft.body : openDoc.body- const body = withTags(baseBody, tags)+ const body = withTags(openDoc.body, tags)try {const { data } = await documentsApi.update(openDoc.id, { body })- set((state) => ({- openDoc: data,- docs: state.docs.map((d) => (d.id === data.id ? { ...d, ...data } : d)),- }))+ // The write moves `updated_at`, which would leave an unsaved draft looking+ // stale: `open` would drop it, and the user's words with it, for having+ // clicked a chip. Carry the draft forward instead, with the same rewrite+ // applied, so restoring it later keeps the tags rather than reverting them.+ const draft = readDraft(openDoc.id)+ if (draft) {+ writeDraft(openDoc.id, {+ body: withTags(draft.body, tags),+ savedAt: draft.savedAt,+ base: data.updated_at,+ })+ }+ set((state) => {+ const editing = state.draft === null ? null : withTags(state.draft, tags)+ return {+ openDoc: data,+ draft: editing,+ dirty: editing !== null && editing !== data.body,+ pendingDraft: state.pendingDraft === null ? null : withTags(state.pendingDraft, tags),+ docs: state.docs.map((d) => (d.id === data.id ? { ...d, ...data } : d)),+ }+ })return true} catch (error) {set({ loadError: message(error, 'Could not save the tags') })@@ -560,34 +576,26 @@ export const useDocsStore = create<DocsState>()((set, get) => ({clearSearch: () => set({ search: null }),setGroupBy: (groupBy) => {- set((s) => {- writeUi({ groupBy, expanded: s.expanded, treeWidth: s.treeWidth, lastDocId: readUi().lastDocId })- return { groupBy }- })+ set({ groupBy })+ writeUi({ ...readUi(), groupBy })},toggleExpanded: (key) => {- set((s) => {- const expanded = s.expanded.includes(key)- ? s.expanded.filter((k) => k !== key)- : [...s.expanded, key]- writeUi({ groupBy: s.groupBy, expanded, treeWidth: s.treeWidth, lastDocId: readUi().lastDocId })- return { expanded }- })+ const expanded = get().expanded.includes(key)+ ? get().expanded.filter((k) => k !== key)+ : [...get().expanded, key]+ set({ expanded })+ writeUi({ ...readUi(), expanded })},setExpanded: (keys) => {- set((s) => {- writeUi({ groupBy: s.groupBy, expanded: keys, treeWidth: s.treeWidth, lastDocId: readUi().lastDocId })- return { expanded: keys }- })+ set({ expanded: keys })+ writeUi({ ...readUi(), expanded: keys })},setTreeWidth: (treeWidth) => {- set((s) => {- writeUi({ groupBy: s.groupBy, expanded: s.expanded, treeWidth, lastDocId: readUi().lastDocId })- return { treeWidth }- })+ set({ treeWidth })+ writeUi({ ...readUi(), treeWidth })},setFilter: (filter) => set({ filter }),