|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { type ReactNode, useRef, useState } from 'react' |
| 4 | +import { |
| 5 | + Button, |
| 6 | + Chip, |
| 7 | + ChipInput, |
| 8 | + cn, |
| 9 | + scrollFadeAttributes, |
| 10 | + scrollFadeClass, |
| 11 | + scrollFadeXClass, |
| 12 | + useScrollEdges, |
| 13 | +} from '@sim/emcn' |
| 14 | +import { Search, X } from '@sim/emcn/icons' |
| 15 | +import { HEADER_ACTION_CLUSTER, PAGE_HEADER_BAR } from '@/components/page-header-bar' |
| 16 | +import { useOrganizationPageFilters } from '@/app/o/[organizationId]/components/organization-page/use-organization-page-filters' |
| 17 | +import { |
| 18 | + SIDEBAR_DIVIDER_PAD_ABOVE_CLASS, |
| 19 | + SIDEBAR_DIVIDER_PAD_BELOW_CLASS, |
| 20 | +} from '@/app/workspace/[workspaceId]/w/components/sidebar/constants' |
| 21 | + |
| 22 | +/** The home surface's reading column, so every organization page shares its width. */ |
| 23 | +const COLUMN_CLASS = 'mx-auto w-full max-w-chat px-6' |
| 24 | + |
| 25 | +export interface OrganizationPageTab { |
| 26 | + id: string |
| 27 | + label: string |
| 28 | +} |
| 29 | + |
| 30 | +interface OrganizationPageProps { |
| 31 | + title: string |
| 32 | + description?: string |
| 33 | + /** Header tabs; the first is the default. Omit for a page with one view. */ |
| 34 | + tabs?: readonly OrganizationPageTab[] |
| 35 | + /** Label of the page's primary action chip. Omit for a page without one. */ |
| 36 | + action?: string |
| 37 | + children?: ReactNode |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * The shell every organization page renders into: the top bar the workspace pages |
| 42 | + * wear, then a fixed page header — title, description, tabs, search, and the |
| 43 | + * optional action — over a scroll region that fades at both edges the way the |
| 44 | + * sidebar does. Pages supply their content and logic; nothing else. |
| 45 | + * |
| 46 | + * The shell paints at once and never waits on data: a page renders each piece — |
| 47 | + * a tab, a list, a count — the moment it is known and nothing before, with no |
| 48 | + * skeleton standing in for it. Pass `tabs` only once they are known; the row |
| 49 | + * simply gains them. |
| 50 | + */ |
| 51 | +export function OrganizationPage({ |
| 52 | + title, |
| 53 | + description, |
| 54 | + tabs, |
| 55 | + action, |
| 56 | + children, |
| 57 | +}: OrganizationPageProps) { |
| 58 | + const scrollContainerRef = useRef<HTMLDivElement>(null) |
| 59 | + const scrollContentRef = useRef<HTMLDivElement>(null) |
| 60 | + const scrollEdges = useScrollEdges(scrollContainerRef, { contentRef: scrollContentRef }) |
| 61 | + const tabsRef = useRef<HTMLDivElement>(null) |
| 62 | + const tabEdges = useScrollEdges(tabsRef, { axis: 'x' }) |
| 63 | + |
| 64 | + const { tab, search, setTab, setSearch } = useOrganizationPageFilters() |
| 65 | + const defaultTab = tabs?.[0]?.id |
| 66 | + const activeTab = tab ?? defaultTab |
| 67 | + |
| 68 | + /** |
| 69 | + * The field stays open while it holds text, across tab switches and reloads, |
| 70 | + * since the text lives in the URL; this only remembers an empty field the |
| 71 | + * viewer opened and has not dismissed. |
| 72 | + */ |
| 73 | + const [searchOpened, setSearchOpened] = useState(false) |
| 74 | + const searchOpen = searchOpened || search.length > 0 |
| 75 | + |
| 76 | + const closeSearch = () => { |
| 77 | + setSearch('') |
| 78 | + setSearchOpened(false) |
| 79 | + } |
| 80 | + |
| 81 | + return ( |
| 82 | + <div className='flex h-full flex-col bg-[var(--bg)]'> |
| 83 | + {/* Reserved even while empty so the page header sits where the workspace's does. */} |
| 84 | + <div className={PAGE_HEADER_BAR}> |
| 85 | + <div className={HEADER_ACTION_CLUSTER} /> |
| 86 | + </div> |
| 87 | + |
| 88 | + <div className={cn(COLUMN_CLASS, SIDEBAR_DIVIDER_PAD_ABOVE_CLASS, 'flex shrink-0 flex-col')}> |
| 89 | + <div className='flex flex-col gap-1 pt-8'> |
| 90 | + <h1 className='text-[18px] text-[var(--text-primary)]'>{title}</h1> |
| 91 | + {description && <p className='text-[var(--text-muted)] text-small'>{description}</p>} |
| 92 | + </div> |
| 93 | + <div className='mt-4 flex items-center justify-between gap-2'> |
| 94 | + {/* The row yields to the controls beside it and scrolls sideways under a fade |
| 95 | + once it can no longer fit; the scrollbar itself never shows. */} |
| 96 | + <div |
| 97 | + ref={tabsRef} |
| 98 | + className={cn( |
| 99 | + scrollFadeXClass, |
| 100 | + 'flex min-w-0 flex-1 items-center gap-[1px] overflow-x-auto [scrollbar-width:none] [&::-webkit-scrollbar]:hidden' |
| 101 | + )} |
| 102 | + {...scrollFadeAttributes(tabEdges)} |
| 103 | + > |
| 104 | + {tabs?.map((item) => { |
| 105 | + const active = item.id === activeTab |
| 106 | + return ( |
| 107 | + <Chip |
| 108 | + key={item.id} |
| 109 | + shape='round' |
| 110 | + active={active} |
| 111 | + aria-pressed={active} |
| 112 | + onClick={() => setTab(item.id === defaultTab ? null : item.id)} |
| 113 | + className='min-w-[44px] shrink-0 text-center' |
| 114 | + > |
| 115 | + {item.label} |
| 116 | + </Chip> |
| 117 | + ) |
| 118 | + })} |
| 119 | + </div> |
| 120 | + <div className='flex shrink-0 items-center gap-1.5'> |
| 121 | + {searchOpen ? ( |
| 122 | + <ChipInput |
| 123 | + autoFocus |
| 124 | + icon={Search} |
| 125 | + value={search} |
| 126 | + placeholder='Search' |
| 127 | + aria-label='Search' |
| 128 | + spellCheck={false} |
| 129 | + autoComplete='off' |
| 130 | + /* Grows leftward from the chip's spot, to this and no further. */ |
| 131 | + className='w-[240px]' |
| 132 | + onChange={(event) => setSearch(event.target.value)} |
| 133 | + onKeyDown={(event) => { |
| 134 | + if (event.key === 'Escape') closeSearch() |
| 135 | + }} |
| 136 | + endAdornment={ |
| 137 | + <Button |
| 138 | + type='button' |
| 139 | + variant='quiet' |
| 140 | + size='icon' |
| 141 | + className='-mr-1 shrink-0' |
| 142 | + aria-label='Close search' |
| 143 | + onClick={closeSearch} |
| 144 | + > |
| 145 | + <X className='size-[13px]' /> |
| 146 | + </Button> |
| 147 | + } |
| 148 | + /> |
| 149 | + ) : ( |
| 150 | + <Chip leftIcon={Search} aria-label='Search' onClick={() => setSearchOpened(true)} /> |
| 151 | + )} |
| 152 | + {action && <Chip variant='primary'>{action}</Chip>} |
| 153 | + </div> |
| 154 | + </div> |
| 155 | + </div> |
| 156 | + |
| 157 | + <div |
| 158 | + ref={scrollContainerRef} |
| 159 | + className={cn( |
| 160 | + SIDEBAR_DIVIDER_PAD_BELOW_CLASS, |
| 161 | + SIDEBAR_DIVIDER_PAD_ABOVE_CLASS, |
| 162 | + scrollFadeClass, |
| 163 | + 'min-h-0 flex-1 overflow-y-auto [scrollbar-gutter:stable_both-edges]' |
| 164 | + )} |
| 165 | + {...scrollFadeAttributes(scrollEdges)} |
| 166 | + > |
| 167 | + <div ref={scrollContentRef} className={COLUMN_CLASS}> |
| 168 | + {children} |
| 169 | + </div> |
| 170 | + </div> |
| 171 | + </div> |
| 172 | + ) |
| 173 | +} |
0 commit comments