Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions packages/common/src/api/tan-query/users/useUpdateProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,28 @@ export const useUpdateProfile = () => {

return { previousMetadata }
},
onError: (error, metadata, context?: MutationContext) => {
// If the mutation fails, roll back user data
onSuccess: (_data, variables) => {
// Re-confirm the mutation in the cache after server success.
// We re-apply mutation variables rather than the raw server response
// to guard against discovery-node propagation lag: the node answering
// getUser may not yet have indexed the confirmed block.
const currentCache = queryClient.getQueryData<UserMetadata>(
getUserQueryKey(currentUserId)
)
if (currentCache) {
primeUserData({
queryClient,
users: [{ ...currentCache, ...variables }],
forceReplace: true
})
}
},
onError: (error, _metadata, context?: MutationContext) => {
// If the mutation fails, roll back user data to previous state
if (context?.previousMetadata) {
primeUserData({
queryClient,
users: [{ ...context.previousMetadata, ...metadata }],
users: [context.previousMetadata],
forceReplace: true
})
}
Expand Down
13 changes: 12 additions & 1 deletion packages/common/src/store/account/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,18 @@ function* setLocalStorageAccountAndUser(
}

yield* call([localStorage, localStorage.setAudiusAccount], formattedAccount)
yield* call([localStorage, localStorage.setAudiusAccountUser], accountUser)
// Prefer cached user data (which may include recent optimistic mutations
// like profile_type changes) over the server-fetched response. This prevents
// setLocalStorageAccountAndUser from overwriting changes that haven't yet
// propagated to all discovery nodes.
const queryClient = yield* getContext('queryClient')
const cachedUser = queryClient.getQueryData<UserMetadata>(
getUserQueryKey(accountUser.user_id)
)
yield* call(
[localStorage, localStorage.setAudiusAccountUser],
cachedUser ?? accountUser
)
}

function* recordIPIfNotRecent(handle: string): SagaIterator {
Expand Down
Loading