|
1
|
"use client" |
|
2
|
|
|
3
|
import React from "react" |
|
4
|
import { createContext, useEffect } from "react" |
|
5
|
import { useSidebar } from "docs-ui" |
|
6
|
import { useArea } from "./area" |
|
7
|
import { Sidebar } from "types" |
|
8
|
|
|
9
|
const PageTitleContext = createContext(null) |
|
10
|
|
|
11
|
type PageTitleProviderProps = { |
|
12
|
children: React.ReactNode |
|
13
|
} |
|
14
|
|
|
15
|
const PageTitleProvider = ({ children }: PageTitleProviderProps) => { |
|
16
|
const { activePath, activeItem } = useSidebar() |
|
17
|
const { displayedArea } = useArea() |
|
18
|
|
|
19
|
useEffect(() => { |
|
20
|
const titleSuffix = `Medusa ${displayedArea} API Reference` |
|
21
|
|
|
22
|
if (!activePath?.length) { |
|
23
|
document.title = titleSuffix |
|
24
|
} else { |
|
25
|
if (activeItem?.path === activePath) { |
|
26
|
document.title = `${activeItem?.title} - ${titleSuffix}` |
|
27
|
} else { |
|
28
|
// find the child that matches the active path |
|
29
|
const item = activeItem?.children?.find( |
|
30
|
(i) => i.type === "link" && i.path === activePath |
|
31
|
) as Sidebar.SidebarItemLink |
|
32
|
if (item) { |
|
33
|
document.title = `${item.title} - ${titleSuffix}` |
|
34
|
} else { |
|
35
|
document.title = titleSuffix |
|
36
|
} |
|
37
|
} |
|
38
|
} |
|
39
|
}, [activePath, displayedArea, activeItem]) |
|
40
|
|
|
41
|
return ( |
|
42
|
<PageTitleContext.Provider value={null}> |
|
43
|
{children} |
|
44
|
</PageTitleContext.Provider> |
|
45
|
) |
|
46
|
} |
|
47
|
|
|
48
|
export default PageTitleProvider |