Files
Homarr/src/modules/usenet/UsenetHistoryList.tsx

134 lines
3.5 KiB
TypeScript
Raw Normal View History

2022-08-26 11:10:40 +02:00
import {
Alert,
Center,
Code,
Group,
Pagination,
Skeleton,
Table,
Text,
Title,
Tooltip,
} from '@mantine/core';
import { IconAlertCircle } from '@tabler/icons';
import { AxiosError } from 'axios';
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';
import { useGetUsenetHistory } from '../../tools/hooks/api';
2022-08-25 21:07:41 +02:00
import { humanFileSize } from '../../tools/humanFileSize';
2022-08-26 21:38:28 +02:00
import { parseDuration } from '../../tools/parseDuration';
2022-08-25 21:07:41 +02:00
dayjs.extend(duration);
interface UsenetHistoryListProps {
2022-08-26 10:46:34 +02:00
serviceId: string;
2022-08-25 21:07:41 +02:00
}
2022-08-26 10:46:34 +02:00
const PAGE_SIZE = 10;
2022-08-25 21:07:41 +02:00
2022-08-26 10:46:34 +02:00
export const UsenetHistoryList: FunctionComponent<UsenetHistoryListProps> = ({ serviceId }) => {
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-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,
serviceId,
});
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>
{(error as AxiosError)?.response?.data as string}
</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 (
2022-08-26 16:12:40 +02:00
<>
2022-08-26 10:46:34 +02:00
<Table highlightOnHover style={{ tableLayout: 'fixed' }}>
<colgroup>
<col span={1} />
<col span={1} style={{ width: 100 }} />
<col span={1} style={{ width: 200 }} />
</colgroup>
<thead>
<tr>
2022-08-26 21:38:28 +02:00
<th>{t('modules/usenet:history.header.name')}</th>
<th>{t('modules/usenet:history.header.size')}</th>
<th>{t('modules/usenet:history.header.duration')}</th>
2022-08-25 21:07:41 +02:00
</tr>
2022-08-26 10:46:34 +02:00
</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>
<td>
2022-08-26 21:38:28 +02:00
<Text size="xs">{parseDuration(history.time, t)}</Text>
2022-08-26 10:46:34 +02:00
</td>
</tr>
))}
</tbody>
</Table>
2022-08-26 16:12:40 +02:00
{totalPages > 1 && (
<Pagination
size="sm"
position="center"
mt="md"
total={totalPages}
page={page}
onChange={setPage}
/>
)}
</>
2022-08-25 21:07:41 +02:00
);
};