forked from alchaincyf/CoderWithAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTutorialSidebar.tsx
More file actions
69 lines (63 loc) · 2.21 KB
/
Copy pathTutorialSidebar.tsx
File metadata and controls
69 lines (63 loc) · 2.21 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
'use client'
import { useState } from 'react'
import Link from 'next/link'
import { ChevronRight, ChevronDown } from 'lucide-react'
import { usePathname } from 'next/navigation'
import { Tutorial } from '@/lib/tutorials';
interface TutorialSidebarProps {
tutorials: Tutorial[];
language: string;
}
function TutorialItem({ item, language, depth = 0 }: { item: Tutorial, language: string, depth?: number }) {
const [isOpen, setIsOpen] = useState(false)
const hasItems = item.items && item.items.length > 0
const pathname = usePathname()
const isActive = pathname === `/${language}/${item.path}`
const toggleOpen = (e: React.MouseEvent) => {
if (hasItems) {
e.preventDefault()
setIsOpen(!isOpen)
}
}
return (
<li>
<div className={`flex items-center py-2 ${isActive ? 'bg-blue-100' : ''}`}>
<div style={{ width: `${depth * 16}px` }} />
<Link
href={`/${encodeURIComponent(language)}/${encodeURIComponent(item.path)}`}
onClick={toggleOpen}
className={`flex-grow hover:text-blue-600 transition-colors duration-200
${depth === 0 ? 'font-semibold' : 'text-sm text-gray-600 ml-2'}
${isActive ? 'text-blue-600' : ''}
${item.isOutline ? 'font-bold' : ''}`}
>
{item.title}
</Link>
{hasItems && (
<button onClick={toggleOpen} className="focus:outline-none ml-2">
{isOpen ? <ChevronDown size={16} /> : <ChevronRight size={16} />}
</button>
)}
</div>
{hasItems && isOpen && item.items && (
<ul>
{item.items.map((subItem: Tutorial) => (
<TutorialItem key={subItem.path} item={subItem} language={language} depth={depth + 1} />
))}
</ul>
)}
</li>
)
}
export default function TutorialSidebar({ tutorials, language }: TutorialSidebarProps) {
return (
<nav className="w-64 bg-gray-50 p-4 overflow-auto h-[calc(100vh-8rem)] border-r border-gray-200">
<h2 className="text-xl font-bold mb-4 text-gray-800">{language} Tutorials</h2>
<ul>
{tutorials.map(item => (
<TutorialItem key={item.path} item={item} language={language} />
))}
</ul>
</nav>
)
}