|
1 | 1 | "use client"; |
2 | 2 | import Link from "next/link"; |
3 | 3 | import { usePathname } from "next/navigation"; |
4 | | -import useSWR, { Fetcher } from "swr"; |
5 | | -import { splitMarkdown } from "./[docs_id]/splitMarkdown"; |
6 | 4 | import { pagesList } from "./pagesList"; |
7 | 5 | import { AccountMenu } from "./accountMenu"; |
8 | 6 | import { ThemeToggle } from "./[docs_id]/themeToggle"; |
9 | 7 | import { useDynamicMdContext } from "./[docs_id]/dynamicMdContext"; |
10 | 8 |
|
11 | | -const fetcher: Fetcher<string, string> = (url) => |
12 | | - fetch(url).then((r) => r.text()); |
13 | | - |
14 | 9 | export function Sidebar() { |
15 | 10 | const pathname = usePathname(); |
16 | 11 | const docs_id = pathname.replace(/^\//, ""); |
17 | 12 | const { dynamicMdContent } = useDynamicMdContext(); |
18 | 13 |
|
19 | | - // コンテキストが空の場合(まだページがロードされていない場合)はフェッチする |
20 | | - const { data, error, isLoading } = useSWR( |
21 | | - dynamicMdContent.length === 0 ? `/docs/${docs_id}.md` : null, |
22 | | - fetcher |
23 | | - ); |
24 | | - |
25 | | - if (error) console.error("Sidebar fetch error:", error); |
26 | | - |
27 | | - // コンテキストがある場合はそれを使用、ない場合はフェッチしたデータを使用 |
28 | | - const splitmdcontent = dynamicMdContent.length > 0 ? dynamicMdContent : splitMarkdown(data ?? ""); |
29 | | - |
30 | 14 | // 現在表示中のセクション(最初にinViewがtrueのもの)を見つける |
31 | 15 | const currentSectionIndex = dynamicMdContent.findIndex( |
32 | 16 | (section) => section.inView |
@@ -78,9 +62,9 @@ export function Sidebar() { |
78 | 62 | <span className="mr-0">{page.id}.</span> |
79 | 63 | {page.title} |
80 | 64 | </Link> |
81 | | - {`${group.id}-${page.id}` === docs_id && !isLoading && ( |
| 65 | + {`${group.id}-${page.id}` === docs_id && dynamicMdContent.length > 0 && ( |
82 | 66 | <ul className="ml-4 text-sm"> |
83 | | - {splitmdcontent.slice(1).map((section, idx) => { |
| 67 | + {dynamicMdContent.slice(1).map((section, idx) => { |
84 | 68 | // idx + 1 は実際のsectionIndexに対応(slice(1)で最初を除外しているため) |
85 | 69 | const isCurrentSection = idx + 1 === currentSectionIndex; |
86 | 70 | return ( |
|
0 commit comments