page-title.ts46 lines · main
| 1 | export interface StudioPageTitleParts { |
| 2 | entity?: string |
| 3 | section?: string |
| 4 | surface?: string |
| 5 | project?: string |
| 6 | org?: string |
| 7 | brand?: string |
| 8 | } |
| 9 | |
| 10 | export const STUDIO_PAGE_TITLE_SEPARATOR = ' | ' |
| 11 | const MAX_SEGMENT_LENGTH = 60 |
| 12 | |
| 13 | const normalizeTitleSegment = (value?: string) => { |
| 14 | if (value === undefined) return undefined |
| 15 | |
| 16 | const normalized = value.trim().replace(/\s+/g, ' ') |
| 17 | if (normalized.length === 0) return undefined |
| 18 | |
| 19 | if (normalized.length <= MAX_SEGMENT_LENGTH) return normalized |
| 20 | return `${normalized.slice(0, MAX_SEGMENT_LENGTH - 1).trimEnd()}…` |
| 21 | } |
| 22 | |
| 23 | export const buildStudioPageTitle = (parts: StudioPageTitleParts) => { |
| 24 | const orderedParts = [ |
| 25 | parts.entity, |
| 26 | parts.section, |
| 27 | parts.surface, |
| 28 | parts.project, |
| 29 | parts.org, |
| 30 | parts.brand, |
| 31 | ] |
| 32 | |
| 33 | const segments: string[] = [] |
| 34 | |
| 35 | orderedParts.forEach((part) => { |
| 36 | const segment = normalizeTitleSegment(part) |
| 37 | if (!segment) return |
| 38 | |
| 39 | const lastSegment = segments[segments.length - 1] |
| 40 | if (lastSegment !== undefined && lastSegment.toLowerCase() === segment.toLowerCase()) return |
| 41 | |
| 42 | segments.push(segment) |
| 43 | }) |
| 44 | |
| 45 | return segments.join(STUDIO_PAGE_TITLE_SEPARATOR) |
| 46 | } |