|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { |
| 4 | + type SandboxLifecycleEventType, |
| 5 | + SandboxLifecycleEventTypeSchema, |
| 6 | +} from '@/core/modules/sandboxes/lifecycle-event-types' |
| 7 | +import { Button } from '@/ui/primitives/button' |
| 8 | +import { |
| 9 | + DropdownMenu, |
| 10 | + DropdownMenuCheckboxItem, |
| 11 | + DropdownMenuContent, |
| 12 | + DropdownMenuSeparator, |
| 13 | + DropdownMenuTrigger, |
| 14 | +} from '@/ui/primitives/dropdown-menu' |
| 15 | +import { SandboxEventTypeBadge } from './event-type-badge' |
| 16 | +import { SANDBOX_EVENT_TYPE_MAP } from './event-type-map' |
| 17 | + |
| 18 | +const getTriggerLabel = (selected: SandboxLifecycleEventType[]) => { |
| 19 | + if (selected.length === SandboxLifecycleEventTypeSchema.options.length) |
| 20 | + return 'All' |
| 21 | + if (selected.length === 0) return 'None' |
| 22 | + const [first] = selected |
| 23 | + if (selected.length === 1 && first) return SANDBOX_EVENT_TYPE_MAP[first].label |
| 24 | + return `${selected.length}/${SandboxLifecycleEventTypeSchema.options.length}` |
| 25 | +} |
| 26 | + |
| 27 | +interface EventTypeFilterProps { |
| 28 | + types: SandboxLifecycleEventType[] |
| 29 | + onTypesChange: (types: SandboxLifecycleEventType[]) => void |
| 30 | +} |
| 31 | + |
| 32 | +export const EventTypeFilter = ({ |
| 33 | + types, |
| 34 | + onTypesChange, |
| 35 | +}: EventTypeFilterProps) => { |
| 36 | + const isAllSelected = |
| 37 | + types.length === SandboxLifecycleEventTypeSchema.options.length |
| 38 | + |
| 39 | + const toggleType = (type: SandboxLifecycleEventType) => { |
| 40 | + const next = types.includes(type) |
| 41 | + ? types.filter((t) => t !== type) |
| 42 | + : [...types, type] |
| 43 | + onTypesChange(next) |
| 44 | + } |
| 45 | + |
| 46 | + const toggleAll = (checked: boolean) => { |
| 47 | + onTypesChange(checked ? [...SandboxLifecycleEventTypeSchema.options] : []) |
| 48 | + } |
| 49 | + |
| 50 | + return ( |
| 51 | + <DropdownMenu> |
| 52 | + <DropdownMenuTrigger asChild> |
| 53 | + <Button variant="secondary" className="font-sans w-min normal-case"> |
| 54 | + Events · {getTriggerLabel(types)} |
| 55 | + </Button> |
| 56 | + </DropdownMenuTrigger> |
| 57 | + <DropdownMenuContent align="start"> |
| 58 | + <DropdownMenuCheckboxItem |
| 59 | + checked={isAllSelected} |
| 60 | + onCheckedChange={toggleAll} |
| 61 | + onSelect={(e) => e.preventDefault()} |
| 62 | + > |
| 63 | + All events |
| 64 | + </DropdownMenuCheckboxItem> |
| 65 | + <DropdownMenuSeparator /> |
| 66 | + {SandboxLifecycleEventTypeSchema.options.map((type) => ( |
| 67 | + <DropdownMenuCheckboxItem |
| 68 | + key={type} |
| 69 | + checked={types.includes(type)} |
| 70 | + onCheckedChange={() => toggleType(type)} |
| 71 | + onSelect={(e) => e.preventDefault()} |
| 72 | + > |
| 73 | + <SandboxEventTypeBadge type={type} /> |
| 74 | + </DropdownMenuCheckboxItem> |
| 75 | + ))} |
| 76 | + </DropdownMenuContent> |
| 77 | + </DropdownMenu> |
| 78 | + ) |
| 79 | +} |
0 commit comments