|
1 | 1 | "use client"; |
2 | 2 | import Link from "next/link"; |
3 | 3 | import { usePathname } from "next/navigation"; |
4 | | -import useSWR, { Fetcher } from 'swr' |
| 4 | +import useSWR, { Fetcher } from "swr"; |
5 | 5 | import { splitMarkdown } from "./[docs_id]/splitMarkdown"; |
| 6 | +import { pagesList } from "./pagesList"; |
6 | 7 |
|
7 | | -const fetcher: Fetcher<string, string> = (url) => fetch(url).then((r) => r.text()) |
| 8 | +const fetcher: Fetcher<string, string> = (url) => |
| 9 | + fetch(url).then((r) => r.text()); |
8 | 10 |
|
9 | 11 | export function Sidebar() { |
10 | 12 | const pathname = usePathname(); |
11 | 13 | const docs_id = pathname.replace(/^\//, ""); |
12 | | - const { data, error, isLoading } = useSWR( |
13 | | - `/docs/${docs_id}.md`, |
14 | | - fetcher |
15 | | - ) |
| 14 | + const { data, error, isLoading } = useSWR(`/docs/${docs_id}.md`, fetcher); |
16 | 15 |
|
17 | | - const pages = [ |
18 | | - { id: "python-1", title: "1. 環境構築と基本思想" }, |
19 | | - { id: "python-2", title: "2. 基本構文とデータ型" }, |
20 | | - { id: "python-3", title: "3. リスト、タプル、辞書、セット" }, |
21 | | - { id: "python-4", title: "4. 制御構文と関数" }, |
22 | | - { id: "python-5", title: "5. モジュールとパッケージ" }, |
23 | | - { id: "python-6", title: "6. オブジェクト指向プログラミング" }, |
24 | | - { id: "python-7", title: "7. ファイルの入出力とコンテキストマネージャ" }, |
25 | | - { id: "python-8", title: "8. 例外処理" }, |
26 | | - { id: "python-9", title: "9. ジェネレータとデコレータ" }, |
27 | | - ]; |
| 16 | + if (error) console.error("Sidebar fetch error:", error); |
28 | 17 |
|
29 | | - if (error) console.error("Sidebar fetch error:", error) |
30 | | - |
31 | | - |
32 | | - |
33 | | - |
34 | | - const splitmdcontent = splitMarkdown(data ?? "") |
| 18 | + const splitmdcontent = splitMarkdown(data ?? ""); |
35 | 19 | return ( |
36 | | - <div className="bg-base-200 min-h-full w-80 p-4"> |
| 20 | + <div className="bg-base-200 h-full w-80 overflow-y-auto"> |
37 | 21 | {/* todo: 背景色ほんとにこれでいい? */} |
38 | | - <h2 className="hidden lg:block text-xl font-bold mb-4"> |
| 22 | + <h2 className="hidden lg:block text-xl font-bold p-4"> |
39 | 23 | {/* サイドバーが常時表示されている場合のみ */} |
40 | 24 | Navbar Title |
41 | 25 | </h2> |
42 | | - |
43 | | - <ol className="menu w-full list-outside"> |
44 | | - {pages.map((page) => ( |
45 | | - <li key={page.id}> |
46 | | - <Link href={`/${page.id}`}>{page.title}</Link> |
47 | | - {page.id === docs_id && !isLoading &&( |
48 | | - <ul className="ml-4 mt-2 text-sm"> |
49 | | - {splitmdcontent |
50 | | - .slice(1) |
51 | | - .map((section, idx) => ( |
52 | | - <li key={idx} style={{ marginLeft: (section.level - 2) + "em"}}> |
53 | | - <Link href={`#${idx+1}`}>{section.title}</Link> |
54 | | - </li> |
55 | | - ))} |
56 | | - </ul> |
57 | | - )} |
58 | 26 |
|
| 27 | + <ul className="menu w-full"> |
| 28 | + {pagesList.map((group) => ( |
| 29 | + <li key={group.id}> |
| 30 | + <details open={docs_id.startsWith(`${group.id}-`)}> |
| 31 | + <summary>{group.lang}</summary> |
| 32 | + <ul> |
| 33 | + {group.pages.map((page) => ( |
| 34 | + <li key={page.id}> |
| 35 | + <Link href={`${group.id}-${page.id}`}> |
| 36 | + <span className="mr-0">{page.id}.</span> |
| 37 | + {page.title} |
| 38 | + </Link> |
| 39 | + {`${group.id}-${page.id}` === docs_id && !isLoading && ( |
| 40 | + <ul className="ml-4 text-sm"> |
| 41 | + {splitmdcontent.slice(1).map((section, idx) => ( |
| 42 | + <li |
| 43 | + key={idx} |
| 44 | + style={{ marginLeft: section.level - 2 + "em" }} |
| 45 | + > |
| 46 | + <Link href={`#${idx + 1}`}>{section.title}</Link> |
| 47 | + </li> |
| 48 | + ))} |
| 49 | + </ul> |
| 50 | + )} |
| 51 | + </li> |
| 52 | + ))} |
| 53 | + </ul> |
| 54 | + </details> |
59 | 55 | </li> |
60 | 56 | ))} |
61 | | - </ol> |
| 57 | + </ul> |
62 | 58 | </div> |
63 | 59 | ); |
64 | 60 | } |
65 | | - |
0 commit comments