|
| 1 | +import { FieldSettings, useFileTableSettingsStore } from '@/lib/store/fileTableSettings'; |
| 2 | +import { |
| 3 | + closestCenter, |
| 4 | + DndContext, |
| 5 | + DragEndEvent, |
| 6 | + KeyboardSensor, |
| 7 | + PointerSensor, |
| 8 | + useSensor, |
| 9 | + useSensors, |
| 10 | +} from '@dnd-kit/core'; |
| 11 | +import { SortableContext, useSortable, verticalListSortingStrategy } from '@dnd-kit/sortable'; |
| 12 | +import { CSS } from '@dnd-kit/utilities'; |
| 13 | +import { Button, Checkbox, Group, Modal, Paper, Text } from '@mantine/core'; |
| 14 | +import { IconGripVertical } from '@tabler/icons-react'; |
| 15 | +import { useShallow } from 'zustand/shallow'; |
| 16 | + |
| 17 | +export const NAMES = { |
| 18 | + name: 'Name', |
| 19 | + originalName: 'Original Name', |
| 20 | + tags: 'Tags', |
| 21 | + type: 'Type', |
| 22 | + size: 'Size', |
| 23 | + createdAt: 'Created At', |
| 24 | + favorite: 'Favorite', |
| 25 | + views: 'Views', |
| 26 | +}; |
| 27 | + |
| 28 | +function SortableTableField({ item }: { item: FieldSettings }) { |
| 29 | + const setVisible = useFileTableSettingsStore((state) => state.setVisible); |
| 30 | + |
| 31 | + const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({ |
| 32 | + id: item.field, |
| 33 | + }); |
| 34 | + |
| 35 | + const style: React.CSSProperties = { |
| 36 | + transform: CSS.Transform.toString(transform), |
| 37 | + transition, |
| 38 | + opacity: isDragging ? 0.5 : 1, |
| 39 | + cursor: 'grab', |
| 40 | + width: '100%', |
| 41 | + }; |
| 42 | + |
| 43 | + return ( |
| 44 | + <Paper withBorder p='xs' ref={setNodeRef} style={style} {...attributes} {...listeners}> |
| 45 | + <Group gap='xs'> |
| 46 | + <IconGripVertical size='1rem' /> |
| 47 | + |
| 48 | + <Checkbox checked={item.visible} onChange={() => setVisible(item.field, !item.visible)} /> |
| 49 | + |
| 50 | + <Text>{NAMES[item.field]}</Text> |
| 51 | + </Group> |
| 52 | + </Paper> |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +export default function TableEditModal({ opened, onCLose }: { opened: boolean; onCLose: () => void }) { |
| 57 | + const [fields, setIndex, reset] = useFileTableSettingsStore( |
| 58 | + useShallow((state) => [state.fields, state.setIndex, state.reset]), |
| 59 | + ); |
| 60 | + |
| 61 | + const sensors = useSensors( |
| 62 | + useSensor(PointerSensor, { activationConstraint: { distance: 5 } }), |
| 63 | + useSensor(KeyboardSensor), |
| 64 | + ); |
| 65 | + |
| 66 | + const handleDragEnd = (event: DragEndEvent) => { |
| 67 | + const { active, over } = event; |
| 68 | + if (active.id !== over?.id) { |
| 69 | + const newIndex = fields.findIndex((item) => item.field === over?.id); |
| 70 | + |
| 71 | + setIndex(active.id as FieldSettings['field'], newIndex); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + return ( |
| 76 | + <Modal opened={opened} onClose={onCLose} title='Table Options' centered> |
| 77 | + <Text mb='md' size='sm' c='dimmed'> |
| 78 | + Select and drag fields below to make them appear/disappear/reorder in the file table view. |
| 79 | + </Text> |
| 80 | + |
| 81 | + <DndContext sensors={sensors} collisionDetection={closestCenter} onDragEnd={handleDragEnd}> |
| 82 | + <SortableContext items={fields.map((item) => item.field)} strategy={verticalListSortingStrategy}> |
| 83 | + {fields.map((item, index) => ( |
| 84 | + <div |
| 85 | + key={index} |
| 86 | + style={{ display: 'flex', alignItems: 'center', gap: '8px', marginBottom: '8px' }} |
| 87 | + > |
| 88 | + <SortableTableField item={item} /> |
| 89 | + </div> |
| 90 | + ))} |
| 91 | + </SortableContext> |
| 92 | + </DndContext> |
| 93 | + |
| 94 | + <Button fullWidth color='red' onClick={() => reset()} variant='light' mt='md'> |
| 95 | + Reset to Default |
| 96 | + </Button> |
| 97 | + </Modal> |
| 98 | + ); |
| 99 | +} |
0 commit comments