Skip to content

A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

11.1 KB
Raw Blame
import type { Ref } from "react";
import { i18n } from "@lingui/core";
import { ph, plural, t } from "@lingui/core/macro";
import { Trans, useLingui } from "@lingui/react/macro";
import { useHotkey } from "@tanstack/react-hotkeys";
import { useQuery } from "@tanstack/react-query";
import { Link } from "@tanstack/react-router";
import { useRef, useState } from "react";
import { Button, buttonVariants } from "@reactive-resume/ui/components/button";
import { Checkbox } from "@reactive-resume/ui/components/checkbox";
import { Icon } from "@reactive-resume/ui/components/icon";
import { IconButton } from "@reactive-resume/ui/components/icon-button";
import { useFloatingToolbarSpace } from "@reactive-resume/ui/hooks/use-floating-toolbar-space";
import { cn } from "@reactive-resume/utils/style";
import { useBatchActions } from "./document-actions";
import { DocumentRow } from "./document-card";
import { LibrarySkeleton } from "./documents-page";
import { documentKey } from "./filter";
import { LibraryError } from "./library-error";
import { useDocumentHistory } from "./library-session";
import { PageNavigation } from "./page-navigation";
import { useDocumentRemovalMotion } from "./removal-motion";
import { PageHeader } from "@/components/layout/page-header";
import { useConfirm } from "@/hooks/use-confirm";
import { orpc } from "@/libs/orpc/client";

const noop = () => undefined;

/**
 * Trash: documents stay 30 days, then go for good. Rows can be selected for Restore or Delete permanently; Empty
 * trash is a separate server-side operation that covers every document in Trash, not only the loaded or selected IDs.
 */
