import { Alert, Checkbox, Collapse, DialogContent, FormGroup, Stack, Tooltip } from "@mui/material"; import dayjs from "dayjs"; import duration from "dayjs/plugin/duration"; import { useCallback, useMemo, useState } from "react"; import { Trans, useTranslation } from "react-i18next"; import { Metadata } from "../../../api/explorer.ts"; import { GroupPermission } from "../../../api/user.ts"; import { setFileDeleteModal } from "../../../redux/fileManagerSlice.ts"; import { useAppDispatch, useAppSelector } from "../../../redux/hooks.ts"; import { deleteDialogPromisePool } from "../../../redux/thunks/dialog.ts"; import SessionManager from "../../../session"; import { formatDuration } from "../../../util/datetime.ts"; import { SmallFormControlLabel } from "../../Common/StyledComponents.tsx"; import DialogAccordion from "../../Dialogs/DialogAccordion.tsx"; import DraggableDialog, { StyledDialogContentText } from "../../Dialogs/DraggableDialog.tsx"; dayjs.extend(duration); export interface DeleteOption { unlink?: boolean; skip_soft_delete?: boolean; } const DeleteConfirmation = () => { const { t } = useTranslation(); const dispatch = useAppDispatch(); const [unlink, setUnlink] = useState(false); const [skipSoftDelete, setSkipSoftDelete] = useState(false); const open = useAppSelector((state) => state.fileManager[0].deleteFileModalOpen); const targets = useAppSelector((state) => state.fileManager[0].deleteFileModalSelected); const promiseId = useAppSelector((state) => state.fileManager[0].deleteFileModalPromiseId); const loading = useAppSelector((state) => state.fileManager[0].deleteFileModalLoading); const hasTrashFiles = useMemo(() => { if (targets) { return targets.some((target) => target.metadata && target.metadata[Metadata.restore_uri]); } return false; }, [targets]); const onClose = useCallback(() => { dispatch( setFileDeleteModal({ index: 0, value: [false, targets, undefined, false], }), ); if (promiseId) { deleteDialogPromisePool[promiseId]?.reject("cancel"); } }, [dispatch, targets, promiseId]); const singleFileToTrash = targets && targets.length == 1 && !hasTrashFiles && !skipSoftDelete; const multipleFilesToTrash = targets && targets.length > 1 && !hasTrashFiles && !skipSoftDelete; const singleFilePermanently = targets && targets.length == 1 && (hasTrashFiles || skipSoftDelete); const multipleFilesPermanently = targets && targets.length > 1 && (hasTrashFiles || skipSoftDelete); const onAccept = useCallback(() => { if (promiseId) { deleteDialogPromisePool[promiseId]?.resolve({ unlink, skip_soft_delete: singleFilePermanently || multipleFilesPermanently ? true : skipSoftDelete, }); } }, [promiseId, unlink, skipSoftDelete, singleFilePermanently, multipleFilesPermanently]); const permission = SessionManager.currentUserGroupPermission(); const showSkipSoftDeleteOption = !hasTrashFiles; const showUnlinkOption = (skipSoftDelete || hasTrashFiles) && permission.enabled(GroupPermission.advance_delete); const showAdvanceOptions = showUnlinkOption || showSkipSoftDeleteOption; const group = useMemo(() => SessionManager.currentUserGroup(), [open]); return ( {(singleFileToTrash || singleFilePermanently) && ( ]} /> )} {(multipleFilesToTrash || multipleFilesPermanently) && t( multipleFilesToTrash ? "application:modals.deleteMultipleDescription" : "application:modals.deleteMultipleDescriptionHard", { num: targets.length, }, )} ]} /> {showAdvanceOptions && ( setSkipSoftDelete(e.target.checked)} checked={skipSoftDelete} /> } label={t("application:modals.skipSoftDelete")} /> setUnlink(e.target.checked)} checked={unlink} />} label={t("application:modals.unlinkOnly")} /> )} ); }; export default DeleteConfirmation;