removing contact cache to fix receive options change

This commit is contained in:
Blake Kaufman
2026-07-26 17:38:42 -04:00
parent 46db9b6cce
commit 17128cd478
2 changed files with 19 additions and 166 deletions
@@ -70,11 +70,8 @@ export default function useContactPayment({
const { contactsPrivateKey } = useKeysContext();
const { isConnectedToTheInternet } = useAppStatus();
const { fiatStats } = useNodeContext();
const {
globalContactsInformation,
getContactReceiveOption,
isContactReceiveOptionCacheLoaded,
} = useGlobalContactsInfo();
const { globalContactsInformation, getContactReceiveOption } =
useGlobalContactsInfo();
const getServerTime = useServerTimeOnly();
const { currentWalletMnemoinc } = useActiveCustodyAccount();
const { showToast } = useToast();
@@ -168,21 +165,18 @@ export default function useContactPayment({
setPrefetchedDoc(null);
setContactReceiveOption(null);
if (!isContactReceiveOptionCacheLoaded) {
return () => {
isCurrent = false;
};
}
const MAX_ATTEMPTS = 5;
const BASE_DELAY_MS = 375;
const fetchWithBackoff = async (attempt = 0) => {
try {
const result = await getContactReceiveOption(selectedContact.uuid);
// getDataFromCollection swallows its errors and returns null, so a
// missing doc is the only signal a read failed. Throw so the retry runs.
if (!result.contactDoc) throw new Error('contact doc unavailable');
if (isCurrent) {
setContactReceiveOption(result.receiveOption ?? null);
setPrefetchedDoc(result.contactDoc ?? null);
setContactReceiveOption(result.receiveOption);
setPrefetchedDoc(result.contactDoc);
}
} catch {
if (!isCurrent) return;
@@ -202,12 +196,7 @@ export default function useContactPayment({
return () => {
isCurrent = false;
};
}, [
getContactReceiveOption,
isContactReceiveOptionCacheLoaded,
requiresContactDoc,
selectedContact?.uuid,
]);
}, [getContactReceiveOption, requiresContactDoc, selectedContact?.uuid]);
// For LNURL contacts the receive address must be resolved against its
// .well-known/lnurlp endpoint to learn the min/max sendable bounds. Fetch it
+11 -147
View File
@@ -15,11 +15,6 @@ import {
decryptMessage,
encriptMessage,
} from '../app/functions/messaging/encodingAndDecodingMessages';
import {
getLocalStorageItem,
setLocalStorageItem,
} from '../app/functions/localStorage';
import {
clearContactRaceRetryTimers,
CONTACTS_TRANSACTION_UPDATE_NAME,
@@ -48,9 +43,6 @@ import { useAuthContext } from './authContext';
const GlobalContactsInfoContext = createContext(null);
const GlobalContactsMessagesContext = createContext(null);
const CONTACT_RECEIVE_OPTIONS_CACHE_KEY = 'blitzContactReceiveOptionsCache';
const CONTACT_RECEIVE_OPTION_CACHE_TTL_MS = 12 * 60 * 60 * 1000;
const normalizeContactReceiveOption = receiveOption =>
receiveOption?.toString()?.toLowerCase() === 'usd' ? 'USD' : 'BTC';
@@ -61,11 +53,6 @@ export const GlobalContactsList = ({ children }) => {
{},
);
const [contactsMessags, setContactsMessagses] = useState({});
const [contactReceiveOptions, setContactReceiveOptions] = useState({});
const [
isContactReceiveOptionCacheLoaded,
setIsContactReceiveOptionCacheLoaded,
] = useState(false);
const unsubscribeMessagesRef = useRef(null);
const isInitialLoad = useRef(true);
@@ -74,45 +61,8 @@ export const GlobalContactsList = ({ children }) => {
globalContactsInformationRef.current = globalContactsInformation;
});
const contactReceiveOptionsRef = useRef(contactReceiveOptions);
useEffect(() => {
contactReceiveOptionsRef.current = contactReceiveOptions;
});
const addedContacts = globalContactsInformation.addedContacts;
useEffect(() => {
let isMounted = true;
async function loadContactReceiveOptions() {
const savedOptions = await getLocalStorageItem(
CONTACT_RECEIVE_OPTIONS_CACHE_KEY,
);
if (!isMounted) return;
try {
const parsedOptions = savedOptions ? JSON.parse(savedOptions) : {};
setContactReceiveOptions(
parsedOptions && typeof parsedOptions === 'object'
? parsedOptions
: {},
);
} catch (error) {
console.warn('Failed to parse contact receive options cache.', error);
setContactReceiveOptions({});
} finally {
setIsContactReceiveOptionCacheLoaded(true);
}
}
loadContactReceiveOptions();
return () => {
isMounted = false;
};
}, []);
const toggleGlobalContactsInformation = useCallback(
(newData, writeToDB) => {
setGlobalContactsInformation(prev => {
@@ -130,102 +80,24 @@ export const GlobalContactsList = ({ children }) => {
[publicKey],
);
const updateCachedContactReceiveOption = useCallback(
(contactUUID, receiveOption) => {
if (!contactUUID) return null;
const getContactReceiveOption = useCallback(async contactUUID => {
if (!contactUUID) return { receiveOption: null, contactDoc: null };
const normalizedReceiveOption =
normalizeContactReceiveOption(receiveOption);
const nextCacheEntry = {
receiveOption: normalizedReceiveOption,
dateChanged: Date.now(),
};
const contactDoc = await getDataFromCollection(
'blitzWalletUsers',
contactUUID,
);
setContactReceiveOptions(prev => {
const nextOptions = {
...prev,
[contactUUID]: nextCacheEntry,
};
setLocalStorageItem(
CONTACT_RECEIVE_OPTIONS_CACHE_KEY,
JSON.stringify(nextOptions),
);
return nextOptions;
});
return nextCacheEntry;
},
[],
);
const getCachedContactReceiveOption = useCallback(contactUUID => {
if (!contactUUID) return null;
const cachedOption = contactReceiveOptionsRef.current[contactUUID];
const cachedDateChanged = Number(cachedOption?.dateChanged);
if (!cachedOption?.receiveOption || !cachedDateChanged) return null;
if (Date.now() - cachedDateChanged > CONTACT_RECEIVE_OPTION_CACHE_TTL_MS) {
return null;
}
if (!contactDoc) return { receiveOption: null, contactDoc: null };
return {
receiveOption: normalizeContactReceiveOption(cachedOption.receiveOption),
dateChanged: cachedDateChanged,
receiveOption: normalizeContactReceiveOption(
contactDoc.lnurlReceiveCurrency,
),
contactDoc,
};
}, []);
const getContactReceiveOption = useCallback(
async contactUUID => {
if (!contactUUID) {
return {
receiveOption: null,
dateChanged: null,
contactDoc: null,
fromCache: false,
};
}
const cachedOption = getCachedContactReceiveOption(contactUUID);
if (cachedOption) {
return {
...cachedOption,
contactDoc: null,
fromCache: true,
};
}
const contactDoc = await getDataFromCollection(
'blitzWalletUsers',
contactUUID,
);
if (!contactDoc) {
return {
receiveOption: null,
dateChanged: null,
contactDoc: null,
fromCache: false,
};
}
const updatedOption = updateCachedContactReceiveOption(
contactUUID,
contactDoc.lnurlReceiveCurrency,
);
return {
...updatedOption,
contactDoc,
fromCache: false,
};
},
[getCachedContactReceiveOption, updateCachedContactReceiveOption],
);
const decodedAddedContacts = useMemo(() => {
if (!publicKey || !addedContacts) return [];
@@ -768,24 +640,16 @@ export const GlobalContactsList = ({ children }) => {
() => ({
decodedAddedContacts,
globalContactsInformation,
contactReceiveOptions,
toggleGlobalContactsInformation,
getContactReceiveOption,
getCachedContactReceiveOption,
updateCachedContactReceiveOption,
isContactReceiveOptionCacheLoaded,
deleteContact,
addContact,
}),
[
decodedAddedContacts,
globalContactsInformation,
contactReceiveOptions,
toggleGlobalContactsInformation,
getContactReceiveOption,
getCachedContactReceiveOption,
updateCachedContactReceiveOption,
isContactReceiveOptionCacheLoaded,
deleteContact,
addContact,
],