|
| 1 | +'use client' |
| 2 | + |
| 3 | +import React from 'react' |
| 4 | + |
| 5 | +import { SlidersHorizontalIcon, TrashIcon } from '@phosphor-icons/react' |
| 6 | + |
| 7 | +import { Button } from './button' |
| 8 | +import { Checkbox } from './checkbox' |
| 9 | +import { Popover, PopoverContent, PopoverTrigger } from './popover' |
| 10 | +import { Typography } from './typography' |
| 11 | + |
| 12 | +import { cn } from '../../utils/cn' |
| 13 | + |
| 14 | +export type FilterItem = { |
| 15 | + column: string |
| 16 | + value: string |
| 17 | + isSelected?: boolean |
| 18 | + label: string |
| 19 | + description?: string |
| 20 | +} |
| 21 | + |
| 22 | +interface FilterProps { |
| 23 | + items: FilterItem[] |
| 24 | + onChange: (items: FilterItem[]) => void |
| 25 | +} |
| 26 | + |
| 27 | +export function getSelectedValues(column: string, items: FilterItem[]) { |
| 28 | + return items.filter((item) => item.column === column && item.isSelected).map((item) => item.value) |
| 29 | +} |
| 30 | + |
| 31 | +export function Filter({ items, onChange }: FilterProps) { |
| 32 | + const [openModal, setOpenModal] = React.useState(false) |
| 33 | + const [internalItems, setInternalItems] = React.useState(items) |
| 34 | + const [selectedColumn, setSelectedColumn] = React.useState<string | null>( |
| 35 | + items.length > 0 ? items[0].column : null |
| 36 | + ) |
| 37 | + |
| 38 | + function toggleItem(column: string, value: string) { |
| 39 | + const newItems = internalItems.map((item) => ({ |
| 40 | + ...item, |
| 41 | + isSelected: |
| 42 | + item.column === column && item.value === value ? !item.isSelected : item.isSelected, |
| 43 | + })) |
| 44 | + setInternalItems(newItems) |
| 45 | + } |
| 46 | + |
| 47 | + function apply() { |
| 48 | + onChange(internalItems) |
| 49 | + setOpenModal(false) |
| 50 | + } |
| 51 | + |
| 52 | + function reset() { |
| 53 | + const newItems = internalItems.map((item) => ({ ...item, isSelected: false })) |
| 54 | + setInternalItems(newItems) |
| 55 | + onChange(newItems) |
| 56 | + setOpenModal(false) |
| 57 | + } |
| 58 | + |
| 59 | + function columnCount(column: string) { |
| 60 | + return internalItems.reduce( |
| 61 | + (acc, item) => acc + (item.column === column && item.isSelected ? 1 : 0), |
| 62 | + 0 |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + const columns = Array.from(new Set(internalItems.map((item) => item.column))) |
| 67 | + const totalSelected = internalItems.reduce((acc, item) => acc + (item.isSelected ? 1 : 0), 0) |
| 68 | + |
| 69 | + return ( |
| 70 | + <Popover open={openModal} onOpenChange={setOpenModal}> |
| 71 | + <PopoverTrigger asChild> |
| 72 | + <Button variant="outline" className="w-fit"> |
| 73 | + <Typography>Filter</Typography> |
| 74 | + <CountBadge count={totalSelected} /> |
| 75 | + <SlidersHorizontalIcon /> |
| 76 | + </Button> |
| 77 | + </PopoverTrigger> |
| 78 | + <PopoverContent asChild> |
| 79 | + <div className="w-fit py-6 bg-surface-primary border border-border-primary rounded-xl flex flex-col gap-4 min-w-[600px] h-[436px]"> |
| 80 | + <Typography type="h4" weight="bold" className="px-6 text-lg w-full"> |
| 81 | + Apply Filters |
| 82 | + </Typography> |
| 83 | + |
| 84 | + <div className="flex items-start gap-6 w-full px-6 flex-1 min-h-0"> |
| 85 | + <ul className="min-w-[230px] overflow-auto h-full"> |
| 86 | + {columns.map((column) => ( |
| 87 | + <li |
| 88 | + key={column} |
| 89 | + className="w-full p-4 rounded-sm hover:bg-surface-primary-hover flex items-center cursor-pointer justify-between" |
| 90 | + onClick={() => setSelectedColumn(column)} |
| 91 | + > |
| 92 | + <Typography weight="normal" type="body"> |
| 93 | + Columns {column} |
| 94 | + </Typography> |
| 95 | + <CountBadge count={columnCount(column)} /> |
| 96 | + </li> |
| 97 | + ))} |
| 98 | + </ul> |
| 99 | + |
| 100 | + <ul className="flex-1 h-full border border-border-primary p-3 rounded-lg overflow-auto"> |
| 101 | + {internalItems |
| 102 | + .filter((item) => item.column === selectedColumn) |
| 103 | + .map((item) => ( |
| 104 | + <li |
| 105 | + key={`${item.column}-${item.value}`} |
| 106 | + className="w-full flex gap-4 items-start p-4 cursor-pointer" |
| 107 | + onClick={() => toggleItem(item.column, item.value)} |
| 108 | + > |
| 109 | + <Checkbox checked={item.isSelected} className="mt-1" /> |
| 110 | + <div> |
| 111 | + <Typography weight="medium" type="body" className="block"> |
| 112 | + {item.label} |
| 113 | + </Typography> |
| 114 | + {item.description && ( |
| 115 | + <Typography |
| 116 | + weight="normal" |
| 117 | + type="caption" |
| 118 | + className="text-text-secondary block" |
| 119 | + > |
| 120 | + {item.description} |
| 121 | + </Typography> |
| 122 | + )} |
| 123 | + </div> |
| 124 | + </li> |
| 125 | + ))} |
| 126 | + </ul> |
| 127 | + </div> |
| 128 | + |
| 129 | + <div className="w-full px-6 pt-4 border-t flex items-center justify-end gap-2 border-border-primary"> |
| 130 | + <Button variant="outline" onClick={reset}> |
| 131 | + <TrashIcon /> |
| 132 | + <Typography>Reset All</Typography> |
| 133 | + </Button> |
| 134 | + <Button onClick={apply}>Apply</Button> |
| 135 | + </div> |
| 136 | + </div> |
| 137 | + </PopoverContent> |
| 138 | + </Popover> |
| 139 | + ) |
| 140 | +} |
| 141 | + |
| 142 | +function CountBadge({ count }: { count: number }) { |
| 143 | + return ( |
| 144 | + <div |
| 145 | + className={cn( |
| 146 | + 'size-[22px] rounded-full bg-surface-primary border flex items-center justify-center', |
| 147 | + { |
| 148 | + 'opacity-0': count === 0, |
| 149 | + } |
| 150 | + )} |
| 151 | + > |
| 152 | + {count} |
| 153 | + </div> |
| 154 | + ) |
| 155 | +} |
0 commit comments