-
Notifications
You must be signed in to change notification settings - Fork 19
feat(settings): Refactor settings form into sections (resolves #215). #242
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
ade7efd
240e37b
2c94d63
4fece91
54b7928
d9e685d
b557613
ae0e945
da84c4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import React from "react"; | ||
|
|
||
| import { | ||
| FormControl, | ||
| FormHelperText, | ||
| FormLabel, | ||
| Input, | ||
| Textarea, | ||
| } from "@mui/joy"; | ||
|
|
||
|
|
||
| interface SettingsFormFieldProps { | ||
| configKey: string; | ||
| helperText: string | React.ReactNode; | ||
| initialValue: string | number; | ||
| label: string; | ||
| type: string; | ||
| } | ||
|
|
||
| /** | ||
| * Displays a form field for user input of configuration values. | ||
| * | ||
| * @param props | ||
| * @param props.configKey | ||
| * @param props.helperText | ||
| * @param props.initialValue | ||
| * @param props.label | ||
| * @param props.type | ||
| * @return | ||
| */ | ||
| const SettingsFormField = ({ | ||
| configKey, | ||
| helperText, | ||
| initialValue, | ||
| label, | ||
| type, | ||
| }: SettingsFormFieldProps) => ( | ||
| <FormControl> | ||
| <FormLabel> | ||
| {label} | ||
| </FormLabel> | ||
| {"number" === type ? | ||
| <Input | ||
| defaultValue={initialValue} | ||
| name={configKey} | ||
| type={"number"}/> : | ||
| <Textarea | ||
| defaultValue={initialValue} | ||
| name={configKey}/>} | ||
| <FormHelperText> | ||
| {helperText} | ||
| </FormHelperText> | ||
| </FormControl> | ||
| ); | ||
|
|
||
|
|
||
| export type {SettingsFormFieldProps}; | ||
| export default SettingsFormField; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| .settings-form-field-sections-group-container { | ||
| overflow-y: auto; | ||
| display: flex; | ||
| flex-direction: column; | ||
| flex-grow: 1; | ||
| gap: 0.75rem; | ||
| } | ||
|
|
||
| .settings-form-field-sections-group { | ||
| gap: 0.5rem; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import React from "react"; | ||
|
|
||
| import { | ||
| AccordionGroup, | ||
| Box, | ||
| Link, | ||
| } from "@mui/joy"; | ||
|
|
||
| import { | ||
| CONFIG_KEY, | ||
| LOCAL_STORAGE_KEY, | ||
| } from "../../../../../typings/config"; | ||
| import {getConfig} from "../../../../../utils/config"; | ||
| import {SettingsFormFieldProps} from "./SettingsFormField"; | ||
| import SettingsFormFieldsSection from "./SettingsFormFieldsSection"; | ||
| import ThemeSwitchFormField from "./ThemeSwitchFormField"; | ||
|
|
||
| import "./SettingsFormFieldSectionsGroup.css"; | ||
|
|
||
|
|
||
| /** | ||
| * Gets form fields information for user input of configuration values. | ||
| * | ||
| * @return A list of form fields information. | ||
| */ | ||
| const getConfigFormFieldSections = (): Array<{ | ||
| name: string; | ||
| fields: Array<React.ReactNode | SettingsFormFieldProps>; | ||
| }> => [ | ||
| { | ||
| name: "Common", | ||
| fields: [ | ||
| <ThemeSwitchFormField key={null}/>, | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| configKey: LOCAL_STORAGE_KEY.PAGE_SIZE, | ||
| helperText: "Number of log messages to display per page.", | ||
| initialValue: getConfig(CONFIG_KEY.PAGE_SIZE), | ||
| label: "View: Page size", | ||
| type: "number", | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: "KV-Pairs IR / JSON", | ||
| fields: [ | ||
| { | ||
| configKey: LOCAL_STORAGE_KEY.DECODER_OPTIONS_FORMAT_STRING, | ||
| helperText: ( | ||
| <span> | ||
| Format string for formatting a JSON log event as plain text. See the | ||
| {" "} | ||
| <Link | ||
| href={"https://docs.yscope.com/yscope-log-viewer/main/user-guide/format-struct-logs-overview.html"} | ||
| level={"body-sm"} | ||
| rel={"noopener"} | ||
| target={"_blank"} | ||
| > | ||
| format string syntax docs | ||
| </Link> | ||
| {" "} | ||
| or leave this blank to display the entire log event. | ||
| </span> | ||
| ), | ||
| initialValue: getConfig(CONFIG_KEY.DECODER_OPTIONS).formatString, | ||
| label: "Decoder: Format string", | ||
| type: "text", | ||
| }, | ||
| { | ||
| configKey: LOCAL_STORAGE_KEY.DECODER_OPTIONS_LOG_LEVEL_KEY, | ||
| helperText: "Key to extract the log level from.", | ||
| initialValue: getConfig(CONFIG_KEY.DECODER_OPTIONS).logLevelKey, | ||
| label: "Decoder: Log level key", | ||
| type: "text", | ||
| }, | ||
| { | ||
| configKey: LOCAL_STORAGE_KEY.DECODER_OPTIONS_TIMESTAMP_KEY, | ||
| helperText: "Key to extract the log timestamp from.", | ||
| initialValue: getConfig(CONFIG_KEY.DECODER_OPTIONS).timestampKey, | ||
| label: "Decoder: Timestamp key", | ||
| type: "text", | ||
| }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| /** | ||
| * Displays a group of form fields for user input of configuration values. | ||
| * | ||
| * @return | ||
| */ | ||
| const SettingsFormFieldSectionsGroup = () => ( | ||
| <Box className={"settings-form-field-sections-group-container"}> | ||
| <AccordionGroup | ||
| className={"settings-form-field-sections-group"} | ||
| size={"sm"} | ||
| > | ||
| {getConfigFormFieldSections().map( | ||
| ({name, fields}, i) => ( | ||
| <SettingsFormFieldsSection | ||
| fields={fields} | ||
| key={i} | ||
| name={name}/> | ||
| ) | ||
| )} | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </AccordionGroup> | ||
| </Box> | ||
| ); | ||
|
|
||
|
|
||
| export default SettingsFormFieldSectionsGroup; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,48 @@ | ||||||||||||||||||||||||||||||||||||
| import React from "react"; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||
| Accordion, | ||||||||||||||||||||||||||||||||||||
| AccordionDetails, | ||||||||||||||||||||||||||||||||||||
| AccordionSummary, | ||||||||||||||||||||||||||||||||||||
| } from "@mui/joy"; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| import SettingsFormField, {SettingsFormFieldProps} from "./SettingsFormField"; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| interface SettingsFormFieldsSectionProps { | ||||||||||||||||||||||||||||||||||||
| name: string; | ||||||||||||||||||||||||||||||||||||
| fields: Array<React.ReactNode | SettingsFormFieldProps>; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||
| * Displays a section of form fields for user input of configuration values. | ||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||
| * @param props | ||||||||||||||||||||||||||||||||||||
| * @param props.fields | ||||||||||||||||||||||||||||||||||||
| * @param props.name | ||||||||||||||||||||||||||||||||||||
| * @return | ||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||
| const SettingsFormFieldsSection = ({ | ||||||||||||||||||||||||||||||||||||
| fields, | ||||||||||||||||||||||||||||||||||||
| name, | ||||||||||||||||||||||||||||||||||||
| }: SettingsFormFieldsSectionProps) => ( | ||||||||||||||||||||||||||||||||||||
| <Accordion defaultExpanded={true}> | ||||||||||||||||||||||||||||||||||||
| <AccordionSummary variant={"soft"}> | ||||||||||||||||||||||||||||||||||||
| {name} | ||||||||||||||||||||||||||||||||||||
| </AccordionSummary> | ||||||||||||||||||||||||||||||||||||
| <AccordionDetails> | ||||||||||||||||||||||||||||||||||||
| {fields.map( | ||||||||||||||||||||||||||||||||||||
| (f, index) => ( | ||||||||||||||||||||||||||||||||||||
| React.isValidElement(f) ? | ||||||||||||||||||||||||||||||||||||
| f : | ||||||||||||||||||||||||||||||||||||
| <SettingsFormField | ||||||||||||||||||||||||||||||||||||
| key={index} | ||||||||||||||||||||||||||||||||||||
| {...(f as SettingsFormFieldProps)}/> | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+40
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid using array index as key. Using the array index as a key can lead to performance issues and unexpected behavior when items are added, removed, or reordered. Consider using a more stable identifier. {fields.map(
(f, index) => (
React.isValidElement(f) ?
f :
<SettingsFormField
- key={index}
+ key={`field-${(f as SettingsFormFieldProps).configKey}`}
{...(f as SettingsFormFieldProps)}/>
)
)}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||||||||||||||
| </AccordionDetails> | ||||||||||||||||||||||||||||||||||||
| </Accordion> | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| export default SettingsFormFieldsSection; | ||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.