add session cache to reduce calls for already fetched contacts

This commit is contained in:
Blake Kaufman
2026-07-04 09:12:23 -04:00
parent 28d556777f
commit 8845f6879d
@@ -76,6 +76,7 @@ export function AddContactContent({
const navigate = useNavigation();
const { refreshCacheObject } = useImageCache();
const searchTrackerRef = useRef(null);
const searchCacheRef = useRef(new Map()); // normalizedTerm -> resolved users[]
const didClickCamera = useRef(null);
const { t } = useTranslation();
const isFocused = useRef(false);
@@ -100,6 +101,8 @@ export function AddContactContent({
return requestUUID;
};
const getSearchCacheKey = term => term.replace(/@/g, '').trim().toLowerCase();
const handleTextInputBlur = () => {
if (setIsKeyboardActive) setIsKeyboardActive(false);
if (!searchInput && !didClickCamera.current) {
@@ -146,6 +149,7 @@ export function AddContactContent({
).filter(Boolean);
refreshCacheObject();
searchCacheRef.current.set(getSearchCacheKey(term), newUsers);
setIsSearching(false);
setUsers(newUsers);
} else {
@@ -176,6 +180,14 @@ export function AddContactContent({
return;
}
const cacheKey = getSearchCacheKey(term);
if (cacheKey && searchCacheRef.current.has(cacheKey)) {
searchTrackerRef.current = null; // cancel any in-flight/pending search
setUsers(searchCacheRef.current.get(cacheKey));
setIsSearching(false);
return;
}
if (term.length > 0) {
const requestUUID = handleSearchTrackerRef();
setIsSearching(true);