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.2 KB
Raw Blame
import type { DocumentSummary } from "./filter";
import { i18n } from "@lingui/core";
import { ph, plural, t } from "@lingui/core/macro";
import { ORPCError } from "@orpc/client";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { getNetworkStateAsync } from "expo-network";
import { useRef, useState } from "react";
import { SUBMITTED_DOCUMENT_LOCKED } from "@reactive-resume/schema/applications/errors";
import { documentRef } from "./filter";
import { playDocumentEffect } from "./library-session";
import { client, orpc } from "@/api/client";
import { getAccountScope, getServer, useSession } from "@/auth/session";
import { confirmUndone } from "@/lib/undone";
import { showToast } from "@/ui";

type BatchOptions = {
	applies?: (document: DocumentSummary) => boolean;
	alreadyDone?: (document: DocumentSummary) => boolean;
	notify?: boolean;
};

// One request body stays bounded even when an older client has loaded the whole library.
const BATCH_SIZE = 500;
const chunks = <T>(items: readonly T[]) =>
	Array.from({ length: Math.ceil(items.length / BATCH_SIZE) }, (_, index) =>
		items.slice(index * BATCH_SIZE, (index + 1) * BATCH_SIZE),
	);

export function documentError(error: unknown) {
	if (error instanceof ORPCError) {
		if (error.data?.reason === SUBMITTED_DOCUMENT_LOCKED)
			return t`This application keeps the documents you submitted. Choose an application without a submitted letter, or duplicate the letter for another job.`;
		if (error.code === "UNAUTHORIZED") return t`Your session expired. Sign in again.`;
		if (error.code === "FORBIDDEN") return t`Unlock it first, then try again.`;
		if (error.code === "NOT_FOUND") return t`This document is no longer available. Refresh and try again.`;
		if (error.code === "TOO_MANY_REQUESTS" || error.code === "RATE_LIMIT_EXCEEDED")
			return t`Too many requests. Try again later.`;
		if (error.code === "BAD_REQUEST") return t`Check the file or fields and try again.`;
		if (error.code === "RESUME_SLUG_ALREADY_EXISTS")
			return t`Another resume already uses this address. Choose a different one.`;
		if (error.code === "RESUME_LOCKED") return t`This resume is locked. Unlock it to make changes.`;
		if (error.code === "INVALID_SLUG")
			return `${t`Use at most 64 characters.`} ${t`The address must contain only Latin letters a–z and numbers, separated by single dashes.`}`;
	}
	return t`Couldn't complete this action. Try again.`;
}

export function useDocuments(trashed = false, enabled = true) {
	const session = useSession();
	const options = orpc.documents.list.queryOptions({ input: { trashed } });
	return useQuery({
		...options,
		queryKey: [...options.queryKey, session.server],
		enabled: enabled && session.status === "signed-in",
	});
}

export function useDocumentRefresh() {
	const queryClient = useQueryClient();
	return () =>
		Promise.all([
			queryClient.invalidateQueries({ queryKey: orpc.documents.key() }),
			queryClient.invalidateQueries({ queryKey: orpc.resume.key() }),
			queryClient.invalidateQueries({ queryKey: orpc.coverLetters.key() }),
			queryClient.invalidateQueries({ queryKey: orpc.applications.key() }),
		]);
}

/** Serialize user actions; report partial batches and undo only successful Trash moves. */
export function useDocumentOperations(enabled = true) {
	const refresh = useDocumentRefresh();
	const accountScope = getAccountScope();
	const session = useSession();
	const active = useRef(false);
	const idle = useRef(Promise.resolve());
	const partial = useRef(false);
	const [busy, setBusy] = useState(false);
	const [error, setError] = useState<string | null>(null);
	const current = () => getServer() === session.server && getAccountScope() === accountScope;
	const notifyCurrent = (options: Parameters<typeof showToast>[0]) => {
		if (current()) showToast(options);
	};
	const fail = (message: string) => {
		if (!current()) return false;
		setError(message);
		notifyCurrent({ message });
		return false;
	};
	const run = async (task: () => Promise<unknown>) => {
		if (!enabled || active.current) return false;
		active.current = true;
		let finish = () => {};
		idle.current = new Promise<void>((resolve) => {
			finish = resolve;
		});
		partial.current = false;
		setBusy(true);
		setError(null);
		try {
			if (session.status !== "signed-in" || !current()) {
				return fail(t`Sign in to continue.`);
			}
			const network = await getNetworkStateAsync();
			if (network.isConnected === false || network.isInternetReachable === false) {
				return fail(t`You're offline. Try again when you reconnect.`);
			}
			if (!current()) return false;
			await task();
			return current() && !partial.current;
		} catch (cause) {
			return fail(documentError(cause));
		} finally {
			try {
				if (current()) await refresh();
			} finally {
				active.current = false;
				setBusy(false);
				finish();
			}
		}
	};
	const batch = async (
		documents: readonly DocumentSummary[],
		task: (document: DocumentSummary) => Promise<unknown>,
		{ applies = () => true, alreadyDone = () => false, notify = true }: BatchOptions = {},
	) => {
		const done: DocumentSummary[] = [];
		const unchanged = documents.filter(alreadyDone).length;
		// Sequential calls avoid overwhelming per-account mutation limits.
		for (const document of documents) {
			if (alreadyDone(document) || !applies(document)) continue;
			if (!current()) break;
			try {
				await task(document);
				done.push(document);
			} catch {
				/* Remaining documents can still succeed. */
			}
		}
		if (!current()) return done;
		const completed = done.length + unchanged;
		const message =
			completed === documents.length
				? plural(completed, { one: "# document updated", other: "# documents updated" })
				: t`Updated ${ph({ completed: i18n.number(completed) })} of ${i18n.number(documents.length)}. Locked documents, and letters for public links, were skipped. Refresh and retry any failures.`;
		if (completed !== documents.length) {
			partial.current = true;
			setError(message);
		}
		if (notify) notifyCurrent({ message });
		return done;
	};
	const trash = async (documents: readonly DocumentSummary[]) => {
		let done: DocumentSummary[] = [];
		let attempted = false;
		const success = await run(async () => {
			attempted = true;
			done = await batch(documents, (document) => client.documents.trash(documentRef(document)), {
				applies: (document) => !document.isLocked,
				notify: false,
			});
			if (done.length) await playDocumentEffect(accountScope, "trash", done);
		});
		if (attempted)
			notifyCurrent({
				message:
					done.length === documents.length
						? plural(done.length, { one: "# document moved to Trash", other: "# documents moved to Trash" })
						: t`Moved ${i18n.number(done.length)} of ${i18n.number(documents.length)} to Trash. Locked documents were skipped. Refresh and retry any failures.`,
				action: done.length
					? {
							label: t`Undo`,
							onPress: () => {
								// The server counts only documents it restored: one already back (another device) or deleted
								// since isn't counted (web document-actions).
								let restored = 0;
								let started = false;
								void idle.current
									.then(() =>
										run(async () => {
											started = true;
											for (const group of chunks(done)) {
												if (!current()) break;
												try {
													const result = await client.documents.restoreMany({ documents: group.map(documentRef) });
													restored += result.restored;
													if (result.restored === group.length)
														await playDocumentEffect(accountScope, "restore", group);
												} catch {
													/* The remaining groups can still succeed. */
												}
											}
										}),
									)
									.then(() => {
										// A run that couldn't start (offline, signed out) already said why.
										if (!started || !current()) return;
										if (restored === done.length) return confirmUndone();
										notifyCurrent({
											message: t`Restored ${ph({ restored: i18n.number(restored) })} of ${i18n.number(done.length)}.`,
										});
									});
							},
						}
					: undefined,
			});
		return success;
	};
	/** Restores the selected loaded documents; the server skips any that changed meanwhile. */
	const restoreSelected = async (documents: readonly DocumentSummary[]) => {
		let restored = 0;
		let attempted = false;
		const success = await run(async () => {
			attempted = true;
			for (const group of chunks(documents)) {
				if (!current()) break;
				try {
					const result = await client.documents.restoreMany({ documents: group.map(documentRef) });
					restored += result.restored;
					// Partial receipts give no item IDs; refresh them without guessing which rows changed.
					if (result.restored === group.length) await playDocumentEffect(accountScope, "restore", group);
				} catch {
					/* The remaining groups can still succeed. */
				}
			}
			if (current() && restored !== documents.length) {
				partial.current = true;
				setError(
					t`Restored ${ph({ restored: i18n.number(restored) })} of ${i18n.number(documents.length)}. Refresh and retry any that remain in Trash.`,
				);
			}
		});
		if (attempted && restored > 0)
			notifyCurrent({
				message:
					restored === documents.length
						? plural(restored, { one: "# document restored", other: "# documents restored" })
						: t`Restored ${ph({ restored: i18n.number(restored) })} of ${i18n.number(documents.length)}. Refresh and retry any that remain in Trash.`,
			});
		return success;
	};
	/** Permanently deletes the selected loaded documents; Empty Trash is a separate server-side operation. */
	const purgeSelected = async (documents: readonly DocumentSummary[]) => {
		let deleted = 0;
		let attempted = false;
		const success = await run(async () => {
			attempted = true;
			for (const group of chunks(documents)) {
				if (!current()) break;
				try {
					const result = await client.documents.purgeMany({ documents: group.map(documentRef) });
					deleted += result.deleted;
					if (result.deleted === group.length) await playDocumentEffect(accountScope, "purge", group);
				} catch {
					/* The remaining groups can still succeed. */
				}
			}
			if (current() && deleted !== documents.length) {
				partial.current = true;
				setError(
					t`Deleted ${ph({ deleted: i18n.number(deleted) })} of ${i18n.number(documents.length)}. Refresh to see what remains.`,
				);
			}
		});
		if (attempted && deleted > 0)
			notifyCurrent({
				message:
					deleted === documents.length
						? plural(deleted, { one: "# document deleted forever", other: "# documents deleted forever" })
						: t`Deleted ${ph({ deleted: i18n.number(deleted) })} of ${i18n.number(documents.length)} forever. Refresh to see what remains.`,
			});
		return success;
	};
	/** Deletes every document in this account's Trash, not only the IDs this device has loaded. */
	const emptyTrash = async () => {
		let deleted = 0;
		const success = await run(async () => {
			deleted = (await client.documents.emptyTrash({})).deleted;
			if (deleted > 0) await playDocumentEffect(accountScope, "purge");
		});
		if (success)
			notifyCurrent({
				message:
					deleted === 0
						? t`Trash was already empty`
						: plural(deleted, { one: "# document deleted forever", other: "# documents deleted forever" }),
			});
		return success;
	};
	return {
		run,
		batch,
		trash,
		restoreSelected,
		purgeSelected,
		emptyTrash,
		busy,
		error,
	};
}