|
| 1 | +import { Box, Button, Flex, Stack, Table, Text } from '@mantine/core'; |
| 2 | +import { useModals } from '@mantine/modals'; |
| 3 | +import { IconTrash } from '@tabler/icons-react'; |
| 4 | +import { observer } from 'mobx-react-lite'; |
| 5 | +import { useEditDashboardContext } from '~/contexts'; |
| 6 | + |
| 7 | +export const EditQueries = observer(() => { |
| 8 | + const modals = useModals(); |
| 9 | + const model = useEditDashboardContext(); |
| 10 | + const navigateToQuery = (queryID: string) => { |
| 11 | + model.editor.setPath(['_QUERIES_', queryID]); |
| 12 | + }; |
| 13 | + |
| 14 | + const removeUnusedQueriesWithConfirmation = () => { |
| 15 | + modals.openConfirmModal({ |
| 16 | + title: 'Delete ununsed queries?', |
| 17 | + children: <Text size="sm">This action cannot be undone.</Text>, |
| 18 | + labels: { confirm: 'Confirm', cancel: 'Cancel' }, |
| 19 | + onCancel: () => console.log('Cancel'), |
| 20 | + onConfirm: () => model.content.removeUnusedQueries(), |
| 21 | + confirmProps: { color: 'red' }, |
| 22 | + zIndex: 320, |
| 23 | + }); |
| 24 | + }; |
| 25 | + |
| 26 | + const usages = model.content.queriesUsage; |
| 27 | + return ( |
| 28 | + <Stack sx={{ height: '100%' }} spacing="sm" pb={'59px'}> |
| 29 | + <Box pt={9} pb={8} sx={{ borderBottom: '1px solid #eee' }}> |
| 30 | + <Text px="md" align="left" sx={{ userSelect: 'none', cursor: 'default' }}> |
| 31 | + Manage Queries |
| 32 | + </Text> |
| 33 | + </Box> |
| 34 | + <Flex justify="flex-end" align="center" px={12}> |
| 35 | + <Button |
| 36 | + variant="subtle" |
| 37 | + size="xs" |
| 38 | + color="red" |
| 39 | + leftIcon={<IconTrash size={14} />} |
| 40 | + disabled={!model.content.hasUnusedQueries} |
| 41 | + onClick={removeUnusedQueriesWithConfirmation} |
| 42 | + > |
| 43 | + Delete unused queries |
| 44 | + </Button> |
| 45 | + </Flex> |
| 46 | + <Box sx={{ flexGrow: 1, overflow: 'auto' }}> |
| 47 | + <Table fontSize="sm" highlightOnHover sx={{ tableLayout: 'fixed' }}> |
| 48 | + <thead> |
| 49 | + <tr> |
| 50 | + <th>Name</th> |
| 51 | + <th style={{ width: '200px' }}>Data Source</th> |
| 52 | + <th style={{ width: '100px', textAlign: 'right' }}>Type</th> |
| 53 | + <th style={{ width: '100px', textAlign: 'center' }}>Usage</th> |
| 54 | + <th style={{ width: '300px', paddingLeft: '24px' }}>Action</th> |
| 55 | + </tr> |
| 56 | + </thead> |
| 57 | + <tbody> |
| 58 | + {model.content.queries.sortedList.map((q) => { |
| 59 | + const usageCount = usages[q.id]?.length ?? 0; |
| 60 | + return ( |
| 61 | + <tr key={q.id}> |
| 62 | + <td>{q.name}</td> |
| 63 | + <td>{q.key}</td> |
| 64 | + <td style={{ textAlign: 'right' }}>{q.type}</td> |
| 65 | + <td |
| 66 | + style={{ |
| 67 | + color: usageCount === 0 ? '#ff0000' : '#000', |
| 68 | + fontWeight: usageCount === 0 ? 'bold' : 'normal', |
| 69 | + textAlign: 'center', |
| 70 | + }} |
| 71 | + > |
| 72 | + {usageCount} |
| 73 | + </td> |
| 74 | + <td> |
| 75 | + <Button variant="subtle" size="xs" onClick={() => navigateToQuery(q.id)}> |
| 76 | + Open |
| 77 | + </Button> |
| 78 | + </td> |
| 79 | + </tr> |
| 80 | + ); |
| 81 | + })} |
| 82 | + </tbody> |
| 83 | + </Table> |
| 84 | + </Box> |
| 85 | + </Stack> |
| 86 | + ); |
| 87 | +}); |
0 commit comments