index.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { useBreakpoint } from 'common'
  2. import { useEffect } from 'react'
  3. import { cn, ResizableHandle, ResizablePanel } from 'ui'
  4. import { SIDEBAR_KEYS, type TYPEOF_SIDEBAR_KEYS } from './LayoutSidebarProvider'
  5. import { useMobileSheet } from '@/components/layouts/Navigation/NavigationBar/MobileSheetContext'
  6. import { useSidebarManagerSnapshot } from '@/state/sidebar-manager-state'
  7. function isSidebarId(content: unknown): content is TYPEOF_SIDEBAR_KEYS {
  8. return (
  9. typeof content === 'string' &&
  10. Object.values(SIDEBAR_KEYS).includes(content as TYPEOF_SIDEBAR_KEYS)
  11. )
  12. }
  13. interface LayoutSidebarProps {
  14. minSize?: string | number
  15. maxSize?: string | number
  16. defaultSize?: string | number
  17. }
  18. export const LayoutSidebar = ({
  19. minSize = '30',
  20. maxSize = '50',
  21. defaultSize = '30',
  22. }: LayoutSidebarProps) => {
  23. const { activeSidebar } = useSidebarManagerSnapshot()
  24. const isMobile = useBreakpoint('md')
  25. const { content: sheetContent, setContent: setMobileSheetContent } = useMobileSheet()
  26. useEffect(() => {
  27. if (!isMobile) {
  28. setMobileSheetContent(null)
  29. return
  30. }
  31. if (activeSidebar?.component) {
  32. setMobileSheetContent(activeSidebar.id)
  33. } else if (isSidebarId(sheetContent)) {
  34. setMobileSheetContent(null)
  35. }
  36. }, [isMobile, activeSidebar, sheetContent, setMobileSheetContent])
  37. if (!activeSidebar?.component) return null
  38. if (isMobile) return null
  39. return (
  40. <>
  41. <ResizableHandle withHandle />
  42. <ResizablePanel
  43. id="panel-side"
  44. key={activeSidebar?.id ?? 'default'}
  45. defaultSize={defaultSize}
  46. minSize={minSize}
  47. maxSize={maxSize}
  48. className={cn(
  49. 'border-l bg fixed z-40 right-0 top-0 bottom-0',
  50. 'h-dvh',
  51. 'md:absolute md:h-auto md:w-1/2',
  52. 'lg:w-2/5',
  53. 'xl:relative xl:border-l-0'
  54. )}
  55. >
  56. {activeSidebar?.component()}
  57. </ResizablePanel>
  58. </>
  59. )
  60. }