|
| 1 | +import { useState } from "react"; |
| 2 | + |
| 3 | +import { |
| 4 | + getGetFilesQueryKey, |
| 5 | + useMoveFileInProject, |
| 6 | + useMovePath, |
| 7 | +} from "@squonk/data-manager-client/file-and-path"; |
| 8 | +import { getGetProjectQueryKey } from "@squonk/data-manager-client/project"; |
| 9 | + |
| 10 | +import DriveFileRenameOutlineRoundedIcon from "@mui/icons-material/DriveFileRenameOutlineRounded"; |
| 11 | +import { Box, IconButton, type IconButtonProps, Tooltip } from "@mui/material"; |
| 12 | +import { useQueryClient } from "@tanstack/react-query"; |
| 13 | +import { Field } from "formik"; |
| 14 | +import { TextField } from "formik-mui"; |
| 15 | + |
| 16 | +import { FormikModalWrapper } from "../../../components/modals/FormikModalWrapper"; |
| 17 | +import { useEnqueueError } from "../../../hooks/useEnqueueStackError"; |
| 18 | + |
| 19 | +// removed type from IconButtonProps as it's also defined there |
| 20 | +export interface RenameButtonProps extends Omit<IconButtonProps, "type"> { |
| 21 | + projectId: string; |
| 22 | + type: "directory" | "file"; |
| 23 | + path: string; |
| 24 | +} |
| 25 | + |
| 26 | +export const RenameButton = ({ projectId, type, path, ...buttonProps }: RenameButtonProps) => { |
| 27 | + const { mutateAsync: moveFile } = useMoveFileInProject(); |
| 28 | + const { mutateAsync: moveDirectory } = useMovePath(); |
| 29 | + |
| 30 | + const [open, setOpen] = useState(false); |
| 31 | + const initialValues = { |
| 32 | + dstPath: path, |
| 33 | + }; |
| 34 | + |
| 35 | + const { enqueueSnackbar, enqueueError } = useEnqueueError(); |
| 36 | + |
| 37 | + const queryClient = useQueryClient(); |
| 38 | + |
| 39 | + const handleSubmit = async ({ dstPath }: typeof initialValues) => { |
| 40 | + try { |
| 41 | + // E.g. for a type="file": /path/to/file.txt -> /new/path/to/file.txt |
| 42 | + const fileName = path.split("/").pop() as string; // file.txt |
| 43 | + const srcPath = "/" + path.split("/").slice(0, -1).join("/"); // /path/to |
| 44 | + const dstPathStem = "/" + dstPath.split("/").slice(0, -1).join("/"); // /new/path/to |
| 45 | + const dstFile = dstPath.split("/").pop() as string; // file.txt |
| 46 | + |
| 47 | + await (type === "directory" |
| 48 | + ? moveDirectory({ |
| 49 | + params: { |
| 50 | + project_id: projectId, |
| 51 | + dst_path: "/" + dstPath, |
| 52 | + src_path: "/" + path, |
| 53 | + }, |
| 54 | + }) |
| 55 | + : moveFile({ |
| 56 | + params: { |
| 57 | + project_id: projectId, |
| 58 | + file: fileName, |
| 59 | + src_path: srcPath, |
| 60 | + dst_path: dstPathStem, |
| 61 | + dst_file: dstFile, |
| 62 | + }, |
| 63 | + })); |
| 64 | + |
| 65 | + enqueueSnackbar({ message: "Renamed and/or moved", variant: "success" }); |
| 66 | + |
| 67 | + await Promise.allSettled([ |
| 68 | + queryClient.invalidateQueries({ queryKey: getGetProjectQueryKey(projectId) }), |
| 69 | + queryClient.invalidateQueries({ |
| 70 | + queryKey: getGetFilesQueryKey({ project_id: projectId, path: srcPath }), |
| 71 | + }), |
| 72 | + queryClient.invalidateQueries({ |
| 73 | + queryKey: getGetFilesQueryKey({ project_id: projectId, path: dstPathStem }), |
| 74 | + }), |
| 75 | + ]); |
| 76 | + |
| 77 | + setOpen(false); |
| 78 | + } catch (error) { |
| 79 | + enqueueError(error); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + return ( |
| 84 | + <> |
| 85 | + <Tooltip title="Rename / Move"> |
| 86 | + <IconButton {...buttonProps} size="small" onClick={() => setOpen(true)}> |
| 87 | + <DriveFileRenameOutlineRoundedIcon /> |
| 88 | + </IconButton> |
| 89 | + </Tooltip> |
| 90 | + <FormikModalWrapper |
| 91 | + id={`rename-${path}`} |
| 92 | + initialValues={initialValues} |
| 93 | + open={open} |
| 94 | + submitText="Rename / Move" |
| 95 | + title="Rename / Move" |
| 96 | + onClose={() => setOpen(false)} |
| 97 | + onSubmit={handleSubmit} |
| 98 | + > |
| 99 | + <Box p={1}> |
| 100 | + <Field |
| 101 | + autoFocus |
| 102 | + component={TextField} |
| 103 | + label="Destination Path" |
| 104 | + name="dstPath" |
| 105 | + sx={{ minWidth: "300px" }} |
| 106 | + /> |
| 107 | + </Box> |
| 108 | + </FormikModalWrapper> |
| 109 | + </> |
| 110 | + ); |
| 111 | +}; |
0 commit comments