Skip to content

refactor HelpContainer#1598

Draft
walesch-yan wants to merge 1 commit into
developfrom
yw-refactor-HelpContainer
Draft

refactor HelpContainer#1598
walesch-yan wants to merge 1 commit into
developfrom
yw-refactor-HelpContainer

Conversation

@walesch-yan

Copy link
Copy Markdown
Collaborator

This PR refactors the HelpContainer into a functional component following similar approaches as for previous PRs.

Comment thread ui/src/containers/HelpContainer.jsx
Comment thread ui/src/containers/HelpContainer.jsx
Comment thread ui/src/containers/HelpContainer.jsx Outdated
Comment on lines +52 to +58
const links = import.meta.env.VITE_HELP_LINKS?.map((link) => (
<div key={link.url}>
<a target="_blank" href={link.url} rel="noreferrer">
{link.name}
</a>
</div>
));

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Unfortunately the HELP_LINKS were not available in the demo, hence I used the url for the key prop here, it would be best to have an id I guess.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think this VITE_HELP_LINKS feature is broken since env vars are strings but this one is used as an array in the code. I would consider removing this feature.

For the sake of discussion, storing JSX in a variable should also be avoided (for the same reason that it doesn't reduce the complexity of the component). I would have probably inlined the JSX and maybe stored and parsed the env var in a constant at the top of the file:

cost HELP_LINKS = import.meta.env.VITE_HELP_LINKS; // + parse as array somehow

...
// In the JSX:
{HELP_LINKS.length > 0 && (
  <Card>
    {HELP_LINKS..map(...)}
  </Card>
)}

@walesch-yan
walesch-yan force-pushed the yw-refactor-HelpContainer branch from 15e5587 to 6e1bf77 Compare March 19, 2025 12:51
@marcus-oscarsson

Copy link
Copy Markdown
Member

Nice :)

@axelboc axelboc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice! FYI, I think we considered removing this page at some point, which is why I hadn't touched it yet. Worth making a decision on this before addressing my comments (especially the React Hook Form one 😁 — although it could be good practice if you're interested, since it's such a simple form)

Comment thread ui/src/containers/HelpContainer.jsx Outdated
</Card.Body>
</Card>
);
const handleSubmit = () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I like to use the function declaration syntax for named functions: function handleSubmit() { ... }. I find it helps to distinguish functions from variables/constants when reading the code.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Sure, I guess this was just my personal preference but for the sake of consistency I will change it (and try to not let my inner demon do that again 😆 )

Comment thread ui/src/containers/HelpContainer.jsx Outdated
<Row>
<Col sm={12} className="d-flex">
<Col sm={4}>
{localContactPanel()}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It's best to avoid "render" functions like this. It doesn't actually remove complexity from the component — on the contrary, it adds a function.

Either inline the JSX or move it into a separate component. In this case, I think inlining is fine — long JSX is okay if the logic (conditions, loops, etc.) is limited.

Comment thread ui/src/containers/HelpContainer.jsx Outdated
Comment on lines +52 to +58
const links = import.meta.env.VITE_HELP_LINKS?.map((link) => (
<div key={link.url}>
<a target="_blank" href={link.url} rel="noreferrer">
{link.name}
</a>
</div>
));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think this VITE_HELP_LINKS feature is broken since env vars are strings but this one is used as an array in the code. I would consider removing this feature.

For the sake of discussion, storing JSX in a variable should also be avoided (for the same reason that it doesn't reduce the complexity of the component). I would have probably inlined the JSX and maybe stored and parsed the env var in a constant at the top of the file:

cost HELP_LINKS = import.meta.env.VITE_HELP_LINKS; // + parse as array somehow

...
// In the JSX:
{HELP_LINKS.length > 0 && (
  <Card>
    {HELP_LINKS..map(...)}
  </Card>
)}

Comment thread ui/src/containers/HelpContainer.jsx Outdated
</Col>
<Col className="col-xs-4">
<span>
<b>Mesh </b> <br />

@axelboc axelboc Mar 19, 2025

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could you please replace the b and br tags with h3 and remove the span wrappers? Might as well improve the HTML a bit... 😂

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Well spotted! Maybe I went a little to fast over this component

Comment thread ui/src/containers/HelpContainer.jsx Outdated
Comment on lines +12 to +13
const senderRef = React.useRef();
const contentRef = React.useRef();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Perhaps a good opportunity to try React Hook Form? It's already installed — I used it for the login form and for the manual sample creation form, if I recall.

@walesch-yan

Copy link
Copy Markdown
Collaborator Author

Nice! FYI, I think we considered removing this page at some point, which is why I hadn't touched it yet. Worth making a decision on this before addressing my comments (especially the React Hook Form one 😁 — although it could be good practice if you're interested, since it's such a simple form)

Oh, maybe I missed/forgot this consideration 😅 I guess we can keep it as a draft then, I prefer to use it as a little practice if possible;)

@walesch-yan
walesch-yan marked this pull request as draft March 19, 2025 14:16
@walesch-yan
walesch-yan force-pushed the yw-refactor-HelpContainer branch from 6e1bf77 to 60e34ce Compare March 24, 2025 16:59

@walesch-yan walesch-yan left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I added the suggestions and integrated the react-hook-form for this component:)

Comment on lines +126 to +146
<Alert
className="mt-3"
variant={feedback.variant}
onClose={() => setFeedback({})}
dismissible
>
{feedback.message}
</Alert>

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Added an alert as feedback for the user if the form was submitted correctly or if it failed

Comment thread ui/src/containers/HelpContainer.jsx
Comment thread ui/src/containers/HelpContainer.jsx Outdated
Comment on lines +37 to +40
setFeedback({
variant: 'success',
message: 'Feedback sent successfully',
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The form state has an isSubmitSuccessful flag that tells you if the submit was successful (i.e. no error thrown in handleSubmit). And if sendFeedback throws an error, you can catch it and call the form's setError function as explained here: https://react-hook-form.com/docs/useform/seterror

This means that you don't need the feedback state and can just declare different alerts in the JSX based on isSubmitSuccessful and errors.

Comment thread ui/src/containers/HelpContainer.jsx Outdated
variant: 'success',
message: 'Feedback sent successfully',
});
reset(DEFAULT_HELPFORM_VALUES);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This may reset isSubmitSuccessful and errors. I think there's an option that controls this: https://react-hook-form.com/docs/useform#resetOptions

@walesch-yan
walesch-yan force-pushed the yw-refactor-HelpContainer branch from 60e34ce to 2f1f635 Compare March 25, 2025 13:08
Comment on lines +50 to +53
useEffect(() => {
if (isSubmitSuccessful) {
reset();
setShowSuccess(true);
}
}, [isSubmitSuccessful, reset]);

@walesch-yan walesch-yan Mar 25, 2025

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

According to the docs the recommended way of using reset is inside of useEffect using isSubmitSuccessful as trigger, I wrapped it inside the if, since reset will change the value of isSubmitSuccessful to false.

As one can see I also kept the useState here since there is no option to keep isSubmitSuccessful between resets but only isSubmitted (which becomes also true if the execution of submit fails). One could use resetField but it would go against the idea of sending multiple forms. Hence I decided to keep a local state variable.

@axelboc axelboc Mar 25, 2025

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Makes sense, in that case, I would move setShowSuccess(false) (currently in handleSubmit) into a separate use effect as well to hide the success message when the user starts filling in the form again:

  useEffect(() => {
    if (isDirty) {
      // Hide success message if user starts filling in the form again
      setShowSuccess(false);
    }
  }, [isDirty]);

But it's probably overly complicating things... Forms suck.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Well in terms of UX your approach is probably better, thanks for all the suggestions!

Forms suck.

Couldn't agree more 😅

@walesch-yan
walesch-yan force-pushed the yw-refactor-HelpContainer branch 2 times, most recently from 2f02f43 to 88a95a0 Compare March 25, 2025 14:15
@walesch-yan
walesch-yan force-pushed the yw-refactor-HelpContainer branch from 88a95a0 to 2802960 Compare March 25, 2025 14:40
@marcus-oscarsson

Copy link
Copy Markdown
Member

Hm, this one has been here for some time now. What would you like to do with it ?

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