Files
outline/app/scenes/DocumentDelete.tsx
Tom Moor f8e70c2c39 chore: resolve mechanical react-hooks/exhaustive-deps warnings (#12207)
Adds missing stable dependencies (e.g. `t`, prop callbacks, store refs,
`setFocusedCommentId`) and removes unnecessary ones across hooks where the
fix is straightforward. For the two MobX-observed `.orderedData` deps in
`History.tsx`, keeps the original deps and silences the false positive
with `eslint-disable-next-line` so the memos still recompute when the
underlying observable arrays change.

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-28 22:06:09 -04:00

155 lines
4.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { observer } from "mobx-react";
import * as React from "react";
import { useTranslation, Trans } from "react-i18next";
import { useHistory } from "react-router-dom";
import { toast } from "sonner";
import type Document from "~/models/Document";
import Button from "~/components/Button";
import Flex from "~/components/Flex";
import Text from "~/components/Text";
import useStores from "~/hooks/useStores";
import { collectionPath, documentPath, homePath } from "~/utils/routeHelpers";
type Props = {
document: Document;
onSubmit: () => void;
};
function DocumentDelete({ document, onSubmit }: Props) {
const { t } = useTranslation();
const { ui, documents, collections, userMemberships, groupMemberships } =
useStores();
const history = useHistory();
const [isDeleting, setDeleting] = React.useState(false);
const [isArchiving, setArchiving] = React.useState(false);
const canArchive = !document.isDraft && !document.isArchived;
const collection = document.collectionId
? collections.get(document.collectionId)
: undefined;
const nestedDocumentsCount = collection
? collection.getChildrenForDocument(document.id).length
: 0;
const handleSubmit = React.useCallback(
async (ev: React.SyntheticEvent) => {
ev.preventDefault();
setDeleting(true);
try {
await document.delete();
userMemberships
.getByDocumentId(document.id)
?.removeDocument(document.id);
groupMemberships
.getByDocumentId(document.id)
?.removeDocument(document.id);
// only redirect if we're currently viewing the document that's deleted
if (ui.activeDocumentId === document.id) {
// If the document has a parent and it's available in the store then
// redirect to it
if (document.parentDocumentId) {
const parent = documents.get(document.parentDocumentId);
if (parent) {
history.push(documentPath(parent));
onSubmit();
return;
}
}
const path = collection ? collectionPath(collection) : homePath();
history.push(path);
}
onSubmit();
} catch (err) {
toast.error(err.message);
} finally {
setDeleting(false);
}
},
[
onSubmit,
ui,
document,
documents,
history,
collection,
userMemberships,
groupMemberships,
]
);
const handleArchive = React.useCallback(
async (ev: React.SyntheticEvent) => {
ev.preventDefault();
setArchiving(true);
try {
await document.archive();
onSubmit();
} catch (err) {
toast.error(err.message);
} finally {
setArchiving(false);
}
},
[onSubmit, document]
);
return (
<form onSubmit={handleSubmit}>
<Text as="p" type="secondary">
{nestedDocumentsCount < 1 ? (
<Trans
defaults="Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history</em>."
values={{
documentTitle: document.titleWithDefault,
}}
components={{
em: <strong />,
}}
/>
) : (
<Trans
count={nestedDocumentsCount}
defaults="Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history and <em>{{ any }} nested document</em>."
values={{
documentTitle: document.titleWithDefault,
any: nestedDocumentsCount,
}}
components={{
em: <strong />,
}}
/>
)}
</Text>
{canArchive && (
<Text as="p" type="secondary">
<Trans>
If youd like the option of referencing or restoring the{" "}
{{
noun: document.noun,
}}{" "}
in the future, consider archiving it instead.
</Trans>
</Text>
)}
<Flex justify="flex-end" gap={8}>
{canArchive && (
<Button type="button" onClick={handleArchive} neutral>
{isArchiving ? `${t("Archiving")}` : t("Archive")}
</Button>
)}
<Button type="submit" danger>
{isDeleting ? `${t("Deleting")}` : t("Im sure Delete")}
</Button>
</Flex>
</form>
);
}
export default observer(DocumentDelete);