Skip to content

Commit 0fd97c0

Browse files
Merge pull request #46886 from callstack-internal/hur/perf/improve-get-icons
perf: Improve get icons
2 parents 0bdfe29 + 1365d10 commit 0fd97c0

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/libs/ReportUtils.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,21 +1917,37 @@ function getIconsForParticipants(participants: number[], personalDetails: OnyxIn
19171917
return avatars;
19181918
}
19191919

1920+
/**
1921+
* Cache the workspace icons
1922+
*/
1923+
const workSpaceIconsCache = new Map<string, {name: string; icon: Icon}>();
1924+
19201925
/**
19211926
* Given a report, return the associated workspace icon.
19221927
*/
19231928
function getWorkspaceIcon(report: OnyxInputOrEntry<Report>, policy?: OnyxInputOrEntry<Policy>): Icon {
19241929
const workspaceName = getPolicyName(report, false, policy);
1925-
const policyExpenseChatAvatarSource = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`]?.avatarURL
1926-
? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`]?.avatarURL
1927-
: getDefaultWorkspaceAvatar(workspaceName);
1930+
const cacheKey = report?.policyID ?? workspaceName;
1931+
const iconFromCache = workSpaceIconsCache.get(cacheKey);
1932+
const avatarURL = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`]?.avatarURL;
1933+
1934+
const isSameAvatarURL = iconFromCache?.icon?.source === avatarURL;
1935+
const isDefaultWorkspaceAvatar = !avatarURL && typeof iconFromCache?.icon?.source !== 'string';
1936+
const hasWorkSpaceNameChanged = iconFromCache?.name !== workspaceName;
1937+
if (iconFromCache && (isSameAvatarURL || isDefaultWorkspaceAvatar) && !hasWorkSpaceNameChanged) {
1938+
return iconFromCache.icon;
1939+
}
1940+
// `avatarURL` can be an empty string, so we have to use || operator here
1941+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
1942+
const policyExpenseChatAvatarSource = avatarURL || getDefaultWorkspaceAvatar(workspaceName);
19281943

19291944
const workspaceIcon: Icon = {
19301945
source: policyExpenseChatAvatarSource ?? '',
19311946
type: CONST.ICON_TYPE_WORKSPACE,
19321947
name: workspaceName,
19331948
id: report?.policyID,
19341949
};
1950+
workSpaceIconsCache.set(cacheKey, {name: workspaceName, icon: workspaceIcon});
19351951
return workspaceIcon;
19361952
}
19371953

0 commit comments

Comments
 (0)