Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Improve get icons #46886

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1887,21 +1887,37 @@ function getIconsForParticipants(participants: number[], personalDetails: OnyxIn
return avatars;
}

/**
* Cache the workspace icons
*/
const workSpaceIconsCache = new Map<string, {name: string; icon: Icon}>();

/**
* Given a report, return the associated workspace icon.
*/
function getWorkspaceIcon(report: OnyxInputOrEntry<Report>, policy?: OnyxInputOrEntry<Policy>): Icon {
const workspaceName = getPolicyName(report, false, policy);
const policyExpenseChatAvatarSource = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`]?.avatarURL
? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`]?.avatarURL
: getDefaultWorkspaceAvatar(workspaceName);
const cacheKey = report?.policyID ?? workspaceName;
const iconFromCache = workSpaceIconsCache.get(cacheKey);
const avatarURL = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`]?.avatarURL;

const isSameAvatarURL = iconFromCache?.icon?.source === avatarURL;
const isDefaultWorkspaceAvatar = !avatarURL && typeof iconFromCache?.icon?.source !== 'string';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB: It might be more reliable and simple to set a flag in the cached value, e.g. isDefaultWorkspaceAvatar. Maybe in the future there's a case where the source is not a string and it's not a default workspace avatar.

const hasWorkSpaceNameChanged = iconFromCache?.name !== workspaceName;
if (iconFromCache && (isSameAvatarURL || isDefaultWorkspaceAvatar) && !hasWorkSpaceNameChanged) {
return iconFromCache.icon;
}
// `avatarURL` can be an empty string, so we have to use || operator here
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const policyExpenseChatAvatarSource = avatarURL || getDefaultWorkspaceAvatar(workspaceName);

const workspaceIcon: Icon = {
source: policyExpenseChatAvatarSource ?? '',
type: CONST.ICON_TYPE_WORKSPACE,
name: workspaceName,
id: report?.policyID,
};
workSpaceIconsCache.set(cacheKey, {name: workspaceName, icon: workspaceIcon});
return workspaceIcon;
}

Expand Down
Loading