|
| 1 | +import { Select } from '@base-ui/react/select' |
| 2 | +import { CaretDownIcon, CheckIcon } from '@phosphor-icons/react' |
| 3 | +import * as v from 'valibot' |
| 4 | +import { libraries } from '~/libraries' |
| 5 | +import { frameworkOptions } from '~/libraries/frameworks' |
| 6 | +import { allMaintainers } from '~/libraries/maintainers' |
| 7 | +import { libraryIdSchema } from '~/utils/schemas' |
| 8 | + |
| 9 | +const frameworkChoices = frameworkOptions.filter(({ value }) => |
| 10 | + allMaintainers.some((maintainer) => |
| 11 | + maintainer.frameworkExpertise?.includes(value), |
| 12 | + ), |
| 13 | +) |
| 14 | +const specialtyLabels = new Map<string, string>() |
| 15 | +for (const label of allMaintainers.flatMap( |
| 16 | + (person) => person.specialties ?? [], |
| 17 | +)) { |
| 18 | + const value = label.toLowerCase() |
| 19 | + if (!specialtyLabels.has(value)) specialtyLabels.set(value, label) |
| 20 | +} |
| 21 | +const specialtyChoices = Array.from(specialtyLabels, ([value, label]) => ({ |
| 22 | + value, |
| 23 | + label, |
| 24 | +})).sort((a, b) => a.label.localeCompare(b.label)) |
| 25 | + |
| 26 | +const groupBySchema = v.picklist(['none', 'core', 'library', 'role']) |
| 27 | +const sortBySchema = v.picklist(['none', 'name', 'role', 'contributions']) |
| 28 | + |
| 29 | +export const maintainersDirectorySearchSchema = v.object({ |
| 30 | + libraries: v.fallback(v.optional(v.array(libraryIdSchema)), undefined), |
| 31 | + frameworks: v.fallback( |
| 32 | + v.optional(v.array(v.picklist(frameworkChoices.map(({ value }) => value)))), |
| 33 | + undefined, |
| 34 | + ), |
| 35 | + specialties: v.fallback( |
| 36 | + v.optional(v.array(v.picklist(specialtyChoices.map(({ value }) => value)))), |
| 37 | + undefined, |
| 38 | + ), |
| 39 | + groupBy: v.fallback(v.optional(groupBySchema, 'none'), 'none'), |
| 40 | + sortBy: v.fallback(v.optional(sortBySchema, 'none'), 'none'), |
| 41 | +}) |
| 42 | + |
| 43 | +type DirectorySearch = v.InferOutput<typeof maintainersDirectorySearchSchema> |
| 44 | + |
| 45 | +const groupByChoices = [ |
| 46 | + { value: 'none', label: 'No grouping' }, |
| 47 | + { value: 'core', label: 'Core status' }, |
| 48 | + { value: 'library', label: 'Library' }, |
| 49 | + { value: 'role', label: 'Role' }, |
| 50 | +] satisfies ReadonlyArray<{ value: DirectorySearch['groupBy']; label: string }> |
| 51 | + |
| 52 | +const sortByChoices = [ |
| 53 | + { value: 'none', label: 'Default' }, |
| 54 | + { value: 'name', label: 'Name' }, |
| 55 | + { value: 'role', label: 'Role' }, |
| 56 | + { value: 'contributions', label: 'Contributions' }, |
| 57 | +] satisfies ReadonlyArray<{ value: DirectorySearch['sortBy']; label: string }> |
| 58 | + |
| 59 | +function FilterSelect< |
| 60 | + TValue extends string, |
| 61 | + TMultiple extends boolean = false, |
| 62 | +>({ |
| 63 | + label, |
| 64 | + choices, |
| 65 | + ...selection |
| 66 | +}: Pick< |
| 67 | + Select.Root.Props<TValue, TMultiple>, |
| 68 | + 'multiple' | 'value' | 'onValueChange' |
| 69 | +> & { |
| 70 | + label: string |
| 71 | + choices: ReadonlyArray<{ value: TValue; label: string }> |
| 72 | +}) { |
| 73 | + const selectionCount = Array.isArray(selection.value) |
| 74 | + ? selection.value.length |
| 75 | + : 0 |
| 76 | + |
| 77 | + return ( |
| 78 | + <div className="flex min-w-0 flex-col gap-1"> |
| 79 | + <Select.Root<TValue, TMultiple> |
| 80 | + {...selection} |
| 81 | + items={choices} |
| 82 | + modal={false} |
| 83 | + > |
| 84 | + <Select.Label className="text-xs font-medium text-text-secondary"> |
| 85 | + {label} |
| 86 | + </Select.Label> |
| 87 | + <Select.Trigger className="flex min-h-9 w-full items-center justify-between gap-2 rounded-lg border border-border-default bg-background-surface px-3 text-left text-sm text-text-primary transition-colors hover:border-border-strong focus-visible:border-border-focus focus-visible:outline-none pointer-coarse:min-h-11"> |
| 88 | + <Select.Value className="min-w-0 truncate" placeholder="All"> |
| 89 | + {selectionCount > 1 ? `${selectionCount} selected` : undefined} |
| 90 | + </Select.Value> |
| 91 | + <Select.Icon className="shrink-0 text-text-muted"> |
| 92 | + <CaretDownIcon size={14} aria-hidden="true" /> |
| 93 | + </Select.Icon> |
| 94 | + </Select.Trigger> |
| 95 | + <Select.Portal> |
| 96 | + <Select.Positioner |
| 97 | + align="start" |
| 98 | + sideOffset={4} |
| 99 | + alignItemWithTrigger={false} |
| 100 | + className="z-[1200]" |
| 101 | + > |
| 102 | + <Select.Popup className="flex max-h-[min(20rem,var(--available-height))] max-w-(--available-width) min-w-(--anchor-width) flex-col overflow-hidden rounded-lg border border-border-default bg-background-elevated shadow-lg outline-none"> |
| 103 | + <Select.List className="min-h-0 overflow-y-auto overscroll-contain p-1"> |
| 104 | + {choices.map(({ value, label }) => ( |
| 105 | + <Select.Item |
| 106 | + key={value} |
| 107 | + value={value} |
| 108 | + className="relative flex min-h-8 cursor-pointer items-center rounded-md py-1.5 pr-2 pl-7 text-sm text-text-primary outline-none data-highlighted:bg-surface-state-hover pointer-coarse:min-h-11" |
| 109 | + > |
| 110 | + <Select.ItemIndicator className="absolute left-2 flex"> |
| 111 | + <CheckIcon size={14} aria-hidden="true" /> |
| 112 | + </Select.ItemIndicator> |
| 113 | + <Select.ItemText>{label}</Select.ItemText> |
| 114 | + </Select.Item> |
| 115 | + ))} |
| 116 | + </Select.List> |
| 117 | + </Select.Popup> |
| 118 | + </Select.Positioner> |
| 119 | + </Select.Portal> |
| 120 | + </Select.Root> |
| 121 | + </div> |
| 122 | + ) |
| 123 | +} |
| 124 | + |
| 125 | +export function MaintainersDirectoryFilters({ |
| 126 | + search, |
| 127 | + onChange, |
| 128 | + clearAll, |
| 129 | +}: { |
| 130 | + search: DirectorySearch |
| 131 | + onChange: (updates: Partial<DirectorySearch>) => void |
| 132 | + clearAll?: () => void |
| 133 | +}) { |
| 134 | + const libraryChoices = libraries |
| 135 | + .filter( |
| 136 | + ({ id, visible }) => visible !== false || search.libraries?.includes(id), |
| 137 | + ) |
| 138 | + .map(({ id, name }) => ({ |
| 139 | + value: id, |
| 140 | + label: name.replace(/^TanStack /, ''), |
| 141 | + })) |
| 142 | + |
| 143 | + return ( |
| 144 | + <section |
| 145 | + aria-label="Filter and sort maintainers" |
| 146 | + className="border-b border-border-subtle pb-4" |
| 147 | + > |
| 148 | + <div className="overflow-x-auto pb-1"> |
| 149 | + <div className="grid min-w-[42rem] grid-cols-5 gap-2"> |
| 150 | + <FilterSelect |
| 151 | + label="Libraries" |
| 152 | + choices={libraryChoices} |
| 153 | + multiple |
| 154 | + value={search.libraries ?? []} |
| 155 | + onValueChange={(libraries) => |
| 156 | + onChange({ libraries: libraries.length ? libraries : undefined }) |
| 157 | + } |
| 158 | + /> |
| 159 | + <FilterSelect |
| 160 | + label="Frameworks" |
| 161 | + choices={frameworkChoices} |
| 162 | + multiple |
| 163 | + value={search.frameworks ?? []} |
| 164 | + onValueChange={(frameworks) => |
| 165 | + onChange({ |
| 166 | + frameworks: frameworks.length ? frameworks : undefined, |
| 167 | + }) |
| 168 | + } |
| 169 | + /> |
| 170 | + <FilterSelect |
| 171 | + label="Specialties" |
| 172 | + choices={specialtyChoices} |
| 173 | + multiple |
| 174 | + value={search.specialties ?? []} |
| 175 | + onValueChange={(specialties) => |
| 176 | + onChange({ |
| 177 | + specialties: specialties.length ? specialties : undefined, |
| 178 | + }) |
| 179 | + } |
| 180 | + /> |
| 181 | + <FilterSelect |
| 182 | + label="Group by" |
| 183 | + choices={groupByChoices} |
| 184 | + value={search.groupBy} |
| 185 | + onValueChange={(groupBy) => |
| 186 | + onChange({ groupBy: groupBy ?? 'none' }) |
| 187 | + } |
| 188 | + /> |
| 189 | + <FilterSelect |
| 190 | + label="Sort by" |
| 191 | + choices={sortByChoices} |
| 192 | + value={search.sortBy} |
| 193 | + onValueChange={(sortBy) => onChange({ sortBy: sortBy ?? 'none' })} |
| 194 | + /> |
| 195 | + </div> |
| 196 | + </div> |
| 197 | + {clearAll && ( |
| 198 | + <div className="flex justify-end"> |
| 199 | + <button |
| 200 | + type="button" |
| 201 | + onClick={clearAll} |
| 202 | + className="min-h-9 text-sm text-text-muted underline-offset-2 hover:text-text-primary hover:underline" |
| 203 | + > |
| 204 | + Clear all |
| 205 | + </button> |
| 206 | + </div> |
| 207 | + )} |
| 208 | + </section> |
| 209 | + ) |
| 210 | +} |
0 commit comments