import * as React from "react" import { useMedia } from "@/hooks/use-media" import { useAtom } from "jotai" import { LogoIcon } from "@/components/icons/logo-icon" import { buttonVariants } from "@/components/ui/button" import { cn } from "@/lib/utils" import { isCollapseAtom } from "@/store/sidebar" import { useAccountOrGuest } from "@/lib/providers/jazz-provider" import { LaIcon } from "@/components/custom/la-icon" import { Link, useLocation } from "@tanstack/react-router" import { LinkSection } from "./partials/link-section" import { PageSection } from "./partials/page-section" import { TopicSection } from "./partials/topic-section" import { ProfileSection } from "./partials/profile-section" import { JournalSection } from "./partials/journal-section" import { TaskSection } from "./partials/task-section" interface SidebarContextType { isCollapsed: boolean setIsCollapsed: React.Dispatch> } const SidebarContext = React.createContext({ isCollapsed: false, setIsCollapsed: () => {}, }) const useSidebarCollapse = ( isTablet: boolean, ): [boolean, React.Dispatch>] => { const [isCollapsed, setIsCollapsed] = useAtom(isCollapseAtom) const location = useLocation() React.useEffect(() => { if (isTablet) setIsCollapsed(true) }, [location.pathname, setIsCollapsed, isTablet]) React.useEffect(() => { setIsCollapsed(isTablet) }, [isTablet, setIsCollapsed]) return [isCollapsed, setIsCollapsed] } interface SidebarItemProps { label: string url: string icon?: React.ReactNode onClick?: () => void children?: React.ReactNode } const SidebarItem: React.FC = React.memo( ({ label, url, icon, onClick, children }) => { const { pathname } = useLocation() const isActive = pathname === url return (
{icon && ( {icon} )} {label} {children}
) }, ) SidebarItem.displayName = "SidebarItem" const LogoAndSearch: React.FC = React.memo(() => { const { pathname } = useLocation() return (
Learn Anything
{pathname === "/search" ? ( "← Back" ) : ( )}
) }) LogoAndSearch.displayName = "LogoAndSearch" const SidebarContent: React.FC = React.memo(() => { const { me } = useAccountOrGuest() return ( ) }) SidebarContent.displayName = "SidebarContent" const Sidebar: React.FC = () => { const isTablet = useMedia("(max-width: 1024px)") const [isCollapsed, setIsCollapsed] = useSidebarCollapse(isTablet) const sidebarClasses = cn( "h-full overflow-hidden transition-all duration-300 ease-in-out", isCollapsed ? "w-0" : "w-auto min-w-56", ) const sidebarInnerClasses = cn( "h-full w-56 min-w-56 transition-transform duration-300 ease-in-out", isCollapsed ? "-translate-x-full" : "translate-x-0", ) const contextValue = React.useMemo( () => ({ isCollapsed, setIsCollapsed }), [isCollapsed, setIsCollapsed], ) if (isTablet) { return ( <>
setIsCollapsed(true)} />
) } return (
) } Sidebar.displayName = "Sidebar" export { Sidebar, SidebarItem, SidebarContext }