Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#9250 Delete action for unsaved Mods #9255

Merged
merged 15 commits into from
Oct 9, 2024
Merged
12 changes: 10 additions & 2 deletions src/pageEditor/hooks/useDeactivateMod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useCallback } from "react";
import { type RegistryId } from "@/types/registryTypes";
import {
DEACTIVATE_MOD_MODAL_PROPS,
DELETE_UNSAVED_MOD_MODAL_PROPS,
useRemoveModComponentFromStorage,
} from "@/pageEditor/hooks/useRemoveModComponentFromStorage";
import { useDispatch, useSelector } from "react-redux";
Expand All @@ -29,14 +30,16 @@ import { useModals } from "@/components/ConfirmationModal";
import { actions } from "@/pageEditor/store/editor/editorSlice";
import { getModComponentId, getModId } from "@/pageEditor/utils";
import { clearLog } from "@/background/messenger/api";
import { isInnerDefinitionRegistryId } from "@/types/helpers";

type Config = {
modId: RegistryId;
shouldShowConfirmation?: boolean;
};

/**
* This hook provides a callback function to deactivate a mod and remove it from the Page Editor
* This hook provides a callback function to deactivate a mod and remove it from the Page Editor. Note that in the case
* of unsaved mods, the mod will be deleted instead of deactivated.
*/
function useDeactivateMod(): (useDeactivateConfig: Config) => Promise<void> {
const dispatch = useDispatch();
Expand All @@ -48,7 +51,12 @@ function useDeactivateMod(): (useDeactivateConfig: Config) => Promise<void> {
return useCallback(
async ({ modId, shouldShowConfirmation = true }) => {
if (shouldShowConfirmation) {
const confirmed = await showConfirmation(DEACTIVATE_MOD_MODAL_PROPS);
const isUnsavedMod = isInnerDefinitionRegistryId(modId);
const confirmed = await showConfirmation(
isUnsavedMod
? DELETE_UNSAVED_MOD_MODAL_PROPS
: DEACTIVATE_MOD_MODAL_PROPS,
);

if (!confirmed) {
return;
Expand Down
11 changes: 11 additions & 0 deletions src/pageEditor/hooks/useRemoveModComponentFromStorage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ export const DEACTIVATE_MOD_MODAL_PROPS: ConfirmationModalProps = {
submitCaption: "Deactivate",
};

export const DELETE_UNSAVED_MOD_MODAL_PROPS: ConfirmationModalProps = {
title: "Delete Mod?",
message: (
<>
This action cannot be undone. If you&apos;d like to deactivate this mod
instead, save the mod first.
</>
),
submitCaption: "Delete",
};

/**
* Returns a callback that removes a mod component from the Page Editor and Mod Component Storage.
*
Expand Down
7 changes: 5 additions & 2 deletions src/pageEditor/modListingPanel/ModActionMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
faHistory,
faPlus,
faTimes,
faTrash,
} from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import styles from "./ActionMenu.module.scss";
Expand Down Expand Up @@ -91,8 +92,10 @@ const ModActionMenu: React.FC<{
},
},
{
title: "Deactivate",
icon: <FontAwesomeIcon icon={faTimes} fixedWidth />,
title: isUnsavedMod ? "Delete" : "Deactivate",
icon: (
<FontAwesomeIcon icon={isUnsavedMod ? faTrash : faTimes} fixedWidth />
),
async action() {
await deactivateMod({ modId });
},
Expand Down
Loading