|
| 1 | +import { |
| 2 | + getGetFilesQueryKey, |
| 3 | + useMoveFileInProject, |
| 4 | + useMovePath, |
| 5 | +} from "@squonk/data-manager-client/file-and-path"; |
| 6 | +import { getGetProjectQueryKey } from "@squonk/data-manager-client/project"; |
| 7 | + |
| 8 | +import { useQueryClient } from "@tanstack/react-query"; |
| 9 | + |
| 10 | +import { useEnqueueError } from "../useEnqueueStackError"; |
| 11 | + |
| 12 | +export type ProjectObject = "directory" | "file"; |
| 13 | + |
| 14 | +/** |
| 15 | + * Valid pattern for a path that will be accepted by the API. |
| 16 | + * A leading `/` is prepended to the path before being sent |
| 17 | + */ |
| 18 | +export const PATH_PATTERN = /^[A-Za-z0-9-_.]+(\/[A-Za-z0-9-_.]+)*$/u; |
| 19 | + |
| 20 | +const getPathStem = (path: string) => "/" + path.split("/").slice(0, -1).join("/"); |
| 21 | + |
| 22 | +// type OnSettledType<T extends {}> = NonNullable<NonNullable<T>["mutation"]>["onSettled"]; |
| 23 | + |
| 24 | +/** |
| 25 | + * This hook is used to move project files or directories from one path to another. |
| 26 | + * |
| 27 | + * It will handle the appropriate mutation and query cache invalidations. |
| 28 | + * |
| 29 | + * When a file is renamed and/or moved, we must invalidate the query cache for |
| 30 | + * - getGetProjectQueryKey for the current project ID |
| 31 | + * - getGetFilesQueryKey for the source path |
| 32 | + * - getGetFilesQueryKey for the destination path |
| 33 | + * |
| 34 | + * When a directory is moved, we must invalidate the query cache for |
| 35 | + * - getGetProjectQueryKey for the current project ID |
| 36 | + * - getGetFilesQueryKey for the source path |
| 37 | + * - getGetFilesQueryKey for the destination path |
| 38 | + * |
| 39 | + * @param projectId - ID of the project the file or directory belongs to |
| 40 | + * @param srcPath - Path of the file or directory to be moved |
| 41 | + * @param type - Type of the object to be moved |
| 42 | + * @param onSettled - Callback function to be called when the mutation is settled. |
| 43 | + * Useful to reset a form or close a modal. |
| 44 | + */ |
| 45 | +export const useMoveProjectObject = ( |
| 46 | + projectId: string, |
| 47 | + srcPath: string, |
| 48 | + type: ProjectObject, |
| 49 | + onSettled?: () => void, |
| 50 | +) => { |
| 51 | + const { enqueueError, enqueueSnackbar } = useEnqueueError(); |
| 52 | + |
| 53 | + const queryClient = useQueryClient(); |
| 54 | + |
| 55 | + // Invalidate the queries for the project and the source and destination paths |
| 56 | + // Used in each of the mutation calls |
| 57 | + const invalidateQueries = (srcPath: string, dstPath?: string) => { |
| 58 | + const promises = [ |
| 59 | + queryClient.invalidateQueries({ queryKey: getGetProjectQueryKey(projectId) }), |
| 60 | + queryClient.invalidateQueries({ |
| 61 | + queryKey: getGetFilesQueryKey({ project_id: projectId, path: getPathStem(srcPath) }), |
| 62 | + }), |
| 63 | + ]; |
| 64 | + if (dstPath) { |
| 65 | + promises.push( |
| 66 | + queryClient.invalidateQueries({ |
| 67 | + queryKey: getGetFilesQueryKey({ project_id: projectId, path: getPathStem(dstPath) }), |
| 68 | + }), |
| 69 | + ); |
| 70 | + } |
| 71 | + return Promise.allSettled(promises); |
| 72 | + }; |
| 73 | + |
| 74 | + // Mutation Hooks |
| 75 | + const { |
| 76 | + mutate: moveFile, |
| 77 | + isPending: filePending, |
| 78 | + error: fileError, |
| 79 | + } = useMoveFileInProject({ |
| 80 | + mutation: { |
| 81 | + onSettled: async (_data, _error, { params: { src_path, dst_file } }) => { |
| 82 | + await invalidateQueries(src_path as string, dst_file as string); |
| 83 | + onSettled && onSettled(); |
| 84 | + }, |
| 85 | + onSuccess: () => { |
| 86 | + enqueueSnackbar({ message: "File renamed and/or moved", variant: "success" }); |
| 87 | + }, |
| 88 | + onError: (error) => enqueueError(error), |
| 89 | + }, |
| 90 | + }); |
| 91 | + |
| 92 | + const { |
| 93 | + mutate: moveDirectory, |
| 94 | + isPending: directoryPending, |
| 95 | + error: directoryError, |
| 96 | + } = useMovePath({ |
| 97 | + mutation: { |
| 98 | + onSettled: async (_data, _error, { params: { src_path } }) => { |
| 99 | + await invalidateQueries(src_path as string); |
| 100 | + onSettled && onSettled(); |
| 101 | + }, |
| 102 | + onSuccess: () => { |
| 103 | + enqueueSnackbar({ message: "Directory renamed and/or moved", variant: "success" }); |
| 104 | + }, |
| 105 | + onError: (error) => enqueueError(error), |
| 106 | + }, |
| 107 | + }); |
| 108 | + |
| 109 | + // Handlers |
| 110 | + const handleFileMove = (dstPath: string, mutationOptions?: Parameters<typeof moveFile>[1]) => { |
| 111 | + // compute the query parameters for the requests |
| 112 | + // E.g. for a type="file": /path/to/file.txt -> /new/path/to/file.txt |
| 113 | + const fileName = srcPath.split("/").pop() as string; // file.txt |
| 114 | + const srcPathStem = getPathStem(srcPath); // /path/to |
| 115 | + const dstPathStem = getPathStem(dstPath); // /new/path/to |
| 116 | + const dstFile = dstPath.trim().split("/").pop() as string; // file.txt |
| 117 | + |
| 118 | + moveFile( |
| 119 | + { |
| 120 | + params: { |
| 121 | + project_id: projectId, |
| 122 | + file: fileName, |
| 123 | + src_path: srcPathStem, |
| 124 | + dst_path: dstPathStem, |
| 125 | + dst_file: dstFile, |
| 126 | + }, |
| 127 | + }, |
| 128 | + mutationOptions, |
| 129 | + ); |
| 130 | + }; |
| 131 | + |
| 132 | + const handleDirectoryMove = ( |
| 133 | + dstPath: string, |
| 134 | + mutationOptions?: Parameters<typeof moveDirectory>[1], |
| 135 | + ) => { |
| 136 | + moveDirectory( |
| 137 | + { |
| 138 | + params: { |
| 139 | + project_id: projectId, |
| 140 | + dst_path: "/" + dstPath, |
| 141 | + src_path: "/" + srcPath, |
| 142 | + }, |
| 143 | + }, |
| 144 | + mutationOptions, |
| 145 | + ); |
| 146 | + }; |
| 147 | + |
| 148 | + if (type === "directory") { |
| 149 | + return { handleMove: handleDirectoryMove, isPending: directoryPending, error: directoryError }; |
| 150 | + } |
| 151 | + return { handleMove: handleFileMove, isPending: filePending, error: fileError }; |
| 152 | +}; |
0 commit comments