export function TrashPage() {
	const { t } = useLingui();
	const [offset, setOffset] = useState(0);
	const [selected, setSelected] = useState<ReadonlySet<string>>(() => new Set());
	const [busy, setBusy] = useState(false);
	const confirm = useConfirm();
	const resultsRef = useRef<HTMLTableElement>(null);
	const { ref: selectionBarRef, space: selectionBarSpace } = useFloatingToolbarSpace();
	const {
		data: page,
		isPending,
		isError,
		isFetching,
		isPlaceholderData,
		refetch,
	} = useQuery({
		...orpc.documents.page.queryOptions({ input: { trashed: true, limit: 100, offset } }),
		gcTime: 0,
		placeholderData: (previous) => previous,
	});
	const [lastPage, setLastPage] = useState({ data: page, offset });
	if (page && !isPlaceholderData && (page !== lastPage.data || offset !== lastPage.offset))
		setLastPage({ data: page, offset });
	const displayed = page ?? lastPage.data;
	const displayedOffset = page && !isPlaceholderData ? offset : lastPage.offset;
	const rowsCurrent = Boolean(page) && !isPlaceholderData && !isError;
	if (page && !isPlaceholderData && offset > 0 && offset >= page.total) {
		setOffset(Math.max(0, Math.floor((page.total - 1) / 100) * 100));
		setSelected(new Set());
	}
	useDocumentRemovalMotion({
		container: resultsRef,
		scope: String(displayedOffset),
		trashed: true,
		data: displayed,
		fetching: isFetching,
		failed: isError,
	});
	const batch = useBatchActions();
	useDocumentHistory(Boolean(page && page.total > 0));
	const documents = displayed?.items;
	const all = documents ?? [];
	const selection = rowsCurrent ? all.filter((document) => selected.has(documentKey(document))) : [];
	const selecting = selection.length > 0;
	const allSelected = all.length > 0 && selection.length === all.length;

	const clearSelection = () => setSelected(new Set());
	const selectAll = (on: boolean) => {
		if (on && !rowsCurrent) return;
		setSelected(new Set(on ? all.map(documentKey) : []));
	};
	const setSelectedFor = (document: (typeof all)[number], on: boolean) => {
		if (!rowsCurrent) return;
		setSelected((current) => {
			const next = new Set(current);
			if (on) next.add(documentKey(document));
			else next.delete(documentKey(document));
			return next;
		});
	};

	useHotkey(
		"Escape",
		() => {
			if (selecting && !busy) clearSelection();
		},
		{ meta: selecting && !busy ? { name: t`Clear selection` } : {} },
	);

	const run = async (action: () => Promise<boolean>) => {
		if (!rowsCurrent) return;
		setBusy(true);
		try {
			// A partial or failed batch keeps the remaining selection so the user can retry it.
			if (await action()) clearSelection();
		} catch {
			// The action reports its own error; the selection stays so the user can retry.
		} finally {
			setBusy(false);
		}
	};

	const purgeSelected = async () => {
		if (!rowsCurrent || !selection.length) return;
		const confirmed = await confirm(
			selection.length === 1
				? t`Delete “${selection[0]?.name ?? ""}” forever?`
				: plural(selection.length, {
						one: "Delete # selected document forever?",
						other: "Delete # selected documents forever?",
					}),
			{
				description:
					selection.length === 1
						? t`The selected document will be deleted for good and can't be restored.`
						: t`The selected documents will be deleted for good and can't be restored.`,
				confirmText: t`Hold to delete`,
				cancelText: t`Keep in Trash`,
				hold: true,
			},
		);
		if (!confirmed) return;
		await run(() => batch.purge(selection));
	};

	const emptyTrash = async () => {
		const confirmed = await confirm(t`Empty Trash?`, {
			description: t`All documents in Trash will be deleted for good and can't be restored.`,
			confirmText: t`Hold to delete`,
			cancelText: t`Keep in Trash`,
			hold: true,
		});
		if (!confirmed) return;
		await run(() => batch.emptyTrash());
	};

	return (
		<div
			className="mx-auto grid w-full max-w-[1180px] content-start gap-5 px-8 py-8 max-sm:px-4 max-sm:py-5"
			style={{ paddingBottom: selecting ? selectionBarSpace : undefined }}
		>
			<PageHeader
				back={
					<Link
						to="/dashboard"
						className={buttonVariants({ variant: "ghost", size: "sm", className: "-ms-2.5 w-fit gap-1.5 text-ink-2" })}
					>
						<Icon name="arrow-left" size={18} />
						<Trans>Back to documents</Trans>
					</Link>
				}
				title={<Trans>Trash</Trans>}
				description={<Trans>Documents are deleted permanently after 30 days.</Trans>}
				actions={
					all.length > 0 ? (
						<Button
							variant="ghost"
							size="sm"
							disabled={busy || !rowsCurrent}
							className="text-danger-text hover:bg-danger-soft"
							onClick={() => void emptyTrash()}
						>
							<Icon name="trash" />
							<Trans>Empty Trash…</Trans>
						</Button>
					) : undefined
				}
			/>
			<nav aria-label={t`Trash`} className="flex gap-2">
				<Button aria-current="page">
					<Trans>Documents</Trans>
				</Button>
				<Link to="/dashboard/applications/trash" className={buttonVariants({ variant: "ghost" })}>
					<Trans>Applications</Trans>
				</Link>
			</nav>

			{isError && <LibraryError retrying={isFetching} onRetry={() => void refetch()} />}

			{isError && !documents ? null : isPending ? (
				// Rows to come, not a bare table header, while Trash loads.
				<LibrarySkeleton view="list" />
			) : documents?.length === 0 ? (
				<div className="grid justify-items-center gap-2 py-16 text-center">
					<Icon name="trash" size={28} className="text-ink-3" />
					<p className="font-display text-[22px] leading-7">
						<Trans>Trash is empty</Trans>
					</p>
					<p className="text-sm text-ink-2">
						<Trans>Documents you move here stay for 30 days.</Trans>
					</p>
				</div>
			) : (
				<table ref={resultsRef} className="w-full table-fixed border-collapse">
					<caption className="sr-only">
						<Trans>Trash</Trans>
					</caption>
					<thead>
						<tr className="border-b border-line text-xs font-medium text-ink-3">
							<th className="w-10 ps-3">
								<Checkbox
									disabled={!rowsCurrent}
									checked={allSelected}
									indeterminate={selecting && !allSelected}
									onCheckedChange={(on) => selectAll(on)}
									aria-label={t`Select this page in Trash`}
								/>
							</th>
							<th className="h-10 ps-3 text-start font-medium">
								<Trans>Name</Trans>
							</th>
							<th className="w-24 px-2 text-start font-medium max-sm:hidden">
								<Trans>Type</Trans>
							</th>
							<th className="w-1/4 px-2 text-start font-medium max-sm:hidden">
								<Trans>Application</Trans>
							</th>
							<th className="w-28 px-2 text-start font-medium max-sm:w-24">
								<Trans>Deleted in</Trans>
							</th>
							<th className="w-12">
								<span className="sr-only">
									<Trans>Options</Trans>
								</span>
							</th>
						</tr>
					</thead>
					<tbody>
						{documents?.map((document) => (
							<DocumentRow
								key={documentKey(document)}
								document={document}
								onTags={noop}
								onLink={noop}
								selected={rowsCurrent && selected.has(documentKey(document))}
								disabled={!rowsCurrent}
								onSelectedChange={(on) => setSelectedFor(document, on)}
							/>
						))}
					</tbody>
				</table>
			)}

			<PageNavigation
				offset={displayedOffset}
				total={displayed?.total ?? 0}
				busy={isFetching || busy || !rowsCurrent}
				onChange={(next) => {
					clearSelection();
					setOffset(next);
				}}
			/>
			<TrashSelectionBar
				ref={selectionBarRef}
				selection={selection}
				allSelected={allSelected}
				busy={busy}
				onSelectAll={() => selectAll(true)}
				onClear={clearSelection}
				onRestore={() => void run(() => batch.restore(selection))}
				onPurge={() => void purgeSelected()}
			/>
		</div>
	);
}

