|
| 1 | +import { format } from 'date-fns' |
| 2 | +import { useRecoilValue } from 'recoil' |
| 3 | +import Button from '../components/Button' |
| 4 | +import ErrorMessage from '../components/ErrorMessage' |
| 5 | +import Page from '../components/Page' |
| 6 | +import DateCell from '../components/tables/cells/DateCell' |
| 7 | +import Table from '../components/tables/Table' |
| 8 | +import TableBody from '../components/tables/TableBody' |
| 9 | +import TableCell from '../components/tables/TableCell' |
| 10 | +import activeGameState, { SelectedActiveGame } from '../state/activeGameState' |
| 11 | +import useSortedItems from '../utils/useSortedItems' |
| 12 | +import { useNavigate, useParams } from 'react-router-dom' |
| 13 | +import routes from '../constants/routes' |
| 14 | +import { IconArrowRight } from '@tabler/icons-react' |
| 15 | +import clsx from 'clsx' |
| 16 | +import Pagination from '../components/Pagination' |
| 17 | +import TextInput from '../components/TextInput' |
| 18 | +import useSearch from '../utils/useSearch' |
| 19 | +import useChannelStorage from '../api/useChannelStorage' |
| 20 | +import { useEffect } from 'react' |
| 21 | + |
| 22 | +export default function ChannelStorage() { |
| 23 | + const { channelId } = useParams() |
| 24 | + const activeGame = useRecoilValue(activeGameState) as SelectedActiveGame |
| 25 | + |
| 26 | + const { search, setSearch, page, setPage, debouncedSearch } = useSearch() |
| 27 | + |
| 28 | + const { |
| 29 | + storageProps, |
| 30 | + channelName, |
| 31 | + loading, |
| 32 | + count, |
| 33 | + itemsPerPage, |
| 34 | + error, |
| 35 | + errorStatusCode |
| 36 | + } = useChannelStorage(activeGame, Number(channelId!), debouncedSearch, page) |
| 37 | + |
| 38 | + const sortedProps = useSortedItems(storageProps, 'updatedAt') |
| 39 | + |
| 40 | + const navigate = useNavigate() |
| 41 | + |
| 42 | + const goToPlayer = (identifier: string) => { |
| 43 | + navigate(`${routes.players}?search=${identifier}`) |
| 44 | + } |
| 45 | + |
| 46 | + const channelNotFound = errorStatusCode === 404 |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + if (channelNotFound) { |
| 50 | + navigate(routes.channels, { replace: true }) |
| 51 | + } |
| 52 | + }, [channelNotFound, navigate]) |
| 53 | + |
| 54 | + if (channelNotFound) { |
| 55 | + return null |
| 56 | + } |
| 57 | + |
| 58 | + return ( |
| 59 | + <Page |
| 60 | + showBackButton |
| 61 | + title={channelName ? `${channelName} storage` : 'Channel storage'} |
| 62 | + isLoading={loading} |
| 63 | + > |
| 64 | + {(sortedProps.length > 0 || debouncedSearch.length > 0) && |
| 65 | + <div className='flex items-center'> |
| 66 | + <div className='w-1/2 grow md:grow-0 md:w-[400px]'> |
| 67 | + <TextInput |
| 68 | + id='storage-search' |
| 69 | + type='search' |
| 70 | + placeholder='Search...' |
| 71 | + onChange={setSearch} |
| 72 | + value={search} |
| 73 | + /> |
| 74 | + </div> |
| 75 | + {Boolean(count) && <span className='ml-4'>{count} storage {count === 1 ? 'prop' : 'props'}</span>} |
| 76 | + </div> |
| 77 | + } |
| 78 | + |
| 79 | + {!error && !loading && storageProps.length === 0 && |
| 80 | + <> |
| 81 | + {debouncedSearch.length > 0 && |
| 82 | + <p>No storage props match your query</p> |
| 83 | + } |
| 84 | + {debouncedSearch.length === 0 && channelName && |
| 85 | + <p>{channelName} doesn't have any storage props yet</p> |
| 86 | + } |
| 87 | + </> |
| 88 | + } |
| 89 | + |
| 90 | + {!error && storageProps.length > 0 && |
| 91 | + <> |
| 92 | + <Table columns={['Key', 'Value', 'Created by', 'Last updated by', 'Created at', 'Updated at']}> |
| 93 | + <TableBody iterator={sortedProps}> |
| 94 | + {(storageProp) => ( |
| 95 | + <> |
| 96 | + <TableCell>{storageProp.key}</TableCell> |
| 97 | + <TableCell>{storageProp.value}</TableCell> |
| 98 | + <TableCell> |
| 99 | + <div className='flex items-center'> |
| 100 | + <span>{storageProp.createdBy.identifier}</span> |
| 101 | + <Button |
| 102 | + variant='icon' |
| 103 | + className={clsx('ml-2 p-1 rounded-full bg-indigo-900')} |
| 104 | + onClick={() => goToPlayer(storageProp.createdBy.identifier)} |
| 105 | + icon={<IconArrowRight size={16} />} |
| 106 | + /> |
| 107 | + </div> |
| 108 | + </TableCell> |
| 109 | + <TableCell> |
| 110 | + <div className='flex items-center'> |
| 111 | + <span>{storageProp.lastUpdatedBy.identifier}</span> |
| 112 | + <Button |
| 113 | + variant='icon' |
| 114 | + className={clsx('ml-2 p-1 rounded-full bg-indigo-900')} |
| 115 | + onClick={() => goToPlayer(storageProp.lastUpdatedBy.identifier)} |
| 116 | + icon={<IconArrowRight size={16} />} |
| 117 | + /> |
| 118 | + </div> |
| 119 | + </TableCell> |
| 120 | + <DateCell>{format(new Date(storageProp.createdAt), 'dd MMM yyyy, HH:mm')}</DateCell> |
| 121 | + <DateCell>{format(new Date(storageProp.updatedAt), 'dd MMM yyyy, HH:mm')}</DateCell> |
| 122 | + </> |
| 123 | + )} |
| 124 | + </TableBody> |
| 125 | + </Table> |
| 126 | + |
| 127 | + {count && itemsPerPage && ( |
| 128 | + <Pagination count={count} pageState={[page, setPage]} itemsPerPage={itemsPerPage} /> |
| 129 | + )} |
| 130 | + </> |
| 131 | + } |
| 132 | + |
| 133 | + {error && <ErrorMessage error={error} />} |
| 134 | + </Page> |
| 135 | + ) |
| 136 | +} |
0 commit comments