Skip to content

feat: refactor UniversalLink component using typescript #6826

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

tomschall
Copy link
Contributor

  • Refactor the UniversalLink component using typescript.
  • Modify lint-staged.config.js to exclude d.ts files
  • Modify tsconfig, include types there, otherwise d.ts files will noch be recognized in .tsx files, e.g. inside components
  • Use newest version of classnames

Copy link

netlify bot commented Mar 11, 2025

Deploy Preview for plone-components canceled.

Name Link
🔨 Latest commit 8a16a26
🔍 Latest deploy log https://app.netlify.com/sites/plone-components/deploys/67dd79d9e5d985000901a053

@tomschall tomschall requested a review from sneridagh March 11, 2025 11:06
@tomschall tomschall self-assigned this Mar 11, 2025
Copy link
Member

@sneridagh sneridagh left a comment

Choose a reason for hiding this comment

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

Overall LGTM, just small adjustments and questions.

@plone/volto-team Please can I have another review?

};
}

const UniversalLink = React.memo<UniversalLinkProps>(
Copy link
Member

Choose a reason for hiding this comment

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

What is the reasoning behind the React.memo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a performance optimization.

The component gets memoized and if we have for example 50 links in a list and only one changes, with React.memo only the changed link get rendered new.

We can use it here because UniversalLink changes only over props, we have no local states or side effects.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made some tests and negative tests to show this optimized behavior and also show when it's not working (unstable props). We always have to use stable references if we want the optimized behavior, that means not passing inline objects to props or inline jsx to children.

I made some detailed examples in the tests, maybe we should also document this.

@sneridagh sneridagh requested a review from a team March 20, 2025 08:16
@pnicolli
Copy link
Contributor

Love the work on converting this very important component to TS, thanks!

@sneridagh I will definitely review this over the weekend 👍

@tomschall tomschall requested a review from sneridagh March 21, 2025 15:16
* main: (59 commits)
  Update or remove broken links (#6953)
  Only accept images in ImageInput component (#6926)
  Bump vite from 5.4.15 to 5.4.16 (#6932)
  Fetch vocabularies and querystring config in the current context (#6935)
  Remove the build from netlify for @plone/components
  Complete missing german translations and Storybook story for 'ContentsDeleteModal' (#6881)
  Remove duplicate configuration item `html_title` (#6933)
  Forwardport hand-cherrypicking the changes happened in Seven for `@plone/registry` relevant for be reintegrated in alpha (`main`). (#6929)
  Fixed regression on brown bag minor version of `@storybook/addon-webpack5-compiler-babel` (#6930)
  Amend PR #6885 changelog to be breaking
  s/Seven/Volto on `main` (#6925)
  Release @plone/registry 3.0.0-alpha.1
  Release @plone/types 2.0.0-alpha.0
  Remove news from packages that won't be released from main
  Fixed merge public folder from addons regression (#6919)
  Fixed handling of errors that are not coming from the backend (#6903)
  Move _hardware-requirements.md include from documentation to volto (#6918)
  Move includes from `plone/documentation` to `plone/volto` (#6917)
  Fix typo in Appextras documentation (#6083)
  Fix gray placeholder on horizontal scroll (#6877)
  ...
@sneridagh
Copy link
Member

@pnicolli I rebased this one. Could you please take a final look at it?

Copy link
Member

@sneridagh sneridagh left a comment

Choose a reason for hiding this comment

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

LGTM!

locale: string;
};
userSession: {
token: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
token: string;
token: string | null;

The default value for token is null. See https://github.com/plone/volto/blob/main/packages/volto/src/reducers/userSession/userSession.js#L14

(state) => state.userSession?.token,
);

function getUrl(props: UniversalLinkProps, token: string): string {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm fine with turning the previous code into a function, if we go this way I would extract the function from the component completely so it's also easily testable. This is not mandatory but would be nice imho.

Comment on lines 7584 to 7585
[email protected]:
resolution: {integrity: sha512-JR/iSQOSt+LQIWwrwEzJ9uk0xfN3mTVYMwt1Ir5mUcSN6pU+V4zQFFaJsclJbPuAUQH+yfWef6tm7l1quW3C8Q==}
Copy link
Contributor

Choose a reason for hiding this comment

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

I would strongly suggest installing classnames 2.5.1 in @plone/volto-slate as well in order to dedupe the dependency

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