type TrashSelectionBarProps = {
	ref: Ref<HTMLDivElement>;
	selection: { type: string; id: string; name: string }[];
	allSelected: boolean;
	busy: boolean;
	onSelectAll: () => void;
	onClear: () => void;
	onRestore: () => void;
	onPurge: () => void;
};

/** Floats over Trash while rows are selected: Restore or Delete permanently the selected loaded documents. */
function TrashSelectionBar({
	ref,
	selection,
	allSelected,
	busy,
	onSelectAll,
	onClear,
	onRestore,
	onPurge,
}: TrashSelectionBarProps) {
	const count = selection.length;

	return (
		<div
			ref={ref}
			role="toolbar"
			aria-label={t`Selected documents in Trash`}
			aria-hidden={count === 0}
			inert={count === 0}
			className={cn(
				"fixed inset-x-0 bottom-6 z-30 mx-auto flex w-fit max-w-[calc(100vw-32px)] items-center gap-1 rounded-xl border border-line bg-raised p-1.5 shadow-e3 transition-[opacity,translate] duration-standard ease-enter max-sm:bottom-[calc(69px+env(safe-area-inset-bottom))]",
				count === 0 && "pointer-events-none translate-y-2 opacity-0",
			)}
		>
			<span className="px-2 text-sm font-semibold whitespace-nowrap" aria-live="polite">
				<Trans>{ph({ count: i18n.number(count) })} selected</Trans>
			</span>
			{!allSelected && (
				<Button variant="ghost" size="sm" disabled={busy} onClick={onSelectAll} className="max-sm:hidden">
					<Trans>Select this page</Trans>
				</Button>
			)}
			<Button variant="ghost" size="sm" disabled={busy} onClick={onRestore}>
				<Icon name="arrow-counter-clockwise" />
				<span className="max-sm:sr-only">
					<Trans>Restore</Trans>
				</span>
			</Button>
			<Button
				variant="ghost"
				size="sm"
				disabled={busy}
				className="text-danger-text hover:bg-danger-soft"
				onClick={onPurge}
			>
				<Icon name="trash" />
				<span className="max-sm:sr-only">
					<Trans>Delete permanently…</Trans>
				</span>
			</Button>
			<IconButton icon="x" size="icon-sm" label={t`Clear selection`} className="text-ink-2" onClick={onClear} />
		</div>
	);
}