|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useCallback, useEffect, useState } from "react"; |
| 4 | +import { useQueryState } from "nuqs"; |
| 5 | +import { Menu } from "lucide-react"; |
| 6 | + |
| 7 | +import type { Category, Script } from "@/lib/types"; |
| 8 | + |
| 9 | +import { ScriptItem } from "@/app/scripts/_components/script-item"; |
| 10 | +import Sidebar from "@/app/scripts/_components/sidebar"; |
| 11 | +import { fetchCategories } from "@/lib/data"; |
| 12 | + |
| 13 | +import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetTrigger } from "../ui/sheet"; |
| 14 | +import { Button } from "../ui/button"; |
| 15 | + |
| 16 | +function MobileSidebar() { |
| 17 | + const [isOpen, setIsOpen] = useState(false); |
| 18 | + const [isLoading, setIsLoading] = useState(false); |
| 19 | + const [categories, setCategories] = useState<Category[]>([]); |
| 20 | + const [lastViewedScript, setLastViewedScript] = useState<Script | undefined>(undefined); |
| 21 | + const [selectedScript, setSelectedScript] = useQueryState("id"); |
| 22 | + const [selectedCategory, setSelectedCategory] = useQueryState("category"); |
| 23 | + |
| 24 | + const loadCategories = useCallback(async () => { |
| 25 | + setIsLoading(true); |
| 26 | + try { |
| 27 | + const response = await fetchCategories(); |
| 28 | + setCategories(response); |
| 29 | + } |
| 30 | + catch (error) { |
| 31 | + console.error(error); |
| 32 | + } |
| 33 | + finally { |
| 34 | + setIsLoading(false); |
| 35 | + } |
| 36 | + }, []); |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + void loadCategories(); |
| 40 | + }, [loadCategories]); |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + if (!selectedScript || categories.length === 0) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const scriptMatch = categories |
| 48 | + .flatMap(category => category.scripts) |
| 49 | + .find(script => script.slug === selectedScript); |
| 50 | + |
| 51 | + setLastViewedScript(scriptMatch); |
| 52 | + }, [selectedScript, categories]); |
| 53 | + |
| 54 | + const handleOpenChange = (openState: boolean) => { |
| 55 | + setIsOpen(openState); |
| 56 | + }; |
| 57 | + |
| 58 | + const handleItemSelect = () => { |
| 59 | + setIsOpen(false); |
| 60 | + }; |
| 61 | + |
| 62 | + const hasLinks = categories.length > 0; |
| 63 | + |
| 64 | + return ( |
| 65 | + <Sheet open={isOpen} onOpenChange={handleOpenChange}> |
| 66 | + <SheetTrigger asChild> |
| 67 | + <Button |
| 68 | + variant="ghost" |
| 69 | + size="icon" |
| 70 | + aria-label="Open navigation menu" |
| 71 | + tabIndex={0} |
| 72 | + onKeyDown={(event) => { |
| 73 | + if (event.key === "Enter" || event.key === " ") { |
| 74 | + setIsOpen(true); |
| 75 | + } |
| 76 | + }} |
| 77 | + > |
| 78 | + <Menu className="size-5" aria-hidden="true" /> |
| 79 | + </Button> |
| 80 | + </SheetTrigger> |
| 81 | + <SheetHeader className="border-b border-border px-6 pb-4 pt-2"><SheetTitle className="sr-only">Categories</SheetTitle></SheetHeader> |
| 82 | + <SheetContent side="left" className="flex w-full max-w-xs flex-col gap-4 overflow-hidden px-0 pb-6"> |
| 83 | + <div className="flex h-full flex-col gap-4 overflow-y-auto"> |
| 84 | + {isLoading && !hasLinks |
| 85 | + ? ( |
| 86 | + <div className="flex w-full flex-col items-center justify-center gap-2 px-6 py-4 text-sm text-muted-foreground"> |
| 87 | + Loading categories... |
| 88 | + </div> |
| 89 | + ) |
| 90 | + : ( |
| 91 | + <div className="flex flex-col gap-4 px-4"> |
| 92 | + <Sidebar |
| 93 | + items={categories} |
| 94 | + selectedScript={selectedScript} |
| 95 | + setSelectedScript={setSelectedScript} |
| 96 | + selectedCategory={selectedCategory} |
| 97 | + setSelectedCategory={setSelectedCategory} |
| 98 | + onItemSelect={handleItemSelect} |
| 99 | + /> |
| 100 | + </div> |
| 101 | + )} |
| 102 | + {selectedScript && lastViewedScript |
| 103 | + ? ( |
| 104 | + <div className="flex flex-col gap-3 px-4"> |
| 105 | + <p className="text-sm font-medium">Last Viewed</p> |
| 106 | + <ScriptItem item={lastViewedScript} setSelectedScript={setSelectedScript} /> |
| 107 | + </div> |
| 108 | + ) |
| 109 | + : null} |
| 110 | + </div> |
| 111 | + </SheetContent> |
| 112 | + </Sheet> |
| 113 | + ); |
| 114 | +} |
| 115 | + |
| 116 | +export default MobileSidebar; |
0 commit comments