Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring
TypeScript diagramhomelabhomelab-infrastructure-observabilitynetworkproxmox-veself-hostedzigbeezwavezwave-js
feat(docs): copy the read-only documentation link
Pouzor7e8a38059bdc
diff --git a/frontend/src/documentation/__tests__/DocsShareLink.test.tsx b/frontend/src/documentation/__tests__/DocsShareLink.test.tsxnew file mode 100644index 0000000..68b32e9--- /dev/null+++ b/frontend/src/documentation/__tests__/DocsShareLink.test.tsx@@ -0,0 +1,91 @@+/**+ * The read-only documentation link, offered in the Documentation footer.+ *+ * The key lives in `.env` and is admin-only, so the app has to ask the server+ * for it: with the feature off there is no button at all, and with it on the+ * button has to hand over a link a reader can actually open.+ */+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'++import { docsviewApi } from '@/api/client'+import { copyToClipboard } from '@/utils/clipboard'+import { DocumentationView } from '../components/DocumentationView'+import { useDocsStore } from '../store'++vi.mock('sonner', async () => (await import('@/test/mocks')).mockSonner())+vi.mock('@/utils/clipboard', () => ({ copyToClipboard: vi.fn() }))++vi.mock('@/api/client', () => ({+ documentsApi: {+ list: vi.fn().mockResolvedValue({ data: [] }),+ coverage: vi.fn().mockResolvedValue({+ data: { devices: 0, documented: 0, header_only: 0, notes_unmigrated: 0 },+ }),+ export: vi.fn(),+ },+ scanApi: { pending: vi.fn().mockResolvedValue({ data: [] }) },+ docsviewApi: { getConfig: vi.fn() },+}))++const getConfig = vi.mocked(docsviewApi.getConfig)+const copy = vi.mocked(copyToClipboard)++beforeEach(() => {+ vi.clearAllMocks()+ copy.mockResolvedValue(true)+ useDocsStore.setState({ docs: [], loaded: false, openDoc: null, filter: '' })+})++afterEach(cleanup)++async function renderView() {+ render(<DocumentationView />)+ await waitFor(() => expect(useDocsStore.getState().loaded).toBe(true))+}++describe('the read-only documentation link', () => {+ it('is not offered while DOCS_VIEW_KEY is unset', async () => {+ getConfig.mockResolvedValue({ data: { enabled: false, key: null } } as never)+ await renderView()++ await waitFor(() => expect(getConfig).toHaveBeenCalled())+ expect(screen.queryByText('Share link')).not.toBeInTheDocument()+ })++ it('copies the link a reader opens, key and all', async () => {+ getConfig.mockResolvedValue({ data: { enabled: true, key: 'a key' } } as never)+ await renderView()++ fireEvent.click(await screen.findByText('Share link'))++ // The key is escaped: it is whatever the admin typed into .env.+ await waitFor(() =>+ expect(copy).toHaveBeenCalledWith(`${window.location.origin}/docs?key=a%20key`),+ )+ const { toast } = await import('sonner')+ expect(toast.success).toHaveBeenCalledWith('Read-only link copied')+ })++ it('says so when the clipboard refuses', async () => {+ getConfig.mockResolvedValue({ data: { enabled: true, key: 'a-key' } } as never)+ copy.mockResolvedValue(false)+ await renderView()++ fireEvent.click(await screen.findByText('Share link'))++ const { toast } = await import('sonner')+ await waitFor(() => expect(toast.error).toHaveBeenCalledWith('Could not copy the link'))+ })++ // The section has to keep working for the logged-in user whatever this+ // answers: it is a footer button, not a precondition.+ it('stays quiet when the server cannot be asked', async () => {+ getConfig.mockRejectedValue(new Error('500'))+ await renderView()++ await waitFor(() => expect(getConfig).toHaveBeenCalled())+ expect(screen.queryByText('Share link')).not.toBeInTheDocument()+ expect(screen.getByText(/Export all|Exporting/)).toBeInTheDocument()+ })+})diff --git a/frontend/src/documentation/__tests__/ExportAll.test.tsx b/frontend/src/documentation/__tests__/ExportAll.test.tsxindex c970f7b..4a91def 100644--- a/frontend/src/documentation/__tests__/ExportAll.test.tsx+++ b/frontend/src/documentation/__tests__/ExportAll.test.tsx@@ -24,6 +24,8 @@ vi.mock('@/api/client', () => ({export: vi.fn(),},scanApi: { pending: vi.fn().mockResolvedValue({ data: [] }) },+ // The footer asks whether the read-only link is configured; off, so no button.+ docsviewApi: { getConfig: vi.fn().mockResolvedValue({ data: { enabled: false, key: null } }) },}))const exportAll = vi.mocked(documentsApi.export)diff --git a/frontend/src/documentation/__tests__/TourAnchors.test.tsx b/frontend/src/documentation/__tests__/TourAnchors.test.tsxindex 3899e62..832a42f 100644--- a/frontend/src/documentation/__tests__/TourAnchors.test.tsx+++ b/frontend/src/documentation/__tests__/TourAnchors.test.tsx@@ -20,6 +20,8 @@ vi.mock('@/api/client', () => ({}),},scanApi: { pending: vi.fn().mockResolvedValue({ data: [] }) },+ // The footer asks whether the read-only link is configured; off, so no button.+ docsviewApi: { getConfig: vi.fn().mockResolvedValue({ data: { enabled: false, key: null } }) },}))beforeEach(() => {diff --git a/frontend/src/documentation/components/DocumentationView.tsx b/frontend/src/documentation/components/DocumentationView.tsxindex dd4b597..f85970a 100644--- a/frontend/src/documentation/components/DocumentationView.tsx+++ b/frontend/src/documentation/components/DocumentationView.tsx@@ -1,14 +1,16 @@import { useCallback, useEffect, useMemo, useRef, useState } from 'react'-import { Archive, BookOpen, ChevronDown, ChevronRight, FilePlus, FolderPlus, Search, X } from 'lucide-react'+import { Archive, BookOpen, ChevronDown, ChevronRight, FilePlus, FolderPlus, Link2, Search, X } from 'lucide-react'import { toast } from 'sonner'-import { scanApi } from '@/api/client'+import { docsviewApi, scanApi } from '@/api/client'import { Button } from '@/components/ui/button'import { Input } from '@/components/ui/input'import { useCanvasStore } from '@/stores/canvasStore'import { useDesignStore } from '@/stores/designStore'import type { InventoryEntry } from '@/types'import { cn } from '@/lib/utils'+import { withBase } from '@/utils/basePath'+import { copyToClipboard } from '@/utils/clipboard'import { formatRelative } from '@/utils/timeFormat'import { downloadAllDocs, downloadDoc } from '../export'import { isOverdue } from '../frontmatter'@@ -301,6 +303,24 @@ export function DocumentationView() {toast.success(created === 1 ? '1 note became a document' : `${created} notes became documents`)}, [loadDocs, scaffold])+ // The read-only link to this documentation, when DOCS_VIEW_KEY is set. The+ // key is admin-only — the app asks the server for it rather than being told+ // at build time — and with the feature off there is simply no button.+ const [docsLink, setDocsLink] = useState<string | null>(null)+ useEffect(() => {+ if (STANDALONE) return+ docsviewApi+ .getConfig()+ .then(({ data }) =>+ setDocsLink(+ data.enabled && data.key+ ? `${window.location.origin}${withBase('docs')}?key=${encodeURIComponent(data.key)}`+ : null,+ ),+ )+ .catch(() => setDocsLink(null))+ }, [])+// Drag-to-resize the tree pane. Width lives in localStorage, per viewer.const dragging = useRef(false)useEffect(() => {@@ -486,6 +506,21 @@ export function DocumentationView() {<Archive size={12} />{exporting ? 'Exporting…' : 'Export all'}</Button>+ {docsLink && (+ <Button+ size="xs"+ variant="ghost"+ className="cursor-pointer gap-1 px-1.5"+ title="Copy the read-only link — anyone holding it reads every document"+ onClick={async () => {+ if (await copyToClipboard(docsLink)) toast.success('Read-only link copied')+ else toast.error('Could not copy the link')+ }}+ >+ <Link2 size={12} />+ Share link+ </Button>+ )}{coverage && (<spanclassName="ml-auto text-[10px] tabular-nums text-muted-foreground/70"