Skip to content

Commit

Permalink
feat: add inline config error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
CSharperMantle committed Jul 10, 2023
1 parent da8095a commit 5bb25c3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
11 changes: 8 additions & 3 deletions src/components/FileFormControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import TextField from "@mui/material/TextField"
import Tooltip from "@mui/material/Tooltip"
import styled from "@mui/system/styled"

import { isNil } from "../common"

const HiddenInput = styled("input")({
display: "none",
})
Expand Down Expand Up @@ -83,6 +85,7 @@ interface IFileFormControlProps {

export const FileFormControl = (props: IFileFormControlProps) => {
const [fileContent, setFileContent] = React.useState(props.initialFileContent)
const [error, setError] = React.useState(false)

return (
<Grid container spacing={0} direction="row" alignItems="center">
Expand All @@ -94,6 +97,7 @@ export const FileFormControl = (props: IFileFormControlProps) => {
label={props.label}
helperText={props.helperText}
disabled={props.disabled ?? false}
error={error}
InputProps={{
readOnly: props.readOnly ?? false,
}}
Expand All @@ -108,9 +112,10 @@ export const FileFormControl = (props: IFileFormControlProps) => {
onFileChange={async (event) => {
let content = (await head(event.target.files)?.text()) ?? ""
content = props.contentPreprocessor?.(content) ?? content
if (props.onFileChange(content)) {
setFileContent(content)
}
const result = props.onFileChange(content)
const isSuccessful = isNil(result) || result
setFileContent(content)
setError(!isSuccessful)
}}
/>
</Grid>
Expand Down
10 changes: 6 additions & 4 deletions src/components/NumberFormControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ interface INumberFormControlProps {

export const NumberFormControl = (props: INumberFormControlProps) => {
const [content, setContent] = React.useState(props.initialContent)
const [error, setError] = React.useState(false)

return (
<TextField
Expand All @@ -55,14 +56,15 @@ export const NumberFormControl = (props: INumberFormControlProps) => {
max: props.max,
}}
disabled={props.disabled ?? false}
error={error}
onChange={(event) => {
const content = isNil(props.contentPreprocessor)
? event.target.value
: props.contentPreprocessor(event.target.value)

if (props.onChange(content)) {
setContent(content)
}
const result = props.onChange(content)
const isSuccessful = isNil(result) || result
setContent(content)
setError(!isSuccessful)
}}
/>
)
Expand Down
6 changes: 5 additions & 1 deletion src/pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ const App = ({ data }: PageProps<Queries.SettingsPageQuery>) => {
)

const jsonMinifyPreprocessor = (json: string): string => {
return JSON.stringify(JSON.parse(json))
try {
return JSON.stringify(JSON.parse(json))
} catch (ex) {
return "{}"
}
}

return (
Expand Down

0 comments on commit 5bb25c3

Please sign in to comment.