Skip to content

Commit 8edc8d6

Browse files
committed
fix: optimistic delete for developer apps
Remove the app from the list immediately on delete click rather than waiting for the API response. Restores previous state on error. Moves list update from onSuccess to onMutate (with cancelQueries to avoid race conditions) and adds onError rollback.
1 parent 8f0ec60 commit 8edc8d6

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

packages/common/src/api/tan-query/developer-apps/useDeleteDeveloperApp.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import { useMutation, useQueryClient } from '@tanstack/react-query'
33

44
import { useQueryContext } from '~/api/tan-query/utils'
55
import { DeveloperApp } from '~/schemas/developerApps'
6-
76
import { useCurrentUserId } from '../users/account/useCurrentUserId'
8-
97
import { getDeveloperAppsQueryKey } from './useDeveloperApps'
108

119
export const useDeleteDeveloperApp = () => {
@@ -19,24 +17,32 @@ export const useDeleteDeveloperApp = () => {
1917
throw new Error('No current user ID')
2018
}
2119
const sdk = await audiusSdk()
22-
2320
await sdk.developerApps.deleteDeveloperApp({
2421
userId: Id.parse(currentUserId),
2522
address: apiKey
2623
})
2724
return {}
2825
},
29-
onSuccess: (_response, apiKey) => {
30-
if (!currentUserId) {
31-
throw new Error('No current user ID')
32-
}
26+
onMutate: async (apiKey) => {
27+
if (!currentUserId) return
28+
const queryKey = getDeveloperAppsQueryKey(currentUserId)
29+
await queryClient.cancelQueries({ queryKey })
30+
const previousApps = queryClient.getQueryData<DeveloperApp[]>(queryKey)
3331
queryClient.setQueryData(
34-
getDeveloperAppsQueryKey(currentUserId),
32+
queryKey,
3533
(oldData: DeveloperApp[] | undefined) => {
3634
if (!oldData) return []
3735
return oldData.filter((app) => app.apiKey !== apiKey)
3836
}
3937
)
38+
return { previousApps }
39+
},
40+
onError: (_error, _apiKey, context) => {
41+
if (!currentUserId || !context?.previousApps) return
42+
queryClient.setQueryData(
43+
getDeveloperAppsQueryKey(currentUserId),
44+
context.previousApps
45+
)
4046
}
4147
})
4248
}

0 commit comments

Comments
 (0)