Files
Homarr/src/widgets/useNet/UsenetHistoryList.tsx

138 lines
3.6 KiB
TypeScript
Raw Normal View History

2022-08-26 11:10:40 +02:00
import {
Alert,
Center,
Code,
Group,
Pagination,
Skeleton,
2023-01-06 13:39:45 +09:00
Stack,
2022-08-26 11:10:40 +02:00
Table,
Text,
Title,
Tooltip,
} from '@mantine/core';
2022-12-11 16:06:41 +01:00
import { useElementSize } from '@mantine/hooks';
import { IconAlertCircle } from '@tabler/icons-react';
2022-08-25 21:07:41 +02:00
import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration';
2022-08-26 16:12:40 +02:00
import { useTranslation } from 'next-i18next';
2022-08-26 10:46:34 +02:00
import { FunctionComponent, useState } from 'react';
2023-07-21 18:08:40 +09:00
2023-07-28 23:09:21 +02:00
import { useGetUsenetHistory } from '../dashDot/api';
2023-09-03 16:23:40 +02:00
import { parseDuration } from '~/tools/client/parseDuration';
import { humanFileSize } from '~/tools/humanFileSize';
2022-08-25 21:07:41 +02:00
dayjs.extend(duration);
interface UsenetHistoryListProps {
appId: string;
2022-08-25 21:07:41 +02:00
}
2023-01-06 13:39:45 +09:00
const PAGE_SIZE = 13;
2022-08-25 21:07:41 +02:00
export const UsenetHistoryList: FunctionComponent<UsenetHistoryListProps> = ({ appId }) => {
2022-08-26 10:46:34 +02:00
const [page, setPage] = useState(1);
2022-08-26 21:38:28 +02:00
const { t } = useTranslation(['modules/usenet', 'common']);
2022-08-26 10:46:34 +02:00
2022-12-11 16:06:41 +01:00
const { ref, width, height } = useElementSize();
const durationBreakpoint = 400;
2022-08-26 11:10:40 +02:00
const { data, isLoading, isError, error } = useGetUsenetHistory({
2022-08-26 10:46:34 +02:00
limit: PAGE_SIZE,
offset: (page - 1) * PAGE_SIZE,
2023-01-05 22:42:56 +09:00
appId,
2022-08-26 10:46:34 +02:00
});
const totalPages = Math.ceil((data?.total || 1) / PAGE_SIZE);
if (isLoading) {
return (
<>
<Skeleton height={40} mt={10} />
<Skeleton height={40} mt={10} />
<Skeleton height={40} mt={10} />
</>
);
}
2022-08-26 11:10:40 +02:00
if (isError) {
return (
<Group position="center">
2022-08-26 21:38:28 +02:00
<Alert
icon={<IconAlertCircle size={16} />}
my="lg"
title={t('modules/usenet:history.error.title')}
color="red"
radius="md"
>
{t('modules/usenet:history.error.message')}
2022-08-26 11:10:40 +02:00
<Code mt="sm" block>
2023-06-10 18:19:06 +02:00
{error.message}
2022-08-26 11:10:40 +02:00
</Code>
</Alert>
</Group>
);
}
2022-08-26 10:46:34 +02:00
if (!data || data.items.length <= 0) {
2022-08-25 21:07:41 +02:00
return (
<Center style={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
2022-08-26 21:38:28 +02:00
<Title order={3}>{t('modules/usenet:history.empty')}</Title>
2022-08-25 21:07:41 +02:00
</Center>
);
}
return (
2023-01-06 13:39:45 +09:00
<Stack justify="space-around" spacing="xs">
<Table highlightOnHover style={{ tableLayout: 'fixed' }} ref={ref}>
<thead>
<tr>
<th>{t('modules/usenet:history.header.name')}</th>
<th style={{ width: 100 }}>{t('modules/usenet:history.header.size')}</th>
{durationBreakpoint < width ? (
<th style={{ width: 200 }}>{t('modules/usenet:history.header.duration')}</th>
) : null}
</tr>
</thead>
<tbody>
{data.items.map((history) => (
<tr key={history.id}>
<td>
<Tooltip position="top" label={history.name}>
<Text
size="xs"
style={{
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis',
}}
>
{history.name}
</Text>
</Tooltip>
</td>
<td>
<Text size="xs">{humanFileSize(history.size)}</Text>
</td>
2022-12-11 16:06:41 +01:00
{durationBreakpoint < width ? (
<td>
2023-01-06 13:39:45 +09:00
<Text size="xs">{parseDuration(history.time, t)}</Text>
2022-12-11 16:06:41 +01:00
</td>
2023-01-06 13:39:45 +09:00
) : null}
</tr>
))}
</tbody>
</Table>
2022-08-26 16:12:40 +02:00
{totalPages > 1 && (
<Pagination
2023-01-06 13:39:45 +09:00
noWrap
2022-08-26 16:12:40 +02:00
size="sm"
position="center"
mt="md"
total={totalPages}
2023-03-03 00:37:22 +09:00
value={page}
2022-08-26 16:12:40 +02:00
onChange={setPage}
/>
)}
2023-01-06 13:39:45 +09:00
</Stack>
2022-08-25 21:07:41 +02:00
);
};