fix: file deletion logic to update recent files and handle errors #146
This commit is contained in:
+11
-1
@@ -4153,6 +4153,7 @@ export class JMAPClient implements IJMAPClient {
|
|||||||
[["FileNode/set", {
|
[["FileNode/set", {
|
||||||
accountId,
|
accountId,
|
||||||
destroy: ids,
|
destroy: ids,
|
||||||
|
onDestroyRemoveChildren: true,
|
||||||
}, "fns0"]],
|
}, "fns0"]],
|
||||||
this.fileUsing(),
|
this.fileUsing(),
|
||||||
);
|
);
|
||||||
@@ -4161,9 +4162,18 @@ export class JMAPClient implements IJMAPClient {
|
|||||||
if (!result || result[0] === "error") {
|
if (!result || result[0] === "error") {
|
||||||
throw new Error(result?.[1]?.description || "FileNode/set destroy failed");
|
throw new Error(result?.[1]?.description || "FileNode/set destroy failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const notDestroyedMap: Record<string, { type?: string; description?: string }> = result[1].notDestroyed || {};
|
||||||
|
const notDestroyedIds = Object.keys(notDestroyedMap);
|
||||||
|
|
||||||
|
if (notDestroyedIds.length > 0) {
|
||||||
|
const firstError = notDestroyedMap[notDestroyedIds[0]];
|
||||||
|
throw new Error(firstError?.description || `Failed to delete ${notDestroyedIds.length} file(s)`);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
destroyed: result[1].destroyed || [],
|
destroyed: result[1].destroyed || [],
|
||||||
notDestroyed: result[1].notDestroyed ? Object.keys(result[1].notDestroyed) : [],
|
notDestroyed: [],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+10
-2
@@ -408,7 +408,7 @@ export const useFileStore = create<FileState>((set, get) => ({
|
|||||||
},
|
},
|
||||||
|
|
||||||
deleteResource: async (name: string) => {
|
deleteResource: async (name: string) => {
|
||||||
const { client, resources, refresh } = get();
|
const { client, resources, recentFiles, refresh } = get();
|
||||||
if (!client) return;
|
if (!client) return;
|
||||||
|
|
||||||
const resource = resources.find(r => r.name === name);
|
const resource = resources.find(r => r.name === name);
|
||||||
@@ -428,11 +428,15 @@ export const useFileStore = create<FileState>((set, get) => ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
await client.destroyFileNodes(idsToDelete);
|
await client.destroyFileNodes(idsToDelete);
|
||||||
|
const deletedIdSet = new Set(idsToDelete);
|
||||||
|
const nextRecentFiles = recentFiles.filter(r => !deletedIdSet.has(r.id));
|
||||||
|
set({ recentFiles: nextRecentFiles });
|
||||||
|
try { localStorage.setItem('files-recent-files', JSON.stringify(nextRecentFiles)); } catch { /* ignore */ }
|
||||||
await refresh();
|
await refresh();
|
||||||
},
|
},
|
||||||
|
|
||||||
deleteResources: async (names: string[]) => {
|
deleteResources: async (names: string[]) => {
|
||||||
const { client, resources, refresh } = get();
|
const { client, resources, recentFiles, refresh } = get();
|
||||||
if (!client) return;
|
if (!client) return;
|
||||||
|
|
||||||
const idsToDelete: string[] = [];
|
const idsToDelete: string[] = [];
|
||||||
@@ -457,7 +461,11 @@ export const useFileStore = create<FileState>((set, get) => ({
|
|||||||
if (idsToDelete.length === 0) return;
|
if (idsToDelete.length === 0) return;
|
||||||
|
|
||||||
await client.destroyFileNodes(idsToDelete);
|
await client.destroyFileNodes(idsToDelete);
|
||||||
|
const deletedIdSet = new Set(idsToDelete);
|
||||||
|
const nextRecentFiles = recentFiles.filter(r => !deletedIdSet.has(r.id));
|
||||||
set({ selectedResources: new Set() });
|
set({ selectedResources: new Set() });
|
||||||
|
set({ recentFiles: nextRecentFiles });
|
||||||
|
try { localStorage.setItem('files-recent-files', JSON.stringify(nextRecentFiles)); } catch { /* ignore */ }
|
||||||
await refresh();
|
await refresh();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user