mirror of
https://github.com/nimbusdotstorage/Nimbus
synced 2026-04-22 17:45:03 +02:00
Added: drag-n-drop to move - bare-minimum working solution
This commit is contained in:
@@ -4,6 +4,7 @@ import type {
|
||||
File,
|
||||
GetFileByIdSchema,
|
||||
GetFilesSchema,
|
||||
MoveFileSchema,
|
||||
UpdateFileSchema,
|
||||
UploadFileSchema,
|
||||
} from "@nimbus/shared";
|
||||
@@ -307,6 +308,53 @@ export function useDownloadFile() {
|
||||
});
|
||||
}
|
||||
|
||||
export function useMoveFile() {
|
||||
const queryClient = useQueryClient();
|
||||
const { clientPromise } = useAccountProvider();
|
||||
return useMutation({
|
||||
mutationFn: async ({ sourceId, targetParentId, newName }: MoveFileSchema) => {
|
||||
const BASE_FILE_CLIENT = await getBaseFileClient(clientPromise);
|
||||
const response = await handleUnauthorizedError(
|
||||
() =>
|
||||
BASE_FILE_CLIENT.move.$post({
|
||||
json: {
|
||||
sourceId,
|
||||
targetParentId,
|
||||
newName,
|
||||
},
|
||||
}),
|
||||
"Failed to move file"
|
||||
);
|
||||
return await response.json();
|
||||
},
|
||||
// onMutate: async ({ sourceId, targetParentId, newName }) => {
|
||||
// await queryClient.cancelQueries({ queryKey: ["files"] });
|
||||
|
||||
// const previousFiles = queryClient.getQueryData<File[]>(["files"]);
|
||||
|
||||
// queryClient.setQueryData(["files", sourceId], (old: File[] = []) =>
|
||||
// old.map(file => (file.id === sourceId ? { ...file, parentId: targetParentId, name: newName } : file))
|
||||
// );
|
||||
|
||||
// return { previousFiles };
|
||||
// },
|
||||
onError: (_, __, context) => {
|
||||
// if (context?.previousFiles) {
|
||||
// queryClient.setQueryData(["files"], context.previousFiles);
|
||||
// }
|
||||
toast.error("Failed to move file");
|
||||
},
|
||||
onSettled: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ["files"] });
|
||||
},
|
||||
|
||||
onSuccess: async () => {
|
||||
toast.success("File moved successfully");
|
||||
await queryClient.invalidateQueries({ queryKey: ["files"] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function getBaseFileClient(clientPromise: Promise<DriveProviderClient>) {
|
||||
const client = await clientPromise;
|
||||
const BASE_FILE_CLIENT = client.api.files;
|
||||
|
||||
Reference in New Issue
Block a user