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
Changes from 1 commit
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
16 changes: 11 additions & 5 deletions src/components/icon/icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -625,28 +625,34 @@
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 };

Check failure on line 632 in src/components/icon/icon.tsx

View workflow job for this annotation

GitHub Actions / Lint and Test on ubuntu-latest

Cannot redeclare block-scoped variable 'initialIcon'.
// Category 2: URL (relative, absolute)

Check failure on line 633 in src/components/icon/icon.tsx

View workflow job for this annotation

GitHub Actions / Lint and Test on ubuntu-latest

Cannot redeclare block-scoped variable 'isLoading'.
} 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,
iconTitle: undefined,
isLoading,
neededLoading: isLoading,
};

Check failure on line 655 in src/components/icon/icon.tsx

View workflow job for this annotation

GitHub Actions / Lint and Test on ubuntu-latest

Cannot redeclare block-scoped variable 'initialIcon'.

Check failure on line 655 in src/components/icon/icon.tsx

View workflow job for this annotation

GitHub Actions / Lint and Test on ubuntu-latest

Cannot redeclare block-scoped variable 'isLoading'.
}

componentDidUpdate(prevProps: OuiIconProps) {
Expand Down
Loading