|
1
|
import i18n from "i18next" |
|
2
|
import { ReactNode, useMemo } from "react" |
|
3
|
import { Helmet } from "react-helmet-async" |
|
4
|
import { UIMatch, useMatches } from "react-router-dom" |
|
5
|
|
|
6
|
const DEFAULT_TITLE = "Medusa" |
|
7
|
|
|
8
|
export type RouteHandle = { |
|
9
|
breadcrumb?: (match?: UIMatch) => string | ReactNode |
|
10
|
/** |
|
11
|
* Resolves the document title for the route. |
|
12
|
* |
|
13
|
* Each route declares its own resolver, co-located with the route, so the |
|
14
|
* title logic lives next to the loader data shape it depends on (rather than |
|
15
|
* being centralised in this hook). Return `undefined` to let a parent route's |
|
16
|
* title or the default apply. |
|
17
|
*/ |
|
18
|
seo?: (match: UIMatch) => { title?: string } |
|
19
|
/** |
|
20
|
* A static label for the route, sourced from `defineRouteConfig` on custom |
|
21
|
* admin pages. When `translationNs` is set, the label is treated as an i18n |
|
22
|
* key and resolved against that namespace. Used for document title fallback. |
|
23
|
*/ |
|
24
|
label?: string |
|
25
|
translationNs?: string |
|
26
|
} |
|
27
|
|
|
28
|
/** |
|
29
|
* Resolves a single matched route to a title string, or `null` when the route |
|
30
|
* does not contribute a title. |
|
31
|
* |
|
32
|
* Resolution order: |
|
33
|
* 1. An explicit `seo` resolver (used by detail pages to surface entity names). |
|
34
|
* 2. A static `label` from `defineRouteConfig` (custom pages' sidebar label). |
|
35
|
* 3. A `breadcrumb` that returns a plain string (used by list/section pages). |
|
36
|
* |
|
37
|
* React-element breadcrumbs (rendered by detail pages) carry no extractable |
|
38
|
* text and are intentionally ignored here; those routes rely on `seo`. |
|
39
|
*/ |
|
40
|
export function getTitleFromMatch( |
|
41
|
match: UIMatch<unknown, RouteHandle> |
|
42
|
): string | null { |
|
43
|
const handle = match.handle |
|
44
|
|
|
45
|
if (handle?.seo) { |
|
46
|
try { |
|
47
|
const title = handle.seo(match)?.title |
|
48
|
if (typeof title === "string" && title.trim() !== "") { |
|
49
|
return title |
|
50
|
} |
|
51
|
} catch { |
|
52
|
// Fall through to the label / breadcrumb / default below. |
|
53
|
} |
|
54
|
} |
|
55
|
|
|
56
|
if (handle?.label?.trim()?.length) { |
|
57
|
try { |
|
58
|
const label = handle.translationNs |
|
59
|
? (i18n.t as any)(handle.label, { ns: handle.translationNs }) |
|
60
|
: handle.label |
|
61
|
if (label?.trim()?.length) { |
|
62
|
return label |
|
63
|
} |
|
64
|
} catch { |
|
65
|
// Fall through to the breadcrumb / default below. |
|
66
|
} |
|
67
|
} |
|
68
|
|
|
69
|
if (handle?.breadcrumb) { |
|
70
|
try { |
|
71
|
const result = handle.breadcrumb(match) |
|
72
|
if (typeof result === "string" && result.trim() !== "") { |
|
73
|
return result |
|
74
|
} |
|
75
|
} catch { |
|
76
|
// Ignore and let a parent route or the default apply. |
|
77
|
} |
|
78
|
} |
|
79
|
|
|
80
|
return null |
|
81
|
} |
|
82
|
|
|
83
|
/** |
|
84
|
* Hook that returns the document title based on the current route. |
|
85
|
* |
|
86
|
* It walks the matched routes from root to leaf and uses the most specific |
|
87
|
* (last) route that yields a title. The format is `"Page Title - Medusa"`, |
|
88
|
* falling back to `"Medusa"` when no route contributes a title. |
|
89
|
*/ |
|
90
|
export function useDocumentTitle() { |
|
91
|
const matches = useMatches() as UIMatch<unknown, RouteHandle>[] |
|
92
|
|
|
93
|
const title = useMemo(() => { |
|
94
|
let pageTitle = "" |
|
95
|
|
|
96
|
for (let indx = matches.length - 1; indx >= 0; indx--) { |
|
97
|
const resolvedTitle = getTitleFromMatch(matches[indx]) |
|
98
|
|
|
99
|
if (resolvedTitle) { |
|
100
|
pageTitle = resolvedTitle |
|
101
|
break |
|
102
|
} |
|
103
|
} |
|
104
|
|
|
105
|
if (!pageTitle) { |
|
106
|
return DEFAULT_TITLE |
|
107
|
} |
|
108
|
|
|
109
|
return `${pageTitle} - Medusa` |
|
110
|
}, [matches]) |
|
111
|
|
|
112
|
return title |
|
113
|
} |
|
114
|
|
|
115
|
/** |
|
116
|
* Component that renders the document title using react-helmet-async. |
|
117
|
* Should be used within a HelmetProvider. |
|
118
|
*/ |
|
119
|
export function DocumentTitle() { |
|
120
|
const title = useDocumentTitle() |
|
121
|
|
|
122
|
return ( |
|
123
|
<Helmet> |
|
124
|
<title>{title}</title> |
|
125
|
</Helmet> |
|
126
|
) |
|
127
|
} |