@@ -24,16 +24,16 @@ import { queryOptions } from "@tanstack/solid-query";
2424import { Accessor } from "solid-js" ;
2525import Ape from "../ape" ;
2626import { SnapshotResult } from "../constants/default-snapshot" ;
27+ import { createEffectOn } from "../hooks/effects" ;
2728import { queryClient } from "../queries" ;
2829import { baseKey } from "../queries/utils/keys" ;
30+ import { isAuthenticated } from "../states/core" ;
31+ import { getLastResult , setLastResult } from "../states/snapshot" ;
2932import {
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" ;
3737import { applyIdWorkaround } from "./utils/misc" ;
3838
3939export type ResultsQueryState = {
@@ -260,6 +260,9 @@ type ActionType = {
260260 insertLocalResult : {
261261 result : SnapshotResult < Mode > ;
262262 } ;
263+ deleteLocalTag : {
264+ tagId : string ;
265+ } ;
263266} ;
264267
265268const 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 ---
324342export 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
386415export 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-
640654export function isResultsReady ( ) : boolean {
641655 return resultsCollection . isReady ( ) ;
642656}
0 commit comments