Skip to content

Commit a97279a

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

2 files changed

Lines changed: 33 additions & 17 deletions

File tree

frontend/src/ts/collections/results.ts

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ import {
3232
saveLocalTagPB,
3333
} from "./tags";
3434
import { isAuthenticated } from "../states/core";
35-
import { createEffectOn } from "../hooks/effects";
3635
import { getLastResult, setLastResult } from "../states/snapshot";
3736
import { applyIdWorkaround } from "./utils/misc";
37+
import { createEffectOn } from "../hooks/effects";
3838

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

265268
const actions = {
@@ -313,12 +316,29 @@ const actions = {
313316
}),
314317
insertLocalResult: createOptimisticAction<ActionType["insertLocalResult"]>({
315318
onMutate: ({ result }) => {
319+
console.log("### insertLocalResult mutate");
316320
resultsCollection.insert(result);
317321
},
318322
mutationFn: async ({ result }) => {
323+
console.log("### insertLocalResult mutationFn");
319324
resultsCollection.utils.writeInsert(normalizeResult(result));
320325
},
321326
}),
327+
deleteLocalTag: createOptimisticAction<ActionType["deleteLocalTag"]>({
328+
onMutate: ({ tagId }) => {
329+
for (const result of [...resultsCollection.values()].filter((it) =>
330+
it.tags.includes(tagId),
331+
)) {
332+
resultsCollection.utils.writeUpdate({
333+
...result,
334+
tags: result.tags.filter((it) => it !== tagId),
335+
});
336+
}
337+
},
338+
mutationFn: async () => {
339+
return true;
340+
},
341+
}),
322342
};
323343
// --- Public API ---
324344
export async function updateTags(
@@ -382,6 +402,17 @@ export async function insertLocalResult(
382402
await transaction.isPersisted.promise;
383403
}
384404

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

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-
640656
export function isResultsReady(): boolean {
641657
return resultsCollection.isReady();
642658
}

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)