diff --git a/ui/src/context/BreadcrumbContext.test.tsx b/ui/src/context/BreadcrumbContext.test.tsx index 19516c92c4c..bfbb7140067 100644 --- a/ui/src/context/BreadcrumbContext.test.tsx +++ b/ui/src/context/BreadcrumbContext.test.tsx @@ -115,8 +115,85 @@ describe("BreadcrumbContext", () => { ).toBe("PAP-3515 • Issues • Anachronist Wiki • Paperclip"); }); + it("includes a task identifier before its name without changing breadcrumb order", () => { + expect( + buildDocumentTitle( + [{ label: "Issues" }, { label: "First task prompt", identifier: "PAP-1204" }], + "Anachronist Wiki", + ), + ).toBe("PAP-1204 — First task prompt • Issues • Anachronist Wiki • Paperclip"); + expect(buildDocumentTitle([{ label: "First task prompt", identifier: " PAP-1204 " }])).toBe( + "PAP-1204 — First task prompt • Paperclip", + ); + }); + + it("keeps titles unchanged when identifiers are missing or blank", () => { + expect(buildDocumentTitle([{ label: "First task prompt" }], "Anachronist Wiki")).toBe( + "First task prompt • Anachronist Wiki • Paperclip", + ); + expect(buildDocumentTitle([{ label: "First task prompt", identifier: " " }])).toBe( + "First task prompt • Paperclip", + ); + }); + + it("does not repeat an identifier that is already the label", () => { + expect(buildDocumentTitle([{ label: "PAP-1204", identifier: "PAP-1204" }])).toBe( + "PAP-1204 • Paperclip", + ); + expect(buildDocumentTitle([{ label: "PAP-1204", identifier: " PAP-1204 " }])).toBe( + "PAP-1204 • Paperclip", + ); + }); + it("omits blank company names from page titles", () => { expect(buildDocumentTitle([{ label: "Inbox" }], " ")).toBe("Inbox • Paperclip"); expect(buildDocumentTitle([], null)).toBe("Paperclip"); }); + + it("updates the document title when tasks and identifiers change", () => { + let updateBreadcrumbs: ReturnType["setBreadcrumbs"] | null = null; + + function TestConsumer() { + updateBreadcrumbs = useBreadcrumbs().setBreadcrumbs; + return null; + } + + act(() => { + root.render( + + + , + ); + }); + expect(document.title).toBe("Anachronist Wiki • Paperclip"); + + act(() => { + updateBreadcrumbs?.([{ label: "First task prompt", identifier: "PAP-1204" }]); + }); + expect(document.title).toBe("PAP-1204 — First task prompt • Anachronist Wiki • Paperclip"); + + act(() => { + updateBreadcrumbs?.([{ label: "First task prompt", identifier: "PAP-1205" }]); + }); + expect(document.title).toBe("PAP-1205 — First task prompt • Anachronist Wiki • Paperclip"); + + act(() => { + updateBreadcrumbs?.([{ label: "Second task prompt", identifier: "PAP-1206" }]); + }); + expect(document.title).toBe("PAP-1206 — Second task prompt • Anachronist Wiki • Paperclip"); + + act(() => { + root.render( + + + , + ); + }); + expect(document.title).toBe("PAP-1206 — Second task prompt • Another Company • Paperclip"); + + act(() => { + updateBreadcrumbs?.([]); + }); + expect(document.title).toBe("Another Company • Paperclip"); + }); }); diff --git a/ui/src/context/BreadcrumbContext.tsx b/ui/src/context/BreadcrumbContext.tsx index 377ca9b3efa..92455d79a97 100644 --- a/ui/src/context/BreadcrumbContext.tsx +++ b/ui/src/context/BreadcrumbContext.tsx @@ -73,7 +73,12 @@ function breadcrumbsEqual(left: Breadcrumb[], right: Breadcrumb[]) { export function buildDocumentTitle(breadcrumbs: Breadcrumb[], companyName?: string | null) { const pageParts = breadcrumbs.length === 0 ? [] - : [...breadcrumbs].reverse().map((breadcrumb) => breadcrumb.label); + : [...breadcrumbs].reverse().map((breadcrumb) => { + const identifier = breadcrumb.identifier?.trim(); + return identifier && identifier !== breadcrumb.label.trim() + ? `${identifier} — ${breadcrumb.label}` + : breadcrumb.label; + }); const companyPart = companyName?.trim() ? [companyName.trim()] : []; const parts = [...pageParts, ...companyPart, "Paperclip"]; return parts.join(" • ");