2022-02-01 17:41:24 +07:00
|
|
|
import _find from "lodash/find";
|
|
|
|
|
import CopyCells from "@src/assets/icons/CopyCells";
|
|
|
|
|
import Cut from "@src/assets/icons/Cut";
|
|
|
|
|
import Paste from "@src/assets/icons/Paste";
|
|
|
|
|
import { useProjectContext } from "@src/contexts/ProjectContext";
|
2022-02-02 18:07:52 +07:00
|
|
|
import { useSnackbar } from "notistack";
|
2022-02-02 20:13:30 +07:00
|
|
|
import { SelectedCell } from "@src/atoms/ContextMenu";
|
2022-02-01 17:41:24 +07:00
|
|
|
|
2022-02-02 18:07:52 +07:00
|
|
|
export interface IContextMenuActions {
|
|
|
|
|
label: string;
|
|
|
|
|
icon: React.ReactNode;
|
|
|
|
|
onClick: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 20:13:30 +07:00
|
|
|
export default function BasicContextMenuActions(
|
|
|
|
|
selectedCell: SelectedCell,
|
|
|
|
|
reset: () => void | Promise<void>
|
|
|
|
|
): IContextMenuActions[] {
|
|
|
|
|
const { tableState, deleteCell, updateCell } = useProjectContext();
|
2022-02-02 18:07:52 +07:00
|
|
|
const { enqueueSnackbar } = useSnackbar();
|
2022-02-01 17:41:24 +07:00
|
|
|
const columns = tableState?.columns;
|
|
|
|
|
const rows = tableState?.rows;
|
2022-02-02 20:13:30 +07:00
|
|
|
const selectedRowIndex = selectedCell.rowIndex as number;
|
|
|
|
|
const selectedColIndex = selectedCell?.colIndex;
|
2022-02-01 17:41:24 +07:00
|
|
|
const selectedCol = _find(columns, { index: selectedColIndex });
|
|
|
|
|
const selectedRow = rows?.[selectedRowIndex];
|
|
|
|
|
|
2022-02-02 20:13:30 +07:00
|
|
|
const handleClose = () => reset?.();
|
2022-02-01 17:41:24 +07:00
|
|
|
|
|
|
|
|
const handleCopy = () => {
|
|
|
|
|
const cell = selectedRow?.[selectedCol.key];
|
2022-02-02 18:07:52 +07:00
|
|
|
const onFail = () =>
|
|
|
|
|
enqueueSnackbar("Failed to copy", { variant: "error" });
|
|
|
|
|
const onSuccess = () =>
|
|
|
|
|
enqueueSnackbar("Copied to clipboard", { variant: "success" });
|
2022-02-01 17:41:24 +07:00
|
|
|
const copy = navigator.clipboard.writeText(JSON.stringify(cell));
|
|
|
|
|
copy.then(onSuccess, onFail);
|
|
|
|
|
|
|
|
|
|
handleClose();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleCut = () => {
|
|
|
|
|
const cell = selectedRow?.[selectedCol.key];
|
|
|
|
|
const notUndefined = Boolean(typeof cell !== "undefined");
|
|
|
|
|
if (deleteCell && notUndefined)
|
|
|
|
|
deleteCell(selectedRow?.ref, selectedCol?.key);
|
|
|
|
|
|
|
|
|
|
handleClose();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handlePaste = () => {
|
|
|
|
|
const paste = navigator.clipboard.readText();
|
|
|
|
|
paste.then(async (clipText) => {
|
|
|
|
|
try {
|
|
|
|
|
const paste = await JSON.parse(clipText);
|
|
|
|
|
updateCell?.(selectedRow?.ref, selectedCol.key, paste);
|
|
|
|
|
} catch (error) {
|
2022-02-02 18:07:52 +07:00
|
|
|
enqueueSnackbar(`${error}`, { variant: "error" });
|
2022-02-01 17:41:24 +07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
handleClose();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// const handleDisable = () => {
|
|
|
|
|
// const cell = selectedRow?.[selectedCol.key];
|
|
|
|
|
// return typeof cell === "undefined" ? true : false;
|
|
|
|
|
// };
|
|
|
|
|
|
2022-02-02 18:07:52 +07:00
|
|
|
const contextMenuActions = [
|
2022-02-01 17:41:24 +07:00
|
|
|
{ label: "Cut", icon: <Cut />, onClick: handleCut },
|
|
|
|
|
{ label: "Copy", icon: <CopyCells />, onClick: handleCopy },
|
|
|
|
|
{ label: "Paste", icon: <Paste />, onClick: handlePaste },
|
|
|
|
|
];
|
2022-02-02 18:07:52 +07:00
|
|
|
|
|
|
|
|
return contextMenuActions;
|
2022-02-01 17:41:24 +07:00
|
|
|
}
|