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: Improve Settings component #434

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 12 additions & 10 deletions src/components/Voyager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,23 +191,25 @@ export default function Voyager(props: VoyagerProps) {
}

function renderGraphViewport() {
const showSettings = !hideSettings && typeGraph != null;

return (
<Box
sx={(theme) => ({
flex: 1,
position: 'relative',
display: 'inline-block',
width: '100%',
height: '100%',
maxHeight: '100%',

[theme.breakpoints.down('md')]: {
height: '50%',
maxWidth: 'none',
position: "relative",
display: "inline-block",
width: "100%",
height: "100%",
maxHeight: "100%",

[theme.breakpoints.down("md")]: {
height: "50%",
maxWidth: "none",
},
})}
>
{!hideSettings && (
{showSettings && (
<Settings
options={displayOptions}
typeGraph={typeGraph}
Expand Down
78 changes: 40 additions & 38 deletions src/components/settings/Settings.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ChangeEvent, Fragment, useMemo } from "react";
import Checkbox from '@mui/material/Checkbox';
import Stack from '@mui/material/Stack';

Expand All @@ -6,16 +7,41 @@ import { VoyagerDisplayOptions } from '../Voyager';
import RootSelector from './RootSelector';

interface SettingsProps {
typeGraph: TypeGraph | null;
typeGraph: TypeGraph;
options: VoyagerDisplayOptions;
onChange: (options: VoyagerDisplayOptions) => void;
}

export default function Settings(props: SettingsProps) {
const { typeGraph, options, onChange } = props;
if (typeGraph == null) {
return null;
}
export default function Settings({ typeGraph, options, onChange }: SettingsProps) {
const checkboxes = useMemo(
() => [
{
id: "sort",
label: "Sort by Alphabet",
checked: options.sortByAlphabet ?? false,
onChange: (e: ChangeEvent<HTMLInputElement>) => onChange({ sortByAlphabet: e.target.checked }),
},
{
id: "skip",
label: "Skip Relay",
checked: options.skipRelay ?? false,
onChange: (e: ChangeEvent<HTMLInputElement>) => onChange({ skipRelay: e.target.checked }),
},
{
id: "deprecated",
label: "Skip deprecated",
checked: options.skipDeprecated ?? false,
onChange: (e: ChangeEvent<HTMLInputElement>) => onChange({ skipDeprecated: e.target.checked }),
},
{
id: "showLeafFields",
label: "Show leaf fields",
checked: options.showLeafFields ?? false,
onChange: (e: ChangeEvent<HTMLInputElement>) => onChange({ showLeafFields: e.target.checked }),
},
],
[options, onChange]
);
Comment on lines +16 to +44
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider optimizing checkbox onChange handlers.

While the useMemo implementation is good, each checkbox creates its own onChange handler. Consider refactoring to use a single shared handler with a parameter for better performance.

 const checkboxes = useMemo(
   () => [
     {
       id: "sort",
       label: "Sort by Alphabet",
       checked: options.sortByAlphabet ?? false,
-      onChange: (e: ChangeEvent<HTMLInputElement>) => onChange({ sortByAlphabet: e.target.checked }),
+      key: 'sortByAlphabet',
     },
     {
       id: "skip",
       label: "Skip Relay",
       checked: options.skipRelay ?? false,
-      onChange: (e: ChangeEvent<HTMLInputElement>) => onChange({ skipRelay: e.target.checked }),
+      key: 'skipRelay',
     },
     // ... other checkboxes
   ],
   [options, onChange]
 );

+ const handleCheckboxChange = useCallback((key: string) => 
+   (e: ChangeEvent<HTMLInputElement>) => onChange({ [key]: e.target.checked }),
+   [onChange]
+ );

Then update the render:

- {checkboxes.map(({ id, checked, label, onChange }) => (
+ {checkboxes.map(({ id, checked, label, key }) => (
   <Fragment key={id}>
-    <Checkbox id={id} checked={checked} onChange={onChange} />
+    <Checkbox id={id} checked={checked} onChange={handleCheckboxChange(key)} />
     <label htmlFor={id}>{label}</label>
   </Fragment>
 ))}

Committable suggestion was skipped due to low confidence.


return (
<Stack
Expand All @@ -29,7 +55,6 @@ export default function Settings(props: SettingsProps) {
borderColor: theme.palette.shadowColor.main,
boxShadow: 2,
padding: 1,
// left-bottom corner
left: 0,
bottom: 0,
})}
Expand All @@ -38,37 +63,14 @@ export default function Settings(props: SettingsProps) {
typeGraph={typeGraph}
onChange={(rootType) => onChange({ rootType })}
/>
<Stack direction="row" className="setting-other-options">
<Checkbox
id="sort"
checked={!!options.sortByAlphabet}
onChange={(event) =>
onChange({ sortByAlphabet: event.target.checked })
}
/>
<label htmlFor="sort">Sort by Alphabet</label>
<Checkbox
id="skip"
checked={!!options.skipRelay}
onChange={(event) => onChange({ skipRelay: event.target.checked })}
/>
<label htmlFor="skip">Skip Relay</label>
<Checkbox
id="deprecated"
checked={!!options.skipDeprecated}
onChange={(event) =>
onChange({ skipDeprecated: event.target.checked })
}
/>
<label htmlFor="deprecated">Skip deprecated</label>
<Checkbox
id="showLeafFields"
checked={!!options.showLeafFields}
onChange={(event) =>
onChange({ showLeafFields: event.target.checked })
}
/>
<label htmlFor="showLeafFields">Show leaf fields</label>

<Stack className="setting-other-options" direction="row">
{checkboxes.map(({ id, checked, label, onChange }) => (
<Fragment key={id}>
<Checkbox id={id} checked={checked} onChange={onChange} />
<label htmlFor={id}>{label}</label>
</Fragment>
))}
</Stack>
</Stack>
);
Expand Down