From 7c9cfcff26699f4e371d8025c9662e40f8560b2e Mon Sep 17 00:00:00 2001 From: Isaac Mann Date: Thu, 21 Nov 2024 15:33:21 -0500 Subject: [PATCH] fix(nx-dev): breadcrumb casing --- .../src/lib/documents.api.ts | 21 ++++ .../feature-doc-viewer/src/lib/doc-viewer.tsx | 2 +- .../src/lib/documents.models.ts | 1 + nx-dev/ui-common/src/lib/breadcrumbs.tsx | 98 ++++++++++++++----- 4 files changed, 95 insertions(+), 27 deletions(-) diff --git a/nx-dev/data-access-documents/src/lib/documents.api.ts b/nx-dev/data-access-documents/src/lib/documents.api.ts index 5b8c6f5c60d00..5cd441d4e76a5 100644 --- a/nx-dev/data-access-documents/src/lib/documents.api.ts +++ b/nx-dev/data-access-documents/src/lib/documents.api.ts @@ -97,6 +97,27 @@ export class DocumentsApi { name: document.name, mediaImage: document.mediaImage || '', relatedDocuments: this.getRelatedDocuments(document.tags), + parentDocuments: path.map((segment, index): RelatedDocument => { + const parentPath = path.slice(0, index + 1).join('/'); + const parentDocument = + this.manifest[this.getManifestKey(parentPath)] || null; + if (!parentDocument) { + return { + id: segment, + name: '', + description: '', + file: '', + path: '/' + path.slice(0, index + 1).join('/'), + }; + } + return { + id: parentDocument.id, + name: parentDocument.name, + description: parentDocument.description, + file: parentDocument.file, + path: parentDocument.path, + }; + }), tags: document.tags, }; } diff --git a/nx-dev/feature-doc-viewer/src/lib/doc-viewer.tsx b/nx-dev/feature-doc-viewer/src/lib/doc-viewer.tsx index b382caaffce97..97d7275cfcabb 100644 --- a/nx-dev/feature-doc-viewer/src/lib/doc-viewer.tsx +++ b/nx-dev/feature-doc-viewer/src/lib/doc-viewer.tsx @@ -101,7 +101,7 @@ export function DocViewer({
- +
{/*MAIN CONTENT*/} diff --git a/nx-dev/models-document/src/lib/documents.models.ts b/nx-dev/models-document/src/lib/documents.models.ts index 6961a22eb4f06..cb6433d9cd98f 100644 --- a/nx-dev/models-document/src/lib/documents.models.ts +++ b/nx-dev/models-document/src/lib/documents.models.ts @@ -29,6 +29,7 @@ export interface ProcessedDocument { id: string; name: string; relatedDocuments: Record; + parentDocuments?: RelatedDocument[]; tags: string[]; } diff --git a/nx-dev/ui-common/src/lib/breadcrumbs.tsx b/nx-dev/ui-common/src/lib/breadcrumbs.tsx index 54b10e84f3acc..71aef1949f318 100644 --- a/nx-dev/ui-common/src/lib/breadcrumbs.tsx +++ b/nx-dev/ui-common/src/lib/breadcrumbs.tsx @@ -1,25 +1,71 @@ import { ChevronRightIcon } from '@heroicons/react/24/solid'; +import { ProcessedDocument } from '@nx/nx-dev/models-document'; import classNames from 'classnames'; -export function Breadcrumbs({ path }: { path: string }): JSX.Element { - const cleanedPath = path.includes('?') - ? path.slice(0, path.indexOf('?')) - : path; - const pages = [ - ...cleanedPath - .split('/') - .filter(Boolean) - .map((segment, index, segments) => ({ - name: segment.includes('#') - ? segment.slice(0, segment.indexOf('#')) - : segment, - // We do not have dedicated page view for executors & generators - href: '/' + segments.slice(0, index + 1).join('/'), - current: '/' + segments.slice(0, index + 1).join('/') === cleanedPath, - })), - ]; - - if (pages.length === 1) { +interface Crumb { + id: string; + name: string; + href: string; + current: boolean; +} + +const sectionNames: Record = { + ci: 'CI', + 'extending-nx': 'Extending Nx', + 'nx-api': 'Nx API', +}; + +const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1); + +export function Breadcrumbs({ + document, + path, +}: { + document?: ProcessedDocument; + path?: string; +}): JSX.Element { + let crumbs: Crumb[] = []; + + if (path) { + const cleanedPath = path.includes('?') + ? path.slice(0, path.indexOf('?')) + : path; + crumbs = [ + ...cleanedPath + .split('/') + .filter(Boolean) + .map((segment, index, segments) => { + const strippedName = segment.includes('#') + ? segment.slice(0, segment.indexOf('#')) + : segment; + const name = + sectionNames[strippedName] || + strippedName.split('-').map(capitalize).join(' '); + return { + id: segment, + name, + // We do not have dedicated page view for executors & generators + href: '/' + segments.slice(0, index + 1).join('/'), + current: + '/' + segments.slice(0, index + 1).join('/') === cleanedPath, + }; + }), + ]; + } + + if (document && document.parentDocuments) { + crumbs = document.parentDocuments.map((parentDocument, index) => ({ + id: parentDocument.id, + name: + parentDocument.name || + sectionNames[parentDocument.id] || + parentDocument.id.split('-').map(capitalize).join(' '), + href: parentDocument.path, + current: index + 1 === document.parentDocuments?.length, + })); + } + + if (crumbs.length < 2) { return <>; } @@ -27,8 +73,8 @@ export function Breadcrumbs({ path }: { path: string }): JSX.Element {