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

ui/ServiceLink: convert to typescript #2853

Merged
merged 3 commits into from
Mar 14, 2023

Conversation

NickHackman
Copy link
Contributor

  • Identified the issue which this PR solves.
  • Read the CONTRIBUTING document.
  • Code builds clean without any errors or warnings.
  • Added appropriate tests for any new functionality.
  • All new and existing tests passed.
  • Added comments in the code, where necessary.
  • Ran make check to catch common errors. Fixed any that came up.

Description:
Part of an ongoing effort to convert existing javascript files to typescript.

Which issue(s) this PR fixes:
#2318

Comment on lines 5 to 8
export const ServiceLink = (
service: Service | null | undefined,
): JSX.Element => {
return <AppLink to={`/services/${service?.id}`}>{service?.name}</AppLink>
Copy link
Contributor Author

@NickHackman NickHackman Mar 10, 2023

Choose a reason for hiding this comment

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

Service is nullable and potentially undefined in its use in AlertDevice

{ServiceLink(alert.service)}

Handling the nullability/undefined here results in potentially invalid or dead links. Is there a preferred method here? Could always conditionally render the AppLink 🤔

EDIT: I meant conditionally render the Grid it contains entirely

<Grid item xs={12}>
<Typography variant='body1'>
{ServiceLink(alert.service)}
</Typography>
</Grid>

The JavaScript variant didn't handle these, so it's likely this never happened, but could happen.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I think the ServiceLink component should require it to be non-null as it's impossible to render a link to a service without its info. The JS version would have just thrown an error and broken the page, so the type info should reflect its requirement.

The call site rendering has a potential bug; conditionally rendering it is fine.

That said, I think having it undefined and throwing an error would have broken the page, so it's likely the data is guaranteed by some previous loading check anyhow. If that is the case, there might be a way to have the type information reflect that it's not nullable in practice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Went up to the Page level and found the assertion being made on alert being present

if (!data.alert) return <ObjectNotFound type='alert' />

I couldn't find an assertion on service being present, the nullability/undefined is being handled

const ep = props.data?.service?.escalationPolicy

const isMaintMode = Boolean(props.data?.service?.maintenanceExpiresAt)

serviceID={props.data?.service?.id ?? ''}

From here, conditionally rendering makes the most sense to me 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Conditional rendering done in 42dd136

@NickHackman NickHackman marked this pull request as ready for review March 10, 2023 17:20
The props are destructured and renamed to alert. Cleaner to use that instead of referencing the data directly.
In the AlertDetails component ServiceLink is now conditionally rendered to prevent a situation where the service is null/undefined and rendering a link to the service.
serviceID={props.data?.service?.id ?? ''}
serviceID={alert?.service?.id ?? ''}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Super minor refactor: above the props are destructured and renamed to alert

{ServiceLink(alert.service)}
</Typography>
</Grid>
{alert.service && (
Copy link
Contributor Author

Choose a reason for hiding this comment

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

From my understanding of JS, the && should be fine here since Objects are never falsy

docs: https://developer.mozilla.org/en-US/docs/Glossary/Falsy

this should avoid the common gotcha with && and conditional rendering. Since undefined and null won't render and alert.service is always truthy

https://reactjs.org/docs/conditional-rendering.html#inline-if-with-logical--operator

@KatieMSB KatieMSB merged commit 86ce656 into target:master Mar 14, 2023
@NickHackman NickHackman deleted the ui-ServiceLink-to-tsx branch March 15, 2023 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants