@@ -1917,21 +1917,37 @@ function getIconsForParticipants(participants: number[], personalDetails: OnyxIn
1917
1917
return avatars ;
1918
1918
}
1919
1919
1920
+ /**
1921
+ * Cache the workspace icons
1922
+ */
1923
+ const workSpaceIconsCache = new Map < string , { name : string ; icon : Icon } > ( ) ;
1924
+
1920
1925
/**
1921
1926
* Given a report, return the associated workspace icon.
1922
1927
*/
1923
1928
function getWorkspaceIcon ( report : OnyxInputOrEntry < Report > , policy ?: OnyxInputOrEntry < Policy > ) : Icon {
1924
1929
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 ) ;
1928
1943
1929
1944
const workspaceIcon : Icon = {
1930
1945
source : policyExpenseChatAvatarSource ?? '' ,
1931
1946
type : CONST . ICON_TYPE_WORKSPACE ,
1932
1947
name : workspaceName ,
1933
1948
id : report ?. policyID ,
1934
1949
} ;
1950
+ workSpaceIconsCache . set ( cacheKey , { name : workspaceName , icon : workspaceIcon } ) ;
1935
1951
return workspaceIcon ;
1936
1952
}
1937
1953
0 commit comments