import { t as i18nT } from "i18next"; import { useTranslation } from "react-i18next"; // // "use client"; import * as React from "react"; import { Slot } from "@radix-ui/react-slot"; import { VariantProps, cva } from "class-variance-authority"; import { PanelLeftIcon } from "~/components/ui/icons/vendor"; import { useIsMobile } from "~/hooks/use-mobile"; import { cn } from "~/lib/utils"; import { Button } from "~/components/ui/button"; import { Input } from "~/components/ui/input"; import { Separator } from "~/components/ui/separator"; import { Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle } from "~/components/ui/sheet"; import { Skeleton } from "~/components/ui/skeleton"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "~/components/ui/tooltip"; import { useLocation, useSearchParams } from "react-router"; import { DARK_SIDEBAR_IN_LIGHT_MODE } from "~/application/Constants"; const SIDEBAR_COOKIE_NAME = "sidebar_state"; const SIDEBAR_COOKIE_MAX_AGE = 60 * 60 * 24 * 7; // Subframe SidebarWithSections layout: expanded width w-60 (15rem). Drives --sidebar-width // across gap/container/offcanvas math (all consume the CSS var, so it stays self-consistent). const SIDEBAR_WIDTH = "15rem"; const SIDEBAR_WIDTH_MOBILE = "80%"; const SIDEBAR_WIDTH_ICON = "3rem"; const SIDEBAR_KEYBOARD_SHORTCUT = "e"; type SidebarContextProps = { state: "expanded" | "collapsed"; open: boolean; setOpen: (open: boolean) => void; openMobile: boolean; setOpenMobile: (open: boolean) => void; isMobile: boolean; toggleSidebar: () => void; }; const SidebarContext = React.createContext(null); function useSidebar() { const context = React.useContext(SidebarContext); if (!context) { throw new Error("useSidebar must be used within a SidebarProvider."); } return context; } function SidebarProvider({ defaultOpen = true, open: openProp, onOpenChange: setOpenProp, className, style, children, ...props }: React.ComponentProps<"div"> & { defaultOpen?: boolean; open?: boolean; onOpenChange?: (open: boolean) => void; }) { const isMobile = useIsMobile(); const [openMobile, setOpenMobile] = React.useState(false); const location = useLocation(); React.useEffect(() => { setOpenMobile(false); }, [location.pathname, setOpenMobile]); // This is the internal state of the sidebar. // We use openProp and setOpenProp for control from outside the component. const [_open, _setOpen] = React.useState(defaultOpen); const open = openProp ?? _open; const setOpen = React.useCallback( (value: boolean | ((value: boolean) => boolean)) => { const openState = typeof value === "function" ? value(open) : value; if (setOpenProp) { setOpenProp(openState); } else { _setOpen(openState); } // This sets the cookie to keep the sidebar state. document.cookie = `${SIDEBAR_COOKIE_NAME}=${openState}; path=/; max-age=${SIDEBAR_COOKIE_MAX_AGE}`; }, [setOpenProp, open] ); // Helper to toggle the sidebar. const toggleSidebar = React.useCallback(() => { return isMobile ? setOpenMobile((open) => !open) : setOpen((open) => !open); }, [isMobile, setOpen, setOpenMobile]); // Adds a keyboard shortcut to toggle the sidebar. React.useEffect(() => { const handleKeyDown = (event: KeyboardEvent) => { if (event.key === SIDEBAR_KEYBOARD_SHORTCUT && (event.metaKey || event.ctrlKey)) { event.preventDefault(); toggleSidebar(); } }; window.addEventListener("keydown", handleKeyDown); return () => window.removeEventListener("keydown", handleKeyDown); }, [toggleSidebar]); // We add a state so that we can do data-state="expanded" or "collapsed". // This makes it easier to style the sidebar with Tailwind classes. const state = open ? "expanded" : "collapsed"; const contextValue = React.useMemo( () => ({ state, open, setOpen, isMobile, openMobile, setOpenMobile, toggleSidebar, }), [state, open, setOpen, isMobile, openMobile, setOpenMobile, toggleSidebar] ); return (
{children}
); } function Sidebar({ side = "left", variant = "sidebar", collapsible = "offcanvas", layout = "app", className, children, ...props }: React.ComponentProps<"div"> & { side?: "left" | "right"; variant?: "sidebar" | "floating" | "inset"; collapsible?: "offcanvas" | "icon" | "none"; layout?: "app" | "admin" | "docs"; }) { const { isMobile, state, openMobile, setOpenMobile } = useSidebar(); const [searchParams] = useSearchParams(); // const withPatch = searchParams.get("patch") === "true"; const withoutPatch = searchParams.get("patch") === "false"; const withPatch = withoutPatch; if (collapsible === "none") { return (
{children}
); } if (isMobile) { return ( button]:hidden", DARK_SIDEBAR_IN_LIGHT_MODE({ layout }) && "dark")} style={ { "--sidebar-width": SIDEBAR_WIDTH_MOBILE, ...props.style, } as React.CSSProperties } side={side} onCloseAutoFocus={(event) => { // The sidebar opens via controlled state, not a Radix trigger, so Radix // cannot track the opener and drops focus to on close. Return // focus to the trigger explicitly so keyboard users are not stranded. const trigger = typeof document !== "undefined" ? document.querySelector('[data-ui-id="ui.action.shell.sidebarTrigger"]') : null; if (trigger) { event.preventDefault(); trigger.focus(); } }} > {i18nT("components.sidebar.title")} {i18nT("components.sidebar.mobileDescription")}
{children}
); } return (