-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsidebar.tsx
More file actions
232 lines (222 loc) · 7.38 KB
/
sidebar.tsx
File metadata and controls
232 lines (222 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
"use client";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { LanguageEntry } from "@/lib/docs";
import { AccountMenu } from "./accountMenu";
import { ThemeToggle } from "./themeToggle";
import {
createContext,
ReactNode,
useCallback,
useContext,
useEffect,
useState,
} from "react";
import { DynamicMarkdownSection } from "./[lang]/[pageId]/pageContent";
import clsx from "clsx";
import { LanguageIcon } from "@my-code/runtime/icons";
import { RuntimeLang } from "@my-code/runtime/languages";
export interface ISidebarMdContext {
loadedDocsId: { lang: string; pageId: string } | null;
sidebarMdContent: DynamicMarkdownSection[];
setSidebarMdContent: (
lang: string,
pageId: string,
content:
| DynamicMarkdownSection[]
| ((prev: DynamicMarkdownSection[]) => DynamicMarkdownSection[])
) => void;
}
const SidebarMdContext = createContext<ISidebarMdContext | null>(null);
export function useSidebarMdContext() {
const context = useContext(SidebarMdContext);
if (!context) {
throw new Error(
"useSidebarMdContext must be used within a SidebarMdProvider"
);
}
return context;
}
export function useSidebarMdContextOptional() {
return useContext(SidebarMdContext);
}
export function SidebarMdProvider({ children }: { children: ReactNode }) {
const [sidebarMdContent, setSidebarMdContent_] = useState<
DynamicMarkdownSection[]
>([]);
const [loadedDocsId, setLoadedDocsId] = useState<{
lang: string;
pageId: string;
} | null>(null);
const setSidebarMdContent = useCallback(
(
lang: string,
pageId: string,
content:
| DynamicMarkdownSection[]
| ((prev: DynamicMarkdownSection[]) => DynamicMarkdownSection[])
) => {
setLoadedDocsId({ lang, pageId });
setSidebarMdContent_(content);
},
[]
);
return (
<SidebarMdContext.Provider
value={{
loadedDocsId,
sidebarMdContent,
setSidebarMdContent,
}}
>
{children}
</SidebarMdContext.Provider>
);
}
export function Sidebar({ pagesList }: { pagesList: LanguageEntry[] }) {
const pathname = usePathname();
const pathnameMatch = pathname.match(/^\/([\w-_]+)\/([\w-_]+).*?/);
const currentLang = pathnameMatch?.[1];
const currentPageId = pathnameMatch?.[2];
const sidebarContext = useSidebarMdContext();
// sidebarMdContextの情報が古かったら使わない
const sidebarMdContent =
sidebarContext.loadedDocsId &&
sidebarContext.loadedDocsId.lang === currentLang &&
sidebarContext.loadedDocsId.pageId === currentPageId
? sidebarContext.sidebarMdContent
: [];
// 現在表示中のセクション(最初にinViewがtrueのもの)を見つける
const currentSectionId = sidebarMdContent.find(
(section, i) => i >= 1 && section.inView
)?.id;
// 目次の開閉状態
const [detailsOpen, setDetailsOpen] = useState<boolean[]>([]);
const currentLangIndex = pagesList.findIndex(
(group) => currentLang === group.id
);
useEffect(() => {
// 表示しているグループが変わったときに現在のグループのdetailsを開く
if (currentLangIndex !== -1) {
setDetailsOpen((detailsOpen) => {
const newDetailsOpen = [...detailsOpen];
while (newDetailsOpen.length <= currentLangIndex) {
newDetailsOpen.push(false);
}
newDetailsOpen[currentLangIndex] = true;
return newDetailsOpen;
});
}
}, [currentLangIndex]);
return (
<div className="bg-base-200 h-full w-80 overflow-y-auto">
<h2 className="hidden lg:flex flex-row items-center p-4 gap-2">
{/* サイドバーが常時表示されているlg以上の場合のみ */}
<Link href="/" className="flex-1 flex items-center">
<img
src="/icon.svg"
alt="my.code(); Logo"
className="inline-block w-8 h-8 mr-1"
/>
<span className="text-xl font-bold font-mono">my.code();</span>
</Link>
<ThemeToggle />
<AccountMenu />
</h2>
<span className="block lg:hidden p-4 pb-0">
<label
htmlFor="drawer-toggle"
aria-label="open sidebar"
className="btn btn-ghost"
>
<svg
className="w-8 h-8"
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M18 17L13 12L18 7M11 17L6 12L11 7"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
<span className="text-lg">Close</span>
</label>
</span>
<ul className="menu w-full">
{pagesList.map((group, i) => (
<li key={group.id}>
<details
open={!!detailsOpen.at(i)}
onToggle={(e) => {
const newDetailsOpen = [...detailsOpen];
while (newDetailsOpen.length <= i) {
newDetailsOpen.push(false);
}
newDetailsOpen[i] = e.currentTarget.open;
setDetailsOpen(newDetailsOpen);
}}
>
<summary>
<LanguageIcon
className="w-4 h-4"
lang={group.id as RuntimeLang}
/>
{group.name}
</summary>
<ul>
{group.pages.map((page) => (
<li key={page.slug}>
<Link
href={`/${group.id}/${page.slug}`}
className={clsx(
"text-wrap text-justify",
group.id === currentLang &&
page.slug === currentPageId &&
"menu-active"
)}
>
<span className="w-5 text-right">
<span className="float-right">{page.index}.</span>
</span>
{page.name}
</Link>
{group.id === currentLang &&
page.slug === currentPageId &&
sidebarMdContent.length > 0 && (
<ul className="ml-4 text-sm">
{sidebarMdContent.slice(1).map((section) => {
return (
<li
key={section.id}
style={{ marginLeft: section.level - 2 + "em" }}
>
<Link
href={`#${section.id}`}
className={clsx(
"text-wrap text-justify",
currentSectionId === section.id
? "font-bold"
: ""
)}
>
{section.title}
</Link>
</li>
);
})}
</ul>
)}
</li>
))}
</ul>
</details>
</li>
))}
</ul>
</div>
);
}