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

refactor: ts checking conditionallyrender props #7840

Merged
merged 4 commits into from
Aug 30, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,18 @@ export const GroupCard = ({
</Tooltip>
))}
elseShow={
<Tooltip
title='This group is not used in any project'
arrow
describeChild
>
<ConditionallyRender
condition={!group.rootRole}
show={<Badge>Not used</Badge>}
/>
</Tooltip>
<ConditionallyRender
condition={!group.rootRole}
show={
<Tooltip
title='This group is not used in any project'
arrow
describeChild
>
<Badge>Not used</Badge>
</Tooltip>
}
/>
}
/>
</ProjectBadgeContainer>
Expand Down
16 changes: 9 additions & 7 deletions frontend/src/component/common/Badge/Badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,17 @@ const StyledBadgeIcon = styled('span')<
: theme.palette[color].main,
}));

const BadgeIcon = (color: Color, icon: ReactElement) => (
const BadgeIcon = (color: Color, icon?: ReactElement) => (
<StyledBadgeIcon color={color}>
<ConditionallyRender
condition={Boolean(icon?.props.sx)}
show={icon}
elseShow={() =>
cloneElement(icon!, {
sx: { fontSize: '16px' },
})
elseShow={
icon
? cloneElement(icon, {
sx: { fontSize: '16px' },
})
: null
}
/>
</StyledBadgeIcon>
Expand Down Expand Up @@ -110,7 +112,7 @@ export const Badge: FC<IBadgeProps> = forwardRef(
>
<ConditionallyRender
condition={Boolean(icon) && !iconRight}
show={BadgeIcon(color, icon!)}
show={BadgeIcon(color, icon)}
/>
<ConditionallyRender
condition={
Expand All @@ -122,7 +124,7 @@ export const Badge: FC<IBadgeProps> = forwardRef(
/>
<ConditionallyRender
condition={Boolean(icon) && Boolean(iconRight)}
show={BadgeIcon(color, icon!)}
show={BadgeIcon(color, icon)}
/>
</StyledBadge>
),
Expand Down
1 change: 1 addition & 0 deletions frontend/src/component/layout/Error/Error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const Error: VFC<IErrorProps> = ({ error }) => {
<ConditionallyRender
condition={showZendeskButton}
show={<ZendeskButton />}
elseShow={undefined}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's more idiomatic to return null than undefined in react. I checked the code and the ConditionallyRender swaps undefined to null under the hood but in the spirit of "don't make me think" I'd go for null here.

Copy link
Contributor

@kwasniew kwasniew Aug 30, 2024

Choose a reason for hiding this comment

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

Same for the other places where we follow this pattern

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, but in this place prop customButton expects something || undefined

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah so it's for the next step of the migration when we do codemods. Those things are worth mentioning in the PR description since it's not obvious.

/>
}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const SegmentExecution: VFC<ISegmentExecutionProps> = ({
</span>
</SegmentResultTextWrapper>
}
elseShow={undefined}
/>
}
isExpanded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,6 @@ export const ReportCard = ({ healthReport }: IReportCardProps) => {
? 'warning.main'
: 'success.main';

const renderActiveToggles = () => (
<StyledBoxActive>
<CheckIcon />
<span>{healthReport.activeCount} active flags</span>
</StyledBoxActive>
);

const renderStaleToggles = () => (
<StyledBoxStale>
<ReportProblemOutlinedIcon />
<span>{healthReport.staleCount} stale flags</span>
</StyledBoxStale>
);

const renderPotentiallyStaleToggles = () => (
<StyledBoxStale>
<ReportProblemOutlinedIcon />
<span>
{healthReport.potentiallyStaleCount} potentially stale flags
</span>
</StyledBoxStale>
);

const StalenessInfoIcon = () => (
<HtmlTooltip
title={
Expand Down Expand Up @@ -157,7 +134,14 @@ export const ReportCard = ({ healthReport }: IReportCardProps) => {
<li>
<ConditionallyRender
condition={Boolean(healthReport.activeCount)}
show={renderActiveToggles}
show={
<StyledBoxActive>
<CheckIcon />
<span>
{healthReport.activeCount} active flags
</span>
</StyledBoxActive>
}
/>
</li>
<ConditionallyRender
Expand All @@ -172,7 +156,14 @@ export const ReportCard = ({ healthReport }: IReportCardProps) => {
<li>
<ConditionallyRender
condition={Boolean(healthReport.staleCount)}
show={renderStaleToggles}
show={
<StyledBoxStale>
<ReportProblemOutlinedIcon />
<span>
{healthReport.staleCount} stale flags
</span>
</StyledBoxStale>
}
/>
</li>
</StyledList>
Expand All @@ -190,7 +181,15 @@ export const ReportCard = ({ healthReport }: IReportCardProps) => {
condition={Boolean(
healthReport.potentiallyStaleCount,
)}
show={renderPotentiallyStaleToggles}
show={
<StyledBoxStale>
<ReportProblemOutlinedIcon />
<span>
{healthReport.potentiallyStaleCount}{' '}
potentially stale flags
</span>
</StyledBoxStale>
}
/>
</li>
</StyledList>
Expand Down
Loading