-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { tss } from "tss"; | ||
|
||
type Props = { | ||
className?: string; | ||
title: string; | ||
description: string | undefined; | ||
onResetToDefault: () => void; | ||
error: JSX.Element | string | undefined; | ||
children: JSX.Element; | ||
}; | ||
|
||
export function FormFieldWrapper(props: Props) { | ||
const { className, title, description, onResetToDefault, error, children } = props; | ||
|
||
const { cx, classes } = useStyles({ "isErrored": error !== undefined }); | ||
|
||
return ( | ||
<div className={cx(classes.root, className)}> | ||
<h3 lang="und">{title}</h3> | ||
<button onClick={onResetToDefault}>Reset to default</button> | ||
{description !== undefined && <p lang="und">{description}</p>} | ||
{children} | ||
{error !== undefined && error} | ||
</div> | ||
); | ||
} | ||
|
||
const useStyles = tss | ||
.withName({ FormFieldWrapper }) | ||
.withParams<{ isErrored: boolean }>() | ||
.create(({ theme, isErrored }) => ({ | ||
"root": { | ||
"color": !isErrored | ||
? undefined | ||
: theme.colors.useCases.alertSeverity.error.main | ||
} | ||
})); |
25 changes: 25 additions & 0 deletions
25
web/src/ui/pages/launcher/formFields/YamlCodeBlock.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { YamlCodeBlockFormField } from "./YamlCodeBlockFormField"; | ||
import { action } from "@storybook/addon-actions"; | ||
|
||
const meta = { | ||
title: "pages/Launcher/formFields/YamlCodeBlock", | ||
component: YamlCodeBlockFormField | ||
} satisfies Meta<typeof YamlCodeBlockFormField>; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
"title": "This is the title", | ||
"description": "This is the description", | ||
"expectedDataType": "object", | ||
"value": { | ||
"key1": "value1", | ||
"key2": "value2" | ||
}, | ||
"onChange": action("Value changed") | ||
} | ||
}; |
99 changes: 99 additions & 0 deletions
99
web/src/ui/pages/launcher/formFields/YamlCodeBlockFormField.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { memo, Suspense } from "react"; | ||
import type { Stringifyable } from "core/tools/Stringifyable"; | ||
import { FormFieldWrapper } from "./FormFieldWrapper"; | ||
import { tss } from "tss"; | ||
import { useFormField } from "./useFormField"; | ||
import YAML from "yaml"; | ||
import { declareComponentKeys, useTranslation } from "ui/i18n"; | ||
|
||
type Props = { | ||
className?: string; | ||
title: string; | ||
description: string | undefined; | ||
expectedDataType: "object" | "array"; | ||
value: Record<string, Stringifyable> | Stringifyable[]; | ||
onChange: (newValue: Record<string, Stringifyable> | Stringifyable[]) => void; | ||
}; | ||
|
||
export const YamlCodeBlockFormField = memo((props: Props) => { | ||
const { className, title, description, expectedDataType, value, onChange } = props; | ||
|
||
const { t } = useTranslation({ YamlCodeBlockFormField }); | ||
|
||
const { cx, classes } = useStyles(); | ||
|
||
const { serializedValue, setSerializedValue, errorMessageKey, resetToDefault } = | ||
useFormField< | ||
Record<string, Stringifyable> | Stringifyable[], | ||
string, | ||
"not valid yaml" | "not an array" | "not an object" | ||
>({ | ||
"serializedValue": JSON.stringify(value), | ||
onChange, | ||
"parse": serializedValue => { | ||
let value: Record<string, Stringifyable> | Stringifyable[]; | ||
|
||
try { | ||
value = YAML.parse(serializedValue); | ||
} catch { | ||
return { | ||
"isValid": false, | ||
"errorMessageKey": "not valid yaml" | ||
}; | ||
} | ||
|
||
switch (expectedDataType) { | ||
case "array": | ||
if (!(value instanceof Array)) { | ||
return { | ||
"isValid": false, | ||
"errorMessageKey": "not an array" | ||
}; | ||
} | ||
break; | ||
case "object": | ||
if (!(value instanceof Object) || value instanceof Array) { | ||
return { | ||
"isValid": false, | ||
"errorMessageKey": "not an object" | ||
}; | ||
} | ||
break; | ||
} | ||
|
||
return { | ||
"isValid": true, | ||
value | ||
}; | ||
} | ||
}); | ||
|
||
return ( | ||
<FormFieldWrapper | ||
className={cx(classes.root, className)} | ||
title={title} | ||
description={description} | ||
error={errorMessageKey === undefined ? undefined : t(errorMessageKey)} | ||
onResetToDefault={resetToDefault} | ||
> | ||
<Suspense fallback={null}> | ||
<input | ||
className={cx(classes.input)} | ||
value={serializedValue} | ||
onChange={e => setSerializedValue(e.target.value)} | ||
/> | ||
</Suspense> | ||
</FormFieldWrapper> | ||
); | ||
}); | ||
|
||
const useStyles = tss.withName({ YamlCodeBlockFormField }).create({ | ||
"root": {}, | ||
"input": {} | ||
}); | ||
|
||
const { i18n } = declareComponentKeys< | ||
"not valid yaml" | "not an array" | "not an object" | ||
>()({ YamlCodeBlockFormField }); | ||
|
||
export type I18n = typeof i18n; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useConstCallback } from "powerhooks/useConstCallback"; | ||
import { useConst } from "powerhooks/useConst"; | ||
import { waitForDebounceFactory } from "powerhooks/tools/waitForDebounce"; | ||
|
||
export function useFormField< | ||
TValue, | ||
TSerializedValue extends string | number | boolean, | ||
ErrorMessageKey extends string | ||
>(params: { | ||
serializedValue: TSerializedValue; | ||
onChange: (newValue: TValue) => void; | ||
parse: ( | ||
serializedValue: TSerializedValue | ||
) => | ||
| { isValid: true; value: TValue } | ||
| { isValid: false; errorMessageKey: ErrorMessageKey }; | ||
}): { | ||
serializedValue: TSerializedValue; | ||
setSerializedValue: (newValue: TSerializedValue) => void; | ||
errorMessageKey: ErrorMessageKey | undefined; | ||
resetToDefault: () => void; | ||
} { | ||
const { serializedValue: serializedValue_params, onChange, parse } = params; | ||
|
||
const serializedValue_default = useConst(() => serializedValue_params); | ||
|
||
const [serializedValue, setSerializedValue] = useState(serializedValue_params); | ||
const [errorMessageKey, setErrorMessageKey] = useState<ErrorMessageKey | undefined>( | ||
undefined | ||
); | ||
|
||
useEffect(() => { | ||
setSerializedValue(serializedValue_params); | ||
}, [serializedValue_params]); | ||
|
||
const onChange_const = useConstCallback(onChange); | ||
|
||
const onChangeWithDebounce = useConst(() => { | ||
const { waitForDebounce } = waitForDebounceFactory({ "delay": 500 }); | ||
|
||
async function onChangeWithDebounce(newValue: TValue) { | ||
await waitForDebounce(); | ||
|
||
onChange_const(newValue); | ||
} | ||
|
||
return onChangeWithDebounce; | ||
}); | ||
|
||
useEffect(() => { | ||
const resultOfParse = parse(serializedValue); | ||
|
||
if (!resultOfParse.isValid) { | ||
setErrorMessageKey(resultOfParse.errorMessageKey); | ||
return; | ||
} | ||
|
||
setErrorMessageKey(undefined); | ||
|
||
onChangeWithDebounce(resultOfParse.value); | ||
}, [serializedValue]); | ||
|
||
return { | ||
serializedValue, | ||
setSerializedValue, | ||
errorMessageKey, | ||
"resetToDefault": () => setSerializedValue(serializedValue_default) | ||
}; | ||
} |