@@ -32,9 +32,9 @@ import {
3232 saveLocalTagPB ,
3333} from "./tags" ;
3434import { isAuthenticated } from "../states/core" ;
35- import { createEffectOn } from "../hooks/effects" ;
3635import { getLastResult , setLastResult } from "../states/snapshot" ;
3736import { applyIdWorkaround } from "./utils/misc" ;
37+ import { createEffectOn } from "../hooks/effects" ;
3838
3939export 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
265268const 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 ---
324344export 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
386417export 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-
640656export function isResultsReady ( ) : boolean {
641657 return resultsCollection . isReady ( ) ;
642658}
0 commit comments