StudioMobileSheetNav.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import type { ReactNode } from 'react'
  2. import { CommandWrapper, MobileSheetNav } from 'ui-patterns'
  3. import {
  4. SIDEBAR_KEYS,
  5. type TYPEOF_SIDEBAR_KEYS,
  6. } from '../../ProjectLayout/LayoutSidebar/LayoutSidebarProvider'
  7. import type { MobileSheetContentType } from './MobileSheetContext'
  8. import { useMobileSheet } from './MobileSheetContext'
  9. import { CommandMenuInnerContent } from '@/components/interfaces/App/CommandMenu/CommandMenu'
  10. import { sidebarManagerState, useSidebarManagerSnapshot } from '@/state/sidebar-manager-state'
  11. function isSidebarId(content: unknown): content is TYPEOF_SIDEBAR_KEYS {
  12. return (
  13. typeof content === 'string' &&
  14. Object.values(SIDEBAR_KEYS).includes(content as TYPEOF_SIDEBAR_KEYS)
  15. )
  16. }
  17. function getSheetChildren(
  18. content: MobileSheetContentType,
  19. activeSidebar: { id: string; component?: () => ReactNode } | null
  20. ): ReactNode {
  21. if (content === null) return null
  22. if (content === 'search') {
  23. return (
  24. <CommandWrapper className="h-full flex flex-col bg-background">
  25. <CommandMenuInnerContent />
  26. </CommandWrapper>
  27. )
  28. }
  29. if (isSidebarId(content) && activeSidebar?.id === content) {
  30. return activeSidebar.component?.() ?? null
  31. }
  32. if (!isSidebarId(content)) return content
  33. return null
  34. }
  35. const StudioMobileSheetNav = () => {
  36. const { content, setContent } = useMobileSheet()
  37. const { activeSidebar } = useSidebarManagerSnapshot()
  38. const sheetChildren = getSheetChildren(content, activeSidebar ?? null)
  39. const handleOpenChange = (open: boolean) => {
  40. if (!open) {
  41. setContent(null)
  42. sidebarManagerState.closeActive()
  43. }
  44. }
  45. return (
  46. <MobileSheetNav
  47. open={content !== null}
  48. onOpenChange={handleOpenChange}
  49. shouldCloseOnViewportResize={!activeSidebar}
  50. >
  51. {sheetChildren}
  52. </MobileSheetNav>
  53. )
  54. }
  55. export { StudioMobileSheetNav }