Skip to content

Commit 3f1e489

Browse files
committed
fix(settings): tag deletion hangs (@fehmer)
1 parent 0ba7f62 commit 3f1e489

2 files changed

Lines changed: 34 additions & 20 deletions

File tree

frontend/src/ts/collections/results.ts

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ import { queryOptions } from "@tanstack/solid-query";
2424
import { Accessor } from "solid-js";
2525
import Ape from "../ape";
2626
import { SnapshotResult } from "../constants/default-snapshot";
27+
import { createEffectOn } from "../hooks/effects";
2728
import { queryClient } from "../queries";
2829
import { baseKey } from "../queries/utils/keys";
30+
import { isAuthenticated } from "../states/core";
31+
import { getLastResult, setLastResult } from "../states/snapshot";
2932
import {
30-
__nonReactive as tagsNonReactive,
3133
reconcileLocalTagPB,
3234
saveLocalTagPB,
35+
__nonReactive as tagsNonReactive,
3336
} from "./tags";
34-
import { isAuthenticated } from "../states/core";
35-
import { createEffectOn } from "../hooks/effects";
36-
import { getLastResult, setLastResult } from "../states/snapshot";
3737
import { applyIdWorkaround } from "./utils/misc";
3838

3939
export type ResultsQueryState = {
@@ -260,6 +260,9 @@ type ActionType = {
260260
insertLocalResult: {
261261
result: SnapshotResult<Mode>;
262262
};
263+
deleteLocalTag: {
264+
tagId: string;
265+
};
263266
};
264267

265268
const actions = {
@@ -319,6 +322,21 @@ const actions = {
319322
resultsCollection.utils.writeInsert(normalizeResult(result));
320323
},
321324
}),
325+
deleteLocalTag: createOptimisticAction<ActionType["deleteLocalTag"]>({
326+
onMutate: ({ tagId }) => {
327+
for (const result of [...resultsCollection.values()].filter((it) =>
328+
it.tags.includes(tagId),
329+
)) {
330+
resultsCollection.utils.writeUpdate({
331+
...result,
332+
tags: result.tags.filter((it) => it !== tagId),
333+
});
334+
}
335+
},
336+
mutationFn: async () => {
337+
return true;
338+
},
339+
}),
322340
};
323341
// --- Public API ---
324342
export async function updateTags(
@@ -382,6 +400,17 @@ export async function insertLocalResult(
382400
await transaction.isPersisted.promise;
383401
}
384402

403+
export async function deleteLocalTag(
404+
params: ActionType["deleteLocalTag"],
405+
): Promise<void> {
406+
if (!resultsCollection.isReady()) {
407+
//not loaded yet, don't need to update
408+
return;
409+
}
410+
const transtaction = actions.deleteLocalTag(params);
411+
await transtaction.isPersisted.promise;
412+
}
413+
385414
// oxlint-disable-next-line typescript/explicit-function-return-type
386415
export function buildResultsQuery(state: ResultsQueryState) {
387416
const applyMode2Filter = <T extends "time" | "words">(
@@ -622,21 +651,6 @@ function buildSettingsResultsQuery(
622651
return query;
623652
}
624653

625-
export function deleteLocalTag(tagId: string): void {
626-
resultsCollection.utils.writeBatch(() => {
627-
for (const result of [...resultsCollection.values()]) {
628-
if (!result.tags.includes(tagId)) {
629-
continue;
630-
}
631-
632-
resultsCollection.utils.writeUpdate({
633-
...result,
634-
tags: result.tags.filter((it) => it !== tagId),
635-
});
636-
}
637-
});
638-
}
639-
640654
export function isResultsReady(): boolean {
641655
return resultsCollection.isReady();
642656
}

frontend/src/ts/modals/edit-tag.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ const actionModals: Record<Action, SimpleModal> = {
106106
};
107107
}
108108

109-
deleteLocalTag(tagId);
109+
await deleteLocalTag({ tagId });
110110
void Settings.update();
111111

112112
return { status: "success", message: `Tag removed` };

0 commit comments

Comments
 (0)