diff --git a/packages/common/src/api/tan-query/users/useUpdateProfile.ts b/packages/common/src/api/tan-query/users/useUpdateProfile.ts index 61e04fcaeb9..51a878060e3 100644 --- a/packages/common/src/api/tan-query/users/useUpdateProfile.ts +++ b/packages/common/src/api/tan-query/users/useUpdateProfile.ts @@ -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( + 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 }) } diff --git a/packages/common/src/store/account/sagas.ts b/packages/common/src/store/account/sagas.ts index 18ee14e2e0a..1a44491a176 100644 --- a/packages/common/src/store/account/sagas.ts +++ b/packages/common/src/store/account/sagas.ts @@ -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( + getUserQueryKey(accountUser.user_id) + ) + yield* call( + [localStorage, localStorage.setAudiusAccountUser], + cachedUser ?? accountUser + ) } function* recordIPIfNotRecent(handle: string): SagaIterator {