import { Box, Divider, ListItemIcon, ListItemText, Menu, MenuItem, styled, Typography, useTheme } from "@mui/material";
import { Fragment, useCallback, useEffect, useMemo } from "react";
import { useTranslation } from "react-i18next";
import { closeContextMenu } from "../../../redux/fileManagerSlice.ts";
import { CreateNewDialogType } from "../../../redux/globalStateSlice.ts";
import { useAppDispatch, useAppSelector } from "../../../redux/hooks.ts";
import { downloadFiles } from "../../../redux/thunks/download.ts";
import {
batchGetDirectLinks,
createNew,
deleteFile,
dialogBasedMoveCopy,
enterFolder,
extractArchive,
goToParent,
goToSharedLink,
newRemoteDownload,
openShareDialog,
openSidebar,
renameFile,
restoreFile,
} from "../../../redux/thunks/file.ts";
import { refreshFileList, uploadClicked, uploadFromClipboard } from "../../../redux/thunks/filemanager.ts";
import { openViewers } from "../../../redux/thunks/viewer.ts";
import AppFolder from "../../Icons/AppFolder.tsx";
import ArchiveArrow from "../../Icons/ArchiveArrow.tsx";
import ArrowSync from "../../Icons/ArrowSync.tsx";
import BinFullOutlined from "../../Icons/BinFullOutlined.tsx";
import Clipboard from "../../Icons/Clipboard.tsx";
import CloudDownloadOutlined from "../../Icons/CloudDownloadOutlined.tsx";
import CopyOutlined from "../../Icons/CopyOutlined.tsx";
import DeleteOutlined from "../../Icons/DeleteOutlined.tsx";
import Download from "../../Icons/Download.tsx";
import Enter from "../../Icons/Enter.tsx";
import FileAdd from "../../Icons/FileAdd.tsx";
import FolderAdd from "../../Icons/FolderAdd.tsx";
import FolderArrowUp from "../../Icons/FolderArrowUp.tsx";
import FolderLink from "../../Icons/FolderLink.tsx";
import FolderOutlined from "../../Icons/FolderOutlined.tsx";
import HistoryOutlined from "../../Icons/HistoryOutlined.tsx";
import Info from "../../Icons/Info.tsx";
import LinkOutlined from "../../Icons/LinkOutlined.tsx";
import Open from "../../Icons/Open.tsx";
import RenameOutlined from "../../Icons/RenameOutlined.tsx";
import ShareOutlined from "../../Icons/ShareOutlined.tsx";
import Tag from "../../Icons/Tag.tsx";
import Upload from "../../Icons/Upload.tsx";
import WrenchSettings from "../../Icons/WrenchSettings.tsx";
import { SelectType } from "../../Uploader/core";
import { CascadingSubmenu } from "./CascadingMenu.tsx";
import MoreMenuItems from "./MoreMenuItems.tsx";
import NewFileTemplateMenuItems from "./NewFileTemplateMenuItems.tsx";
import OpenWithMenuItems from "./OpenWithMenuItems.tsx";
import OrganizeMenuItems from "./OrganizeMenuItems.tsx";
import TagMenuItems from "./TagMenuItems.tsx";
import useActionDisplayOpt from "./useActionDisplayOpt.ts";
export const SquareMenu = styled(Menu)(() => ({
"& .MuiPaper-root": {
minWidth: "200px",
},
}));
export const SquareMenuItem = styled(MenuItem, {
shouldForwardProp: (prop) => prop !== "hoverColor",
})<{ hoverColor?: string }>(({ theme, hoverColor }) => ({
"&:hover .MuiListItemIcon-root": {
color: hoverColor ?? theme.palette.primary.main,
},
}));
export const DenseDivider = styled(Divider)(() => ({
margin: "4px 0 !important",
}));
export const EmptyMenu = () => {
const { t } = useTranslation();
return (
{t("fileManager.noActionsCanBeDone")}
);
};
export interface ContextMenuProps {
fmIndex: number;
}
const ContextMenu = ({ fmIndex = 0 }: ContextMenuProps) => {
const { t } = useTranslation();
const dispatch = useAppDispatch();
const theme = useTheme();
const contextMenuOpen = useAppSelector((state) => state.fileManager[fmIndex].contextMenuOpen);
const contextMenuType = useAppSelector((state) => state.fileManager[fmIndex].contextMenuType);
const contextMenuPos = useAppSelector((state) => state.fileManager[fmIndex].contextMenuPos);
const selected = useAppSelector((state) => state.fileManager[fmIndex].selected);
const targetOverwrite = useAppSelector((state) => state.fileManager[fmIndex].contextMenuTargets);
const targets = useMemo(() => {
const targetsMap = targetOverwrite ?? selected;
return Object.keys(targetsMap).map((key) => targetsMap[key]);
}, [targetOverwrite, selected]);
const parent = useAppSelector((state) => state.fileManager[fmIndex].list?.parent);
const displayOpt = useActionDisplayOpt(targets, contextMenuType, parent, fmIndex);
const onClose = useCallback(() => {
dispatch(closeContextMenu({ index: fmIndex, value: undefined }));
}, [dispatch]);
// The menu overlay lets pointer events fall through to the file list, so
// dismiss it manually on any click outside the menu paper — the same event
// still reaches the file underneath (upstream #3278).
useEffect(() => {
if (!contextMenuOpen) {
return;
}
const onPointerDown = (e: PointerEvent) => {
if ((e.target as HTMLElement | null)?.closest?.(".MuiMenu-paper")) {
return;
}
dispatch(closeContextMenu({ index: fmIndex, value: undefined }));
};
document.addEventListener("pointerdown", onPointerDown, true);
return () => document.removeEventListener("pointerdown", onPointerDown, true);
}, [contextMenuOpen, dispatch, fmIndex]);
const showOpenWithCascading = displayOpt.showOpenWithCascading && displayOpt.showOpenWithCascading();
const showOpenWith = displayOpt.showOpenWith && displayOpt.showOpenWith();
let part1 =
displayOpt.showOpen ||
showOpenWithCascading ||
showOpenWith ||
displayOpt.showEnter ||
displayOpt.showDownload ||
displayOpt.showRemoteDownload ||
displayOpt.showTorrentRemoteDownload ||
displayOpt.showExtractArchive ||
displayOpt.showUpload;
let part2 =
displayOpt.showCreateFolder ||
displayOpt.showCreateFile ||
displayOpt.showShare ||
displayOpt.showRename ||
displayOpt.showCopy ||
displayOpt.showDirectLink;
let part3 =
displayOpt.showTags || displayOpt.showOrganize || displayOpt.showMore || displayOpt.showNewFileFromTemplate;
let part4 = displayOpt.showInfo || displayOpt.showGoToParent || displayOpt.showGoToSharedLink;
let part5 = displayOpt.showRestore || displayOpt.showDelete || displayOpt.showRefresh;
const showDivider1 = part1 && part2;
const showDivider2 = part2 && part3;
const showDivider3 = part3 && part4;
const showDivider4 = part4 && part5;
const part1Elements = part1 ? (
<>
{displayOpt.showUpload && (
dispatch(uploadClicked(0, SelectType.File))}>
{t("application:fileManager.uploadFiles")}
)}
{displayOpt.showEnter && (
dispatch(enterFolder(0, targets[0]))}>
{t("application:fileManager.enter")}
)}
{displayOpt.showUpload && (
dispatch(uploadClicked(0, SelectType.Directory))}>
{t("application:fileManager.uploadFolder")}
)}
{displayOpt.showUpload && (
dispatch(uploadFromClipboard(0))}>
{t("application:uploader.uploadFromClipboard")}
)}
{displayOpt.showRemoteDownload && (
dispatch(newRemoteDownload(0))}>
{t("application:fileManager.newRemoteDownloads")}
)}
{displayOpt.showOpen && (
dispatch(openViewers(fmIndex, targets[0]))}>
{t("application:fileManager.open")}
)}
{showOpenWithCascading && (
}
>
)}
{showOpenWith && (
dispatch(openViewers(fmIndex, targets[0], targets[0].size, undefined, true))}>
{t("application:fileManager.openWith")}
)}
{displayOpt.showDownload && (
dispatch(downloadFiles(fmIndex, targets))}>
{t("application:fileManager.download")}
)}
{displayOpt.showExtractArchive && (
dispatch(extractArchive(fmIndex, targets))}>
{t("application:fileManager.extractArchive")}
)}
{displayOpt.showTorrentRemoteDownload && (
dispatch(newRemoteDownload(fmIndex, targets[0]))}>
{t("application:fileManager.createRemoteDownloadForTorrent")}
)}
>
) : undefined;
const part2Elements = part2 ? (
<>
{displayOpt.showCreateFolder && (
dispatch(createNew(fmIndex, CreateNewDialogType.folder))}>
{t("application:fileManager.newFolder")}
)}
{displayOpt.showCreateFile && (
dispatch(createNew(fmIndex, CreateNewDialogType.file))}>
{t("application:fileManager.newFile")}
)}
{displayOpt.showShare && (
dispatch(openShareDialog(fmIndex, targets))}>
{t("application:fileManager.share")}
)}
{displayOpt.showRename && (
dispatch(renameFile(fmIndex, targets[0]))}>
{t("application:fileManager.rename")}
)}
{displayOpt.showCopy && (
dispatch(dialogBasedMoveCopy(fmIndex, targets, true))}>
{t("application:fileManager.copy")}
)}
{displayOpt.showDirectLink && (
dispatch(batchGetDirectLinks(fmIndex, targets))}>
{t("application:fileManager.getSourceLink")}
)}
>
) : undefined;
const part3Elements = part3 ? (
<>
{displayOpt.showTags && (
}>
)}
{displayOpt.showOrganize && (
}
>
)}
{displayOpt.showMore && (
}
>
)}
{displayOpt.showNewFileFromTemplate && }
>
) : undefined;
const part4Elements = part4 ? (
<>
{displayOpt.showGoToSharedLink && (
dispatch(goToSharedLink(fmIndex, targets[0]))}>
{t("application:fileManager.goToSharedLink")}
)}
{displayOpt.showGoToParent && (
dispatch(goToParent(0, targets[0]))}>
{t("application:fileManager.openParentFolder")}
)}
{displayOpt.showInfo && (
dispatch(openSidebar(fmIndex, targets[0]))}>
{t("application:fileManager.viewDetails")}
)}
>
) : undefined;
const part5Elements = part5 ? (
<>
{displayOpt.showRestore && (
dispatch(restoreFile(fmIndex, targets))}>
{t("application:fileManager.restore")}
)}
{displayOpt.showDelete && (
dispatch(deleteFile(fmIndex, targets))}>
{t("application:fileManager.delete")}
)}
{displayOpt.showRefresh && (
dispatch(refreshFileList(fmIndex))}>
{t("application:fileManager.refresh")}
)}
>
) : undefined;
const allParts = [part1Elements, part2Elements, part3Elements, part4Elements, part5Elements].filter(
(p) => p != undefined,
);
return (
{
e.preventDefault();
},
},
}}
>
{allParts.map((part, index) => (
{part}
{index < allParts.length - 1 && }
))}
{allParts.length == 0 && }
);
};
export default ContextMenu;