import { Container, FormControl, Grid2, InputLabel, MenuItem, Pagination, Select } from '@mui/material' import { pipe } from 'fp-ts/lib/function' import { matchW } from 'fp-ts/lib/TaskEither' import { useAtomValue } from 'jotai' import { useEffect, useState, useTransition } from 'react' import { serverURL } from '../atoms/settings' import ArchiveCard from '../components/ArchiveCard' import EmptyArchive from '../components/EmptyArchive' import { useToast } from '../hooks/toast' import { ffetch } from '../lib/httpClient' import { ArchiveEntry, PaginatedResponse } from '../types' import LoadingBackdrop from '../components/LoadingBackdrop' const Archive: React.FC = () => { const [isLoading, setLoading] = useState(true) const [archiveEntries, setArchiveEntries] = useState() const [currentCursor, setCurrentCursor] = useState(0) const [cursor, setCursor] = useState({ first: 0, next: 0 }) const [pageSize, setPageSize] = useState(25) const [isPending, startTransition] = useTransition() const serverAddr = useAtomValue(serverURL) const { pushMessage } = useToast() const fetchArchived = (startCursor = 0) => pipe( ffetch>(`${serverAddr}/archive?id=${startCursor}&limit=${pageSize}`), matchW( (l) => pushMessage(l, 'error'), (r) => { setArchiveEntries(r.data) setCursor({ ...cursor, first: r.first, next: r.next }) } ) )() const softDelete = (id: string) => pipe( ffetch(`${serverAddr}/archive/soft/${id}`, { method: 'DELETE' }), matchW( (l) => pushMessage(l, 'error'), (_) => startTransition(async () => await fetchArchived()) ) )() const hardDelete = (id: string) => pipe( ffetch(`${serverAddr}/archive/hard/${id}`, { method: 'DELETE' }), matchW( (l) => pushMessage(l, 'error'), (_) => startTransition(async () => await fetchArchived()) ) )() const setPage = (page: number) => setCurrentCursor(pageSize * (page - 1)) useEffect(() => { fetchArchived(currentCursor).then(() => setLoading(false)) }, [currentCursor]) return ( { archiveEntries && archiveEntries.length !== 0 ? { archiveEntries.map((entry) => ( startTransition(async () => await softDelete(entry.id))} onHardDelete={() => startTransition(async () => await hardDelete(entry.id))} /> )) } : } setPage(v)} /> Page size ) } export default Archive