From 21ba5a214b3aaf05a1b1f042321fb94aab363656 Mon Sep 17 00:00:00 2001 From: ShatilKhan Date: Sat, 16 Dec 2023 01:41:34 +0600 Subject: [PATCH 1/2] icon computation without mutable variables Signed-off-by: ShatilKhan --- src/components/icon/icon.tsx | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/components/icon/icon.tsx b/src/components/icon/icon.tsx index 45a09d3078..7d636c8a4a 100644 --- a/src/components/icon/icon.tsx +++ b/src/components/icon/icon.tsx @@ -625,21 +625,27 @@ export class OuiIcon extends PureComponent { let initialIcon; let isLoading = false; - // Category 1: cached oui icons + // Define helper function + const getIconAndLoadingStatus = (type: any) => { + // Category 1: cached oui icons if (isCachedIcon(type)) { - initialIcon = iconComponentCache[type as string]; + return { icon: iconComponentCache[type as string], isLoading: false }; // Category 2: URL (relative, absolute) } else if (isUrl(type)) { - initialIcon = type; + return { icon: type, isLoading: false }; // Category 3: non-cached oui icon or new icon } else if (typeof type === 'string') { - isLoading = true; this.loadIconComponent(type as OuiIconType); + return { icon: undefined, isLoading: true }; } else { // Category 4: custom icon component - initialIcon = type; this.onIconLoad(); + return { icon: type, isLoading: false }; } + }; + + // Use the helper function + const { icon: initialIcon, isLoading } = getIconAndLoadingStatus(type); this.state = { icon: initialIcon, From 330dc5c8e114eb9d33d2fadfc0510a89cbd94d23 Mon Sep 17 00:00:00 2001 From: ShatilKhan Date: Sat, 16 Dec 2023 12:05:15 +0600 Subject: [PATCH 2/2] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9707fcfe2f..c7f76c56c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ - Add `crossInCircleEmpty` and `power` icons ([#1044](https://github.com/opensearch-project/oui/pull/1044)) - Add `Figma` icon and link to OUI Figma resources ([#1064](https://github.com/opensearch-project/oui/pull/1064)) - Implement validation for icon input source & set default icon to `Beaker` ([#1137](https://github.com/opensearch-project/oui/pull/1137)) +- Rewrite `InitialIcon` computation without mutable variables ([#1186](https://github.com/opensearch-project/oui/pull/1186)) ### 🐛 Bug Fixes