#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
/** The sweep, rendered in place of the workbench canvas with the rails either side.
* A takeover, not a registered view: unregistering an active view ejects mid-sweep. */
import { Icon } from "@editor/ui/Icon";
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { ActionIcon } from "@editor/ui/ActionIcon";
import { Button } from "@editor/ui/Button";
import { BrandMark } from "@editor/components/shared/BrandMark";
import {
claimRun,
sweptPaths,
} from "@editor/components/onboarding/classificationDemo/classificationDemoSession";
import { startBackgroundClassification } from "@editor/components/onboarding/classificationDemo/backgroundClassification";
import { useClassificationDemo } from "@editor/components/onboarding/classificationDemo/useClassificationDemo";
import type { ClassificationDemoViewData } from "@editor/components/onboarding/classificationDemo/classificationDemoShared";
import {
ClassificationDemoBreakdown,
ClassificationDemoChart,
useSliceColours,
} from "@editor/components/onboarding/classificationDemo/ClassificationDemoChart";
import {
CategoryTicker,
FollowUpPanel,
ProcessingCounts,
ProcessingHero,
useProcessingLabel,
} from "@editor/components/onboarding/classificationDemo/classificationDemoSlides";
import styles from "@editor/components/onboarding/classificationDemo/classificationDemo.module.css";
export function ClassificationDemoWorkbenchView({
data,
onDone,
}: {
data: ClassificationDemoViewData;
/** Hands the canvas back. The only way out — see the file header. */
onDone: () => void;
}) {
const { t } = useTranslation();
const trick = useClassificationDemo(true);
const [selectedId, setSelectedId] = useState<string | null>(null);
// Claimed from the session, not a ref: a remount would reset a ref and run the same
// sweep again from zero, re-reading and re-metering every document.
useEffect(() => {
if (!trick.directory) return;
if (!claimRun(data.runToken)) return;
setSelectedId(null);
trick.start(data.limit);
}, [data.runToken, data.limit, trick]);
const processingLabel = useProcessingLabel(
trick.progress.phase,
trick.progress.processed,
trick.progress.total,
);
const groups = trick.outcome?.groups ?? [];
const colours = useSliceColours(groups);
const selected = groups.find((group) => group.id === selectedId) ?? null;
const remaining = trick.outcome?.remaining ?? 0;
const canContinue = remaining > 0;
// The sweep opens nothing, so it has no workbench state to tear down — and clearing
// here would close whatever the user already had open.
const leave = onDone;
// Safe from any state: cancel is a no-op unless a sweep is actually running.
const dismiss = () => {
trick.cancel();
leave();
};
// The rest runs behind the rail's ring rather than on the canvas, so the user gets the
// app back at once. Swept paths are copied before the session store clears them.
const continueInBackground = () => {
if (!trick.directory || !trick.outcome) return;
startBackgroundClassification({
directory: trick.directory,
folderName: t("classificationDemo.folderName", "Downloads"),
limit: remaining,
exclude: sweptPaths(),
alreadyProcessed: trick.outcome.processed,
total: trick.outcome.pdfsInFolder,
});
leave();
};
// Every state wears the same close control, so leaving never depends on which one the
// view happens to be in.
const shell = (children: React.ReactNode) => (
<div className={styles.view}>
<ActionIcon
className={styles.viewClose}
variant="tertiary"
accent="neutral"
onClick={dismiss}
aria-label={t("common.close", "Close")}
>
<Icon name="x" size={20} />
</ActionIcon>
{children}
</div>
);
if (trick.status === "failed") {
return shell(
<div className={styles.viewColumn}>
<BrandMark height="2.25rem" />
<h2 className={styles.viewHeading}>
{t("classificationDemo.results.failedTitle", "That did not finish")}
</h2>
<p className={styles.viewLead}>
{t(
"classificationDemo.results.failedBody",
"Stirling could not read your Downloads folder. Nothing on your computer was changed.",
)}
</p>
<div className={styles.viewActions}>
<Button onClick={leave}>
{t("classificationDemo.buttons.done", "Done")}
</Button>
</div>
</div>,
);
}
if (trick.status === "done" && trick.outcome) {
return shell(
<div className={styles.results}>
<header className={styles.resultsHeader}>
<BrandMark height="2.25rem" />
<h2 className={styles.viewHeading}>
{t("classificationDemo.results.heading", "Your Downloads, sorted")}
</h2>
<p className={styles.viewLead}>
<Icon name="circle-check" className={styles.resultTick} />
{/* Two pluralised fragments: i18next pluralises on a single `count`, so one
string with two counts gets one wrong ("read 1 PDFs into 1 types"). */}
{t("classificationDemo.results.summary", {
documents: t("classificationDemo.results.documentCount", {
count: trick.outcome.processed,
}),
types: t("classificationDemo.results.typeCount", {
count: groups.length,
}),
defaultValue:
"Stirling read {{documents}} and sorted them into {{types}}.",
})}
</p>
</header>
<div className={styles.resultsBody}>
<ClassificationDemoChart
groups={groups}
total={trick.outcome.processed}
selectedId={selectedId}
onSelect={setSelectedId}
/>
<div className={styles.resultsDetail}>
<CategoryTicker
groups={groups}
selectedId={selectedId}
onSelect={setSelectedId}
/>
<ClassificationDemoBreakdown
group={selected}
colour={selected ? colours.get(selected.id) : undefined}
/>
</div>
</div>
<footer className={styles.resultsFooter}>
{canContinue && <FollowUpPanel remaining={remaining} />}
<div className={styles.viewActions}>
<Button variant="quiet" accent="neutral" onClick={leave}>
{canContinue
? t("classificationDemo.buttons.notNow", "Not now")
: t("classificationDemo.buttons.done", "Done")}
</Button>
{canContinue && (
<Button onClick={continueInBackground}>
{t("classificationDemo.followUp.restCta", "Process the rest")}
</Button>
)}
</div>
</footer>
</div>,
);
}
return shell(
<div className={styles.viewColumn}>
<ProcessingHero label={processingLabel} />
<ProcessingCounts progress={trick.progress} />
<CategoryTicker groups={trick.progress.groups} />
<div className={styles.viewActions}>
<Button variant="quiet" accent="neutral" onClick={dismiss}>
{t("classificationDemo.buttons.stop", "Stop")}
</Button>
</div>
</div>,
);
}