|
1 | 1 | import { useQueryClient } from '@tanstack/react-query' |
2 | | -import { useCallback, useEffect, useRef, useState } from 'react' |
| 2 | +import { useCallback, useEffect, useState } from 'react' |
3 | 3 | import { useTranslation } from 'react-i18next' |
4 | 4 | import styled, { css } from 'styled-components' |
5 | 5 | import { useClient } from 'wagmi' |
@@ -43,120 +43,53 @@ export const TransactionNotifications = () => { |
43 | 43 |
|
44 | 44 | const [open, setOpen] = useState(false) |
45 | 45 |
|
46 | | - const { resumeTransactionFlow, getResumable, getLatestTransaction } = useTransactionFlow() |
| 46 | + const { resumeTransactionFlow, getResumable } = useTransactionFlow() |
47 | 47 |
|
48 | 48 | const [notificationQueue, setNotificationQueue] = useState<Notification[]>([]) |
49 | 49 | const currentNotification = notificationQueue[0] |
50 | 50 |
|
51 | | - // Use ref to avoid recreating callback when state.items changes |
52 | | - // This prevents cascading re-renders that can restart gas estimation |
53 | | - const getLatestTransactionRef = useRef(getLatestTransaction) |
54 | | - useEffect(() => { |
55 | | - getLatestTransactionRef.current = getLatestTransaction |
56 | | - }, [getLatestTransaction]) |
57 | | - |
58 | 51 | const updateCallback = useCallback<UpdateCallback>( |
59 | | - ({ action, key, status, hash }) => { |
| 52 | + ({ action, key, status, hash, cacheBust }) => { |
60 | 53 | if (status === 'pending' || status === 'repriced') return |
61 | 54 | if (status === 'confirmed') { |
62 | | - // Get transaction data for cache busting (use ref to avoid dependency) |
63 | | - const latestTx = key ? getLatestTransactionRef.current(key) : undefined |
64 | | - const txData = latestTx?.data as { name?: string; records?: unknown } | undefined |
65 | | - const ensName = txData?.name |
66 | | - |
| 55 | + // Handle analytics events |
67 | 56 | switch (action) { |
68 | | - case 'registerName': { |
| 57 | + case 'registerName': |
69 | 58 | trackEvent('register', chainName) |
70 | | - // Bust cache for avatar/header if being set during registration |
71 | | - if (ensName && client) { |
72 | | - const registrationData = txData as { |
73 | | - name: string |
74 | | - records?: { texts?: Array<{ key: string }> } |
75 | | - } |
76 | | - const hasAvatarChange = registrationData.records?.texts?.some( |
77 | | - (text) => text.key === 'avatar', |
78 | | - ) |
79 | | - const hasHeaderChange = registrationData.records?.texts?.some( |
80 | | - (text) => text.key === 'header', |
81 | | - ) |
82 | | - if (hasAvatarChange) bustMediaCache(ensName, client as ClientWithEns, 'avatar') |
83 | | - if (hasHeaderChange) bustMediaCache(ensName, client as ClientWithEns, 'header') |
84 | | - } |
85 | | - queryClient.invalidateQueries({ queryKey: [META_DATA_QUERY_KEY] }) |
86 | 59 | break |
87 | | - } |
88 | 60 | case 'commitName': |
89 | 61 | trackEvent('commit', chainName) |
90 | 62 | break |
91 | 63 | case 'extendNames': |
92 | 64 | trackEvent('renew', chainName) |
93 | 65 | break |
94 | | - case 'updateResolver': |
95 | | - case 'resetProfile': { |
96 | | - // These actions always bust both avatar and header |
97 | | - if (ensName && client) { |
98 | | - bustMediaCache(ensName, client as ClientWithEns) |
99 | | - } |
100 | | - queryClient.invalidateQueries({ queryKey: [META_DATA_QUERY_KEY] }) |
101 | | - break |
102 | | - } |
103 | | - case 'updateProfile': |
104 | | - case 'updateProfileRecords': { |
105 | | - // Check if avatar or header are being modified in records |
106 | | - if (ensName && client) { |
107 | | - const profileData = txData as { |
108 | | - name: string |
109 | | - records?: |
110 | | - | { texts?: Array<{ key: string }> } |
111 | | - | Array<{ key: string; group?: string }> |
112 | | - } |
113 | | - // Handle both RecordOptions format and ProfileRecord[] format |
114 | | - const { records } = profileData |
115 | | - let hasAvatarChange = false |
116 | | - let hasHeaderChange = false |
117 | | - if (Array.isArray(records)) { |
118 | | - // ProfileRecord[] format (updateProfileRecords) |
119 | | - hasAvatarChange = records.some( |
120 | | - (r) => r.key === 'avatar' && (r as { group?: string }).group === 'media', |
121 | | - ) |
122 | | - hasHeaderChange = records.some( |
123 | | - (r) => r.key === 'header' && (r as { group?: string }).group === 'media', |
124 | | - ) |
125 | | - } else if (records?.texts) { |
126 | | - // RecordOptions format (updateProfile) |
127 | | - hasAvatarChange = records.texts.some((text) => text.key === 'avatar') |
128 | | - hasHeaderChange = records.texts.some((text) => text.key === 'header') |
129 | | - } |
130 | | - if (hasAvatarChange) bustMediaCache(ensName, client as ClientWithEns, 'avatar') |
131 | | - if (hasHeaderChange) bustMediaCache(ensName, client as ClientWithEns, 'header') |
132 | | - } |
133 | | - queryClient.invalidateQueries({ queryKey: [META_DATA_QUERY_KEY] }) |
134 | | - break |
135 | | - } |
136 | | - case 'resetProfileWithRecords': |
137 | | - case 'migrateProfile': |
138 | | - case 'migrateProfileWithReset': { |
139 | | - // Check if avatar or header are in the records being set |
140 | | - if (ensName && client) { |
141 | | - const recordsData = txData as { |
142 | | - name: string |
143 | | - records?: { texts?: Array<{ key: string }> } |
144 | | - } |
145 | | - const hasAvatarChange = recordsData.records?.texts?.some( |
146 | | - (text) => text.key === 'avatar', |
147 | | - ) |
148 | | - const hasHeaderChange = recordsData.records?.texts?.some( |
149 | | - (text) => text.key === 'header', |
150 | | - ) |
151 | | - if (hasAvatarChange) bustMediaCache(ensName, client as ClientWithEns, 'avatar') |
152 | | - if (hasHeaderChange) bustMediaCache(ensName, client as ClientWithEns, 'header') |
153 | | - } |
154 | | - queryClient.invalidateQueries({ queryKey: [META_DATA_QUERY_KEY] }) |
155 | | - break |
156 | | - } |
157 | 66 | default: |
158 | 67 | break |
159 | 68 | } |
| 69 | + |
| 70 | + // Use pre-computed cache bust flags from transaction store |
| 71 | + if (cacheBust && client) { |
| 72 | + const { avatar, header, name } = cacheBust |
| 73 | + if (name) { |
| 74 | + if (avatar) bustMediaCache(name, client as ClientWithEns, 'avatar') |
| 75 | + if (header) bustMediaCache(name, client as ClientWithEns, 'header') |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Invalidate metadata queries for actions that modify profile data |
| 80 | + const profileActions = [ |
| 81 | + 'registerName', |
| 82 | + 'updateResolver', |
| 83 | + 'resetProfile', |
| 84 | + 'updateProfile', |
| 85 | + 'updateProfileRecords', |
| 86 | + 'resetProfileWithRecords', |
| 87 | + 'migrateProfile', |
| 88 | + 'migrateProfileWithReset', |
| 89 | + ] |
| 90 | + if (profileActions.includes(action)) { |
| 91 | + queryClient.invalidateQueries({ queryKey: [META_DATA_QUERY_KEY] }) |
| 92 | + } |
160 | 93 | } |
161 | 94 | const resumable = key && getResumable(key) |
162 | 95 | const item = { |
|
0 commit comments