2022-04-15 18:45:22 +00:00
|
|
|
import { useState } from "react";
|
2022-03-13 09:57:20 +00:00
|
|
|
import useSWR from "swr";
|
|
|
|
import { useMedia } from "react-use";
|
|
|
|
|
|
|
|
import theme from "../../lib/theme";
|
|
|
|
|
|
|
|
import { ContainerLoadingIndicator } from "../LoadingIndicator";
|
|
|
|
|
|
|
|
import FileTable from "./FileTable";
|
|
|
|
import useFormattedFilesData from "./useFormattedFilesData";
|
2022-04-15 18:45:22 +00:00
|
|
|
import { MobileFileList } from "./MobileFileList";
|
|
|
|
import { Pagination } from "./Pagination";
|
|
|
|
|
|
|
|
const PAGE_SIZE = 10;
|
2022-03-13 09:57:20 +00:00
|
|
|
|
|
|
|
const FileList = ({ type }) => {
|
|
|
|
const isMediumScreenOrLarger = useMedia(`(min-width: ${theme.screens.md})`);
|
2022-04-15 18:45:22 +00:00
|
|
|
const [offset, setOffset] = useState(0);
|
|
|
|
const baseUrl = `user/${type}?pageSize=${PAGE_SIZE}`;
|
|
|
|
const {
|
|
|
|
data,
|
|
|
|
error,
|
|
|
|
mutate: refreshList,
|
|
|
|
} = useSWR(`${baseUrl}&offset=${offset}`, {
|
|
|
|
revalidateOnMount: true,
|
|
|
|
});
|
2022-03-13 09:57:20 +00:00
|
|
|
const items = useFormattedFilesData(data?.items || []);
|
2022-04-15 18:45:22 +00:00
|
|
|
const count = data?.count || 0;
|
2022-03-13 09:57:20 +00:00
|
|
|
|
2022-04-15 18:45:22 +00:00
|
|
|
// Next page preloading
|
|
|
|
const hasMoreRecords = data ? data.offset + data.pageSize < data.count : false;
|
|
|
|
const nextPageOffset = hasMoreRecords ? data.offset + data.pageSize : offset;
|
|
|
|
useSWR(`${baseUrl}&offset=${nextPageOffset}`);
|
2022-03-13 09:57:20 +00:00
|
|
|
|
|
|
|
if (!items.length) {
|
|
|
|
return (
|
|
|
|
<div className="flex w-full h-full justify-center items-center text-palette-400">
|
|
|
|
{!data && !error && <ContainerLoadingIndicator />}
|
|
|
|
{!data && error && <p>An error occurred while loading this data.</p>}
|
|
|
|
{data && <p>No {type} found.</p>}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-04-15 18:45:22 +00:00
|
|
|
<>
|
|
|
|
{isMediumScreenOrLarger ? (
|
|
|
|
<FileTable onUpdated={refreshList} items={items} />
|
|
|
|
) : (
|
|
|
|
<MobileFileList items={items} onUpdated={refreshList} />
|
|
|
|
)}
|
|
|
|
<Pagination count={count} offset={offset} setOffset={setOffset} pageSize={PAGE_SIZE} />
|
|
|
|
</>
|
2022-03-13 09:57:20 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default FileList;
|