|
| 1 | +import { useMemo } from 'react' |
| 2 | +import type { ColumnSort } from '@tanstack/react-table' |
| 3 | +import { DataTypeUtils, type DataType } from './data-types' |
| 4 | +import { useHightideTranslation } from '@/src/i18n/useHightideTranslation' |
| 5 | +import { ArrowDownWideNarrow, ArrowUpNarrowWide, PlusIcon, TrashIcon, XIcon } from 'lucide-react' |
| 6 | +import { PopUpRoot } from '../../layout/popup/PopUpRoot' |
| 7 | +import { PopUp } from '../../layout/popup/PopUp' |
| 8 | +import { PopUpOpener } from '../../layout/popup/PopUpOpener' |
| 9 | +import { Button } from '../Button' |
| 10 | +import { Combobox } from '@/src/components/user-interaction/Combobox/Combobox' |
| 11 | +import { ComboboxOption } from '@/src/components/user-interaction/Combobox/ComboboxOption' |
| 12 | +import { PopUpContext } from '../../layout/popup/PopUpContext' |
| 13 | +import { IconButton } from '../IconButton' |
| 14 | +import clsx from 'clsx' |
| 15 | + |
| 16 | +export interface SortingListItem { |
| 17 | + id: string, |
| 18 | + label: string, |
| 19 | + dataType: DataType, |
| 20 | +} |
| 21 | + |
| 22 | +export interface SortingListProps { |
| 23 | + sorting: ColumnSort[], |
| 24 | + onSortingChange: (sorting: ColumnSort[]) => void, |
| 25 | + availableItems: SortingListItem[], |
| 26 | +} |
| 27 | + |
| 28 | +export const SortingList = ({ sorting, onSortingChange, availableItems }: SortingListProps) => { |
| 29 | + const translation = useHightideTranslation() |
| 30 | + const activeIds = useMemo(() => sorting.map((item) => item.id), [sorting]) |
| 31 | + const inactiveItems = useMemo( |
| 32 | + () => |
| 33 | + availableItems.filter((item) => !activeIds.includes(item.id)).sort((a, b) => a.label.localeCompare(b.label)), |
| 34 | + [availableItems, activeIds] |
| 35 | + ) |
| 36 | + const itemRecord = useMemo( |
| 37 | + () => |
| 38 | + availableItems.reduce( |
| 39 | + (acc, item) => { |
| 40 | + acc[item.id] = item |
| 41 | + return acc |
| 42 | + }, |
| 43 | + {} as Record<string, SortingListItem> |
| 44 | + ), |
| 45 | + [availableItems] |
| 46 | + ) |
| 47 | + |
| 48 | + const setSortDirection = (columnId: string, desc: boolean) => { |
| 49 | + onSortingChange(sorting.map((s) => (s.id === columnId ? { ...s, desc } : s))) |
| 50 | + } |
| 51 | + |
| 52 | + const removeSort = (columnId: string) => { |
| 53 | + onSortingChange(sorting.filter((s) => s.id !== columnId)) |
| 54 | + } |
| 55 | + |
| 56 | + return ( |
| 57 | + <div className="flex-row-2 flex-wrap gap-y-2"> |
| 58 | + <PopUpRoot> |
| 59 | + <PopUpOpener> |
| 60 | + {({ toggleOpen, props }) => ( |
| 61 | + <Button {...props} onClick={toggleOpen} color="neutral" size="sm" className="min-w-36"> |
| 62 | + {translation('addSorting')} |
| 63 | + <PlusIcon className="size-4" /> |
| 64 | + </Button> |
| 65 | + )} |
| 66 | + </PopUpOpener> |
| 67 | + <PopUp className="flex-col-2 p-2"> |
| 68 | + <PopUpContext.Consumer> |
| 69 | + {({ setIsOpen }) => ( |
| 70 | + <Combobox |
| 71 | + onItemClick={(id) => { |
| 72 | + const item = itemRecord[id] |
| 73 | + if (!item) return |
| 74 | + onSortingChange([...sorting, { id: item.id, desc: false }]) |
| 75 | + setIsOpen(false) |
| 76 | + }} |
| 77 | + > |
| 78 | + {inactiveItems.map((item) => ( |
| 79 | + <ComboboxOption key={item.id} value={item.id} label={item.label}> |
| 80 | + {DataTypeUtils.toIcon(item.dataType)} |
| 81 | + {item.label} |
| 82 | + </ComboboxOption> |
| 83 | + ))} |
| 84 | + </Combobox> |
| 85 | + )} |
| 86 | + </PopUpContext.Consumer> |
| 87 | + </PopUp> |
| 88 | + </PopUpRoot> |
| 89 | + {sorting.map((columnSort) => { |
| 90 | + const item = itemRecord[columnSort.id] |
| 91 | + if (!item) return null |
| 92 | + return ( |
| 93 | + <PopUpRoot key={columnSort.id}> |
| 94 | + <PopUpOpener> |
| 95 | + {({ toggleOpen, props }) => ( |
| 96 | + <Button {...props} onClick={toggleOpen} color="secondary" coloringStyle="tonal-outline" size="sm"> |
| 97 | + <span className="font-bold">{item.label}</span> |
| 98 | + {columnSort.desc ? <ArrowDownWideNarrow className="size-5"/> : <ArrowUpNarrowWide className="size-5"/>} |
| 99 | + </Button> |
| 100 | + )} |
| 101 | + </PopUpOpener> |
| 102 | + <PopUpContext.Consumer> |
| 103 | + {({ setIsOpen }) => ( |
| 104 | + <PopUp |
| 105 | + className={clsx('flex-col-3 p-3 min-w-64')} |
| 106 | + onClose={() => setIsOpen(false)} |
| 107 | + > |
| 108 | + <div className="flex-row-4 justify-between w-full items-center gap-2"> |
| 109 | + <span className="typography-title-sm font-semibold">{item.label}</span> |
| 110 | + <div className="flex-row-0 shrink-0 items-center"> |
| 111 | + <IconButton |
| 112 | + tooltip={translation('removeFilter')} |
| 113 | + onClick={() => { |
| 114 | + removeSort(columnSort.id) |
| 115 | + setIsOpen(false) |
| 116 | + }} |
| 117 | + color="negative" |
| 118 | + coloringStyle="text" |
| 119 | + size="sm" |
| 120 | + > |
| 121 | + <TrashIcon className="size-4" /> |
| 122 | + </IconButton> |
| 123 | + <IconButton |
| 124 | + tooltip={translation('close')} |
| 125 | + onClick={() => setIsOpen(false)} |
| 126 | + color="neutral" |
| 127 | + coloringStyle="text" |
| 128 | + size="sm" |
| 129 | + > |
| 130 | + <XIcon className="size-4" /> |
| 131 | + </IconButton> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + <div className="flex-row-1 w-full gap-2"> |
| 135 | + <Button |
| 136 | + type="button" |
| 137 | + className="flex-1 flex-row-1 items-center justify-center gap-2" |
| 138 | + color={columnSort.desc ? 'neutral' : 'primary'} |
| 139 | + coloringStyle="solid" |
| 140 | + size="md" |
| 141 | + onClick={() => setSortDirection(columnSort.id, false)} |
| 142 | + > |
| 143 | + <ArrowUpNarrowWide className="size-4" /> |
| 144 | + {translation('sortAsc')} |
| 145 | + </Button> |
| 146 | + <Button |
| 147 | + type="button" |
| 148 | + className="flex-1 flex-row-1 items-center justify-center gap-2" |
| 149 | + color={columnSort.desc ? 'primary' : 'neutral'} |
| 150 | + coloringStyle="solid" |
| 151 | + size="md" |
| 152 | + onClick={() => setSortDirection(columnSort.id, true)} |
| 153 | + > |
| 154 | + <ArrowDownWideNarrow className="size-4" /> |
| 155 | + {translation('sortDesc')} |
| 156 | + </Button> |
| 157 | + </div> |
| 158 | + </PopUp> |
| 159 | + )} |
| 160 | + </PopUpContext.Consumer> |
| 161 | + </PopUpRoot> |
| 162 | + ) |
| 163 | + })} |
| 164 | + </div> |
| 165 | + ) |
| 166 | +} |
0 commit comments