|
| 1 | +import { |
| 2 | + CircleUser, |
| 3 | + Home, |
| 4 | + LineChart, |
| 5 | + Menu, |
| 6 | + Fullscreen, |
| 7 | + Shrink |
| 8 | +} from "lucide-react" |
| 9 | +import { |
| 10 | + DropdownMenu, |
| 11 | + DropdownMenuContent, |
| 12 | + DropdownMenuItem, |
| 13 | + DropdownMenuLabel, |
| 14 | + DropdownMenuSeparator, |
| 15 | + DropdownMenuTrigger, |
| 16 | +} from "@/components/ui/dropdown-menu" |
| 17 | +import { Sheet, SheetContent, SheetTrigger } from "@/components/ui/sheet" |
| 18 | +import { Button } from "@/components/ui/button" |
| 19 | +import { ModeToggle } from "@/components/Theme/mode-toggle" |
| 20 | +import { useNavigate } from "react-router-dom"; |
| 21 | +import { useState, useEffect } from "react" |
| 22 | + |
| 23 | +const BACKEND_URL = import.meta.env.VITE_BACKEND_URL || "http://localhost:5000"; |
| 24 | + |
| 25 | +const MobileSidebar = () => { |
| 26 | + const [userImage, setUserImage] = useState<string | null>(null); |
| 27 | + const [isFullscreen, setIsFullscreen] = useState(() => { |
| 28 | + // Parse the value from localStorage as a boolean |
| 29 | + return localStorage.getItem('isFullscreen') === 'true'; |
| 30 | + }); |
| 31 | + const navigate = useNavigate(); |
| 32 | + |
| 33 | + const handleFullscreenToggle = () => { |
| 34 | + if (!isFullscreen) { |
| 35 | + document.documentElement.requestFullscreen(); |
| 36 | + } else { |
| 37 | + document.exitFullscreen(); |
| 38 | + } |
| 39 | + setIsFullscreen(!isFullscreen); |
| 40 | + // Store the fullscreen state as a string ("true"/"false") |
| 41 | + localStorage.setItem('isFullscreen', String(!isFullscreen)); |
| 42 | + }; |
| 43 | + |
| 44 | + useEffect(() => { |
| 45 | + const handleFullscreenChange = () => { |
| 46 | + const fullscreen = document.fullscreenElement !== null; |
| 47 | + setIsFullscreen(fullscreen); |
| 48 | + localStorage.setItem('isFullscreen', String(fullscreen)); |
| 49 | + }; |
| 50 | + |
| 51 | + document.addEventListener('fullscreenchange', handleFullscreenChange); |
| 52 | + return () => { |
| 53 | + document.removeEventListener('fullscreenchange', handleFullscreenChange); |
| 54 | + }; |
| 55 | + }, []); |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + const fetchUserData = async () => { |
| 59 | + try { |
| 60 | + const username = localStorage.getItem('username'); |
| 61 | + if (!username) { |
| 62 | + throw new Error('Username not found in localStorage'); |
| 63 | + } |
| 64 | + |
| 65 | + const response = await fetch(`${BACKEND_URL}/profile/${username}`, { |
| 66 | + method: 'GET', |
| 67 | + headers: { |
| 68 | + 'Content-Type': 'application/json', |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + if (!response.ok) { |
| 73 | + throw new Error('Network response was not ok'); |
| 74 | + } |
| 75 | + |
| 76 | + const responseData = await response.json(); |
| 77 | + setUserImage(responseData.user.image || null); |
| 78 | + } catch (error) { |
| 79 | + console.error('Error fetching user data', error); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + fetchUserData(); |
| 84 | + }, []); |
| 85 | + |
| 86 | + return ( |
| 87 | + <header className="flex h-14 items-center gap-4 border-b bg-muted/40 px-4 lg:h-[60px] lg:px-6"> |
| 88 | + <Sheet> |
| 89 | + <SheetTrigger asChild> |
| 90 | + <Button variant="outline" size="icon" className="shrink-0 md:hidden"> |
| 91 | + <Menu className="h-5 w-5" /> |
| 92 | + <span className="sr-only">Toggle navigation menu</span> |
| 93 | + </Button> |
| 94 | + </SheetTrigger> |
| 95 | + <SheetContent side="left" className="flex flex-col"> |
| 96 | + <nav className="grid gap-2 text-lg font-medium"> |
| 97 | + <a href="#" className="flex items-center gap-2 text-lg font-semibold"> |
| 98 | + <span>Quantica</span> |
| 99 | + </a> |
| 100 | + <a |
| 101 | + href="/home" |
| 102 | + className="mx-[-0.65rem] flex items-center gap-4 rounded-xl px-3 py-2 text-muted-foreground hover:text-foreground" |
| 103 | + > |
| 104 | + <Home className="h-5 w-5" /> |
| 105 | + Home |
| 106 | + </a> |
| 107 | + <a |
| 108 | + href="/plot" |
| 109 | + className="mx-[-0.65rem] flex items-center gap-4 rounded-xl px-3 py-2 text-muted-foreground hover:text-foreground" |
| 110 | + > |
| 111 | + <LineChart className="h-5 w-5" /> |
| 112 | + f(x) Plotter |
| 113 | + </a> |
| 114 | + </nav> |
| 115 | + </SheetContent> |
| 116 | + </Sheet> |
| 117 | + <div className="w-full flex-1"></div> |
| 118 | + <DropdownMenu> |
| 119 | + {isFullscreen ? ( |
| 120 | + <Shrink onClick={handleFullscreenToggle} /> |
| 121 | + ) : ( |
| 122 | + <Fullscreen onClick={handleFullscreenToggle} /> |
| 123 | + )} |
| 124 | + <ModeToggle /> |
| 125 | + <DropdownMenuTrigger asChild> |
| 126 | + <Button variant="secondary" size="icon" className="rounded-full"> |
| 127 | + {userImage ? ( |
| 128 | + <img src={userImage} alt="User Profile" className="h-5 w-5 rounded-full" /> |
| 129 | + ) : ( |
| 130 | + <CircleUser className="h-5 w-5" /> |
| 131 | + )} |
| 132 | + <span className="sr-only">Toggle user menu</span> |
| 133 | + </Button> |
| 134 | + </DropdownMenuTrigger> |
| 135 | + <DropdownMenuContent align="end"> |
| 136 | + <DropdownMenuLabel>My Account</DropdownMenuLabel> |
| 137 | + <DropdownMenuSeparator /> |
| 138 | + <DropdownMenuItem |
| 139 | + onClick={() => { |
| 140 | + const username = localStorage.getItem('username'); |
| 141 | + if (username) { |
| 142 | + navigate(`/profile/${username}`); |
| 143 | + } else { |
| 144 | + console.error('Username not found in localStorage'); |
| 145 | + } |
| 146 | + }} |
| 147 | + > |
| 148 | + Profile |
| 149 | + </DropdownMenuItem> |
| 150 | + <DropdownMenuItem onClick={() => navigate('/settings')}>Settings</DropdownMenuItem> |
| 151 | + <DropdownMenuSeparator /> |
| 152 | + <DropdownMenuItem>Logout</DropdownMenuItem> |
| 153 | + </DropdownMenuContent> |
| 154 | + </DropdownMenu> |
| 155 | + </header> |
| 156 | + ); |
| 157 | +}; |
| 158 | + |
| 159 | +export default MobileSidebar; |
0 commit comments