Files
Stirling-PDF/frontend/src/core/components/shared/NavigationWarningModal.tsx
T
James BruntonandGitHub a3e45bc182 Add frontend autoformatting and set CI to require formatted code for all languages (#6052)
# Description of Changes
Changes the strategy for autoformatting to reject PRs if they are not
formatted correctly instead of allowing them to merge and then spawning
a new PR to fix the formatting. The old strategy just caused more work
for us because we'd have to manually approve the followup PR and get it
merged, which required 2 reviewers so in practice it rarely got done and
just meant everyone's PRs ended up containing reformatting for unrelated
files, which makes code review unnecessarily difficult. If the PR's code
is not formatted correctly after this PR, a comment will be added
automatically to tell the author how to run the formatter script to fix
their code so it can go in.

This also enables autoformatting for the frontend code, using Prettier.
I've enabled it for pretty much everything in the frontend folder, other
than 3rd party files and files it doesn't make sense for. I also
excluded Markdown because it sounds likely to be more annoying to have
to autoformat the Markdown in the frontend folder but nowhere else. Open
to changing this though if people disagree.

> [!note]
> 
> Advice to reviewers: The first commit contains all of the actual logic
I've introduced (CI changes, Prettier config, etc.)
> The second commit is just the reformatting of the entire frontend
folder.
> The first commit needs proper review, the second one just give it a
spot-check that it's doing what you'd expect.
2026-04-10 17:41:19 +01:00

191 lines
5.9 KiB
TypeScript

import { useRef, useEffect } from "react";
import { Modal, Text, Button, Group, Stack } from "@mantine/core";
import { useNavigationGuard } from "@app/contexts/NavigationContext";
import { useTranslation } from "react-i18next";
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
import DeleteOutlineIcon from "@mui/icons-material/DeleteOutline";
import CheckCircleOutlineIcon from "@mui/icons-material/CheckCircleOutline";
import { Z_INDEX_TOAST } from "@app/styles/zIndex";
const NavigationWarningModal = () => {
const { t } = useTranslation();
const {
showNavigationWarning,
hasUnsavedChanges,
pendingNavigation,
cancelNavigation,
setHasUnsavedChanges,
navigationWarningHandlersRef,
} = useNavigationGuard();
// Store pendingNavigation in a ref so async handlers always have the latest,
// not a stale closure captured before an await.
const pendingNavigationRef = useRef(pendingNavigation);
useEffect(() => {
pendingNavigationRef.current = pendingNavigation;
}, [pendingNavigation]);
const handleKeepWorking = () => {
cancelNavigation();
};
const finishAndNavigate = () => {
const nav = pendingNavigationRef.current;
setHasUnsavedChanges(false);
cancelNavigation();
if (nav) {
nav();
}
};
const handleDiscardChanges = async () => {
const handlers = navigationWarningHandlersRef.current;
if (handlers?.onDiscardAndContinue) {
await handlers.onDiscardAndContinue();
}
finishAndNavigate();
};
const handleApplyAndContinue = async () => {
const handlers = navigationWarningHandlersRef.current;
if (handlers?.onApplyAndContinue) {
await handlers.onApplyAndContinue();
}
finishAndNavigate();
};
const handleExportAndContinue = async () => {
const handlers = navigationWarningHandlersRef.current;
if (handlers?.onExportAndContinue) {
await handlers.onExportAndContinue();
}
finishAndNavigate();
};
// Read handler availability at render time for button visibility
const handlers = navigationWarningHandlersRef.current;
const hasApply = !!handlers?.onApplyAndContinue;
const hasExport = !!handlers?.onExportAndContinue;
const BUTTON_WIDTH = "12rem";
// Only show modal if there are unsaved changes AND there's an actual pending navigation
if (!hasUnsavedChanges || !pendingNavigation) {
return null;
}
return (
<Modal
opened={showNavigationWarning}
onClose={handleKeepWorking}
title={t("unsavedChangesTitle", "Unsaved Changes")}
centered
size="auto"
closeOnClickOutside={true}
closeOnEscape={true}
zIndex={Z_INDEX_TOAST}
>
<Stack>
<Stack ta="center" p="md">
<Text size="md" fw="300">
{t("unsavedChanges", "You have unsaved changes to your PDF.")}
</Text>
<Text size="lg" fw="500">
{t("areYouSure", "Are you sure you want to leave?")}
</Text>
</Stack>
{/* Desktop layout: 2 groups side by side */}
<Group justify="space-between" gap="xl" visibleFrom="md">
<Group gap="sm">
<Button
variant="light"
color="var(--mantine-color-gray-8)"
onClick={handleKeepWorking}
w={BUTTON_WIDTH}
leftSection={<ArrowBackIcon fontSize="small" />}
>
{t("keepWorking", "Keep Working")}
</Button>
</Group>
<Group gap="sm">
<Button
variant="filled"
color="var(--mantine-color-red-9)"
onClick={handleDiscardChanges}
w={BUTTON_WIDTH}
leftSection={<DeleteOutlineIcon fontSize="small" />}
>
{t("discardChanges", "Discard Changes")}
</Button>
{hasApply && (
<Button
variant="filled"
onClick={handleApplyAndContinue}
w={BUTTON_WIDTH}
leftSection={<CheckCircleOutlineIcon fontSize="small" />}
>
{t("applyAndContinue", "Apply & Leave")}
</Button>
)}
{hasExport && (
<Button
variant="filled"
onClick={handleExportAndContinue}
w={BUTTON_WIDTH}
leftSection={<CheckCircleOutlineIcon fontSize="small" />}
>
{t("exportAndContinue", "Export & Leave")}
</Button>
)}
</Group>
</Group>
{/* Mobile layout: centered stack of 4 buttons */}
<Stack align="center" gap="sm" hiddenFrom="md">
<Button
variant="light"
color="var(--mantine-color-gray-8)"
onClick={handleKeepWorking}
w={BUTTON_WIDTH}
leftSection={<ArrowBackIcon fontSize="small" />}
>
{t("keepWorking", "Keep Working")}
</Button>
<Button
variant="filled"
color="var(--mantine-color-red-9)"
onClick={handleDiscardChanges}
w={BUTTON_WIDTH}
leftSection={<DeleteOutlineIcon fontSize="small" />}
>
{t("discardChanges", "Discard Changes")}
</Button>
{hasApply && (
<Button
variant="filled"
onClick={handleApplyAndContinue}
w={BUTTON_WIDTH}
leftSection={<CheckCircleOutlineIcon fontSize="small" />}
>
{t("applyAndContinue", "Apply & Leave")}
</Button>
)}
{hasExport && (
<Button
variant="filled"
onClick={handleExportAndContinue}
w={BUTTON_WIDTH}
leftSection={<CheckCircleOutlineIcon fontSize="small" />}
>
{t("exportAndContinue", "Export & Leave")}
</Button>
)}
</Stack>
</Stack>
</Modal>
);
};
export default NavigationWarningModal;