-
Notifications
You must be signed in to change notification settings - Fork 16
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 all commits
ade7efd
240e37b
2c94d63
4fece91
54b7928
d9e685d
b557613
ae0e945
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,112 @@ | ||
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"; | ||
|
||
|
||
type SettingsFormFields = SettingsFormFieldProps | React.ReactNode; | ||
|
||
/** | ||
* Gets form fields information for user input of configuration values. | ||
* | ||
* @return A list of form fields information or React nodes. | ||
*/ | ||
const getConfigFormFieldSections = (): Array<{ | ||
name: string; | ||
fields: SettingsFormFields[]; | ||
}> => [ | ||
{ | ||
name: "Common", | ||
fields: [ | ||
<ThemeSwitchFormField key={"settings-theme-switch"}/>, | ||
{ | ||
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}) => ( | ||
<SettingsFormFieldsSection | ||
fields={fields} | ||
key={name} | ||
name={name}/> | ||
) | ||
)} | ||
</AccordionGroup> | ||
</Box> | ||
); | ||
|
||
export type {SettingsFormFields}; | ||
export default SettingsFormFieldSectionsGroup; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.settings-form-field-section-summary { | ||
font-weight: 700 !important; | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,54 @@ | ||||||||||||||||||||||||||||||||||||
import React from "react"; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
import { | ||||||||||||||||||||||||||||||||||||
Accordion, | ||||||||||||||||||||||||||||||||||||
AccordionDetails, | ||||||||||||||||||||||||||||||||||||
AccordionSummary, | ||||||||||||||||||||||||||||||||||||
} from "@mui/joy"; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
import SettingsFormField, {SettingsFormFieldProps} from "./SettingsFormField"; | ||||||||||||||||||||||||||||||||||||
import {SettingsFormFields} from "./SettingsFormFieldSectionsGroup"; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
import "./SettingsFormFieldsSection.css"; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
interface SettingsFormFieldsSectionProps { | ||||||||||||||||||||||||||||||||||||
name: string; | ||||||||||||||||||||||||||||||||||||
fields: SettingsFormFields[]; | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||
* Displays a section of form fields for user input of configuration values. | ||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||
* @param props | ||||||||||||||||||||||||||||||||||||
* @param props.fields A list of form fields information or React nodes. | ||||||||||||||||||||||||||||||||||||
* @param props.name | ||||||||||||||||||||||||||||||||||||
* @return | ||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||
const SettingsFormFieldsSection = ({ | ||||||||||||||||||||||||||||||||||||
fields, | ||||||||||||||||||||||||||||||||||||
name, | ||||||||||||||||||||||||||||||||||||
}: SettingsFormFieldsSectionProps) => ( | ||||||||||||||||||||||||||||||||||||
<Accordion defaultExpanded={true}> | ||||||||||||||||||||||||||||||||||||
<AccordionSummary | ||||||||||||||||||||||||||||||||||||
className={"settings-form-field-section-summary"} | ||||||||||||||||||||||||||||||||||||
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick (assertive)
Consider supporting more input types.
Currently, the component only handles "number" type specifically and defaults to a textarea for all other types. In the future, you might want to support more HTML input types (text, email, password, etc.) for better form control and validation.
📝 Committable suggestion