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

Rewrite InitialIcon computation without mutable variables #1186

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
- Add `Figma` icon and link to OUI Figma resources ([#1064](https://github.com/opensearch-project/oui/pull/1064))
- Rename `crossInACircleFilled` asset files and ensure backward compatibility ([#1113](https://github.com/opensearch-project/oui/pull/1113))
- 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))
- Add `Docking` icons ([#1041](https://github.com/opensearch-project/oui/pull/1041))


### 🐛 Bug Fixes

- Add exit code to compile-scss script on failure ([#1024](https://github.com/opensearch-project/oui/pull/1024))
Expand Down
16 changes: 11 additions & 5 deletions src/components/icon/icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -632,21 +632,27 @@ export class OuiIcon extends PureComponent<OuiIconProps, State> {
let initialIcon;
let isLoading = false;
Comment on lines 632 to 633
Copy link
Member

Choose a reason for hiding this comment

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

These should be removed now, right?


// Category 1: cached oui icons
// Define helper function
const getIconAndLoadingStatus = (type: any) => {
Copy link
Member

Choose a reason for hiding this comment

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

I think we should already have a type for type that's more narrow?

// 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,
Expand Down