Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

10.9 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import {
  Button,
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
  HStack,
  VStack
} from "@carbon/react";
import { Plural, Trans, useLingui } from "@lingui/react/macro";
import { useEffect, useState } from "react";
import { useFetcher } from "react-router";
import { DateTime } from "~/components";
import type { CompanyTemplateRun } from "~/modules/settings/backups.service";
import {
  ExcludedRowsInfo,
  PurgeCorruptedRowsModal
} from "~/modules/settings/ui/Backups";
import { formatElapsed } from "~/modules/settings/ui/Backups/format";
import { path } from "~/utils/path";
import { totalScopeRows } from "../../settings.utils";
import { KeepDemoDataModal } from "./KeepDemoDataModal";

/** A run with no completion after this long is treated as stalled, and offered a
 *  revert retry — the job's own crash handler can't fire if the process died. */
const STALLED_AFTER_MS = 5 * 60 * 1000;

export function TemplateReviewRow({
  run,
  datasetLabel,
  onResolve,
  onPurgeAndApply
}: {
  run: CompanyTemplateRun;
  datasetLabel: string | null;
  /** Hides the row for a keep/dismiss, which clear the marker asynchronously —
   *  a revert must NOT use this: it keeps running, and the row is what reports it. */
  onResolve: (templateRunId: string) => void;
  /** The route submits, so it can put the run back if the purge is refused. */
  onPurgeAndApply: () => void;
}) {
  const { t } = useLingui();
  const fetcher = useFetcher();
  const submitting = fetcher.state !== "idle";

  // Clicking Revert only ENQUEUES the job — the marker still says `ready` until
  // the job starts and flips it a poll or two later, so without this the row
  // snaps back to Keep/Revert and reads as "the click did nothing". Held until
  // the server reports a status of its own, so a job that never runs still
  // reaches the stalled state below.
  const [revertRequestedAt, setRevertRequestedAt] = useState<number | null>(
    null
  );
  const isReverting = run.status === "reverting" || revertRequestedAt !== null;
  const busy = run.status === "running" || isReverting;
  const scopeViolations =
    run.status === "failed" && run.reason === "scope-violations";
  const [confirmPurge, setConfirmPurge] = useState(false);
  const [confirmKeep, setConfirmKeep] = useState(false);
  useEffect(() => {
    // Any status but `ready` means the server has taken over the story —
    // including `failed`, where the optimistic spinner must not hide the error.
    if (run.status !== "ready") setRevertRequestedAt(null);
  }, [run.status]);

  // Phase keys are stable; the copy for them lives here, not in the job.
  const phaseLabel: Record<string, string> = {
    snapshot: t`Saving a copy of your current data`,
    seed: t`Adding demo records`,
    wipe: t`Clearing demo data`,
    load: t`Putting your data back`,
    files: t`Restoring files`
  };

  const [mountedAt] = useState(() => Date.now());
  const [now, setNow] = useState(() => Date.now());
  useEffect(() => {
    if (!busy) return;
    const id = setInterval(() => setNow(Date.now()), 1000);
    return () => clearInterval(id);
  }, [busy]);

  // Time a revert from the click while it is still only optimistic — the
  // marker's `startedAt` still belongs to the apply, and would read as minutes.
  // A row with no `startedAt` at all is an optimistic apply the job hasn't
  // acknowledged yet, so it counts from when the row appeared.
  const startedMs =
    revertRequestedAt ??
    (run.startedAt ? Date.parse(run.startedAt) : mountedAt);
  const elapsedMs = now - startedMs;
  const stalled = busy && elapsedMs > STALLED_AFTER_MS;
  const elapsed = formatElapsed(elapsedMs);

  const submit = (intent: "keep" | "revert" | "dismiss") => {
    if (intent === "revert") setRevertRequestedAt(Date.now());
    else onResolve(run.templateRunId);
    fetcher.submit(
      { intent, templateRunId: run.templateRunId },
      { method: "post", action: path.to.demoData }
    );
  };

  // Shown as a phase label, not a bar — a demo-sized run finishes in seconds, so
  // a filling bar would only ever be a flash.
  const progress = run.progress;

  return (
    <Card className="w-full">
      <CardHeader>
        <CardTitle>
          {isReverting ? (
            <Trans>Putting your data back</Trans>
          ) : run.status === "running" ? (
            <Trans>Applying demo data</Trans>
          ) : (
            <Trans>Demo data applied</Trans>
          )}
        </CardTitle>
        <CardDescription>
          {busy ? (
            <Trans>
              This usually takes a few seconds. You can leave the page — it
              keeps running.
            </Trans>
          ) : (
            <Trans>
              Demo data was applied to this company. Keep it, or revert to put
              back exactly what was here before.
            </Trans>
          )}
        </CardDescription>
      </CardHeader>
      <CardContent>
        <VStack spacing={2} className="w-full border rounded-lg p-3">
          <HStack className="w-full justify-between">
            <VStack spacing={0} className="min-w-0">
              <span className="text-sm font-medium truncate">
                {datasetLabel ?? run.datasetKey ?? t`Demo data`}
              </span>
              {scopeViolations ? (
                // Exact tables and the raw error live in the popover, as in RestoreReviewRow.
                <span className="break-words text-xs text-destructive">
                  <Plural
                    value={totalScopeRows(run.violationRowsByTable)}
                    one="# row links to data outside this company, so a safety copy of your current data can't be made."
                    other="# rows link to data outside this company, so a safety copy of your current data can't be made."
                  />{" "}
                  <ExcludedRowsInfo
                    excludedRows={run.violations}
                    title={<Trans>Why this failed</Trans>}
                    description={
                      <Trans>
                        Each line is a link from this company's data to a row it
                        doesn't own. Removing those rows lets the demo data
                        apply.
                      </Trans>
                    }
                    technical={run.error}
                  />
                </span>
              ) : run.status === "failed" ? (
                <span className="text-xs text-destructive">
                  {t`Failed`} — {run.error ?? t`unknown error`}
                </span>
              ) : (
                <span className="text-xs text-muted-foreground">
                  {run.startedAt && (
                    <DateTime value={run.startedAt} variant="absolute" />
                  )}
                  {" · "}
                  {busy
                    ? `${progress ? (phaseLabel[progress.phase] ?? progress.phase) : t`Starting…`} · ${elapsed}`
                    : t`ready to keep or revert`}
                </span>
              )}
            </VStack>
            <HStack spacing={2} className="shrink-0">
              {run.status === "ready" && !busy && (
                <>
                  <Button
                    isLoading={submitting}
                    isDisabled={submitting}
                    onClick={() => setConfirmKeep(true)}
                  >
                    <Trans>Keep</Trans>
                  </Button>
                  <Button
                    variant="destructive"
                    isLoading={submitting}
                    isDisabled={submitting}
                    onClick={() => submit("revert")}
                  >
                    <Trans>Revert</Trans>
                  </Button>
                </>
              )}
              {scopeViolations && (
                <Button
                  variant="destructive"
                  isDisabled={submitting}
                  onClick={() => setConfirmPurge(true)}
                >
                  <Trans>Remove corrupted data and apply</Trans>
                </Button>
              )}
              {run.status === "failed" && (
                <Button
                  variant="secondary"
                  isLoading={submitting}
                  isDisabled={submitting}
                  onClick={() => submit("dismiss")}
                >
                  <Trans>Dismiss</Trans>
                </Button>
              )}
              {/* The button the user pressed stays put while the job runs —
                  spinning, not gone — so an apply spins on Apply and a revert on
                  Revert. Once a run looks stalled it becomes the revert escape
                  either way, since that is the only way out. */}
              {busy &&
                (isReverting || stalled ? (
                  <Button
                    variant="destructive"
                    isLoading={!stalled || submitting}
                    isDisabled={!stalled || submitting || !run.hasSnapshot}
                    onClick={() => submit("revert")}
                  >
                    {stalled ? (
                      <Trans>Retry revert</Trans>
                    ) : (
                      <Trans>Reverting</Trans>
                    )}
                  </Button>
                ) : (
                  <Button isLoading isDisabled>
                    <Trans>Applying</Trans>
                  </Button>
                ))}
            </HStack>
          </HStack>

          {confirmKeep && (
            <KeepDemoDataModal
              onCancel={() => setConfirmKeep(false)}
              onConfirm={() => {
                setConfirmKeep(false);
                submit("keep");
              }}
            />
          )}

          {confirmPurge && (
            <PurgeCorruptedRowsModal
              rowsByTable={run.violationRowsByTable}
              description={
                <Trans>
                  These rows link to data outside this company, so a safety copy
                  of your current data can't be made. Deleting them cannot be
                  undone. If they turn out to be shared with other companies in
                  this group, nothing is deleted and the demo data isn't
                  applied.
                </Trans>
              }
              confirmLabel={<Trans>Delete and apply</Trans>}
              onCancel={() => setConfirmPurge(false)}
              onConfirm={() => {
                setConfirmPurge(false);
                onPurgeAndApply();
              }}
            />
          )}

          {stalled && (
            <span className="text-xs text-muted-foreground">
              <Trans>
                This is taking longer than expected. It may still finish — if it
                doesn't, retry the revert to put your data back.
              </Trans>
            </span>
          )}
        </VStack>
      </CardContent>
    </Card>
  );
}