-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add parameterization rule UI (#257)
- Loading branch information
Showing
21 changed files
with
474 additions
and
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Box, Grid, Heading, Text } from '@radix-ui/themes' | ||
import { FilterField } from './FilterField' | ||
import { SelectorField } from './SelectorField' | ||
import { ParamaterizationValueEditor } from './ParameterizationValueEditor' | ||
|
||
export function ParameterizationEditor() { | ||
return ( | ||
<> | ||
<Heading size="2" weight="medium" mb="2"> | ||
Parameterization | ||
</Heading> | ||
|
||
<Text size="2" as="p" mb="2" color="gray"> | ||
Replace request data with variables or custom values. | ||
</Text> | ||
<Grid columns="2" gap="3"> | ||
<Box> | ||
<FilterField field="filter" /> | ||
<SelectorField field="selector" /> | ||
</Box> | ||
<Box> | ||
<ParamaterizationValueEditor /> | ||
</Box> | ||
</Grid> | ||
</> | ||
) | ||
} |
115 changes: 115 additions & 0 deletions
115
src/views/Generator/RuleEditor/ParameterizationValueEditor.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,115 @@ | ||
import { Code, Flex, Text, TextField } from '@radix-ui/themes' | ||
import { ControlledSelect, FieldGroup } from '@/components/Form' | ||
import { ParameterizationRule } from '@/types/rules' | ||
import { useFormContext } from 'react-hook-form' | ||
import { useGeneratorStore } from '@/store/generator' | ||
import { useMemo } from 'react' | ||
import { exhaustive } from '@/utils/typescript' | ||
|
||
export function ParamaterizationValueEditor() { | ||
const { | ||
control, | ||
formState: { errors }, | ||
} = useFormContext<ParameterizationRule>() | ||
|
||
const variablesExist = useGeneratorStore( | ||
(state) => state.variables.length > 0 | ||
) | ||
|
||
const variablesLabel = variablesExist | ||
? 'Variables' | ||
: 'Variables (add in test options)' | ||
|
||
const VALUE_TYPE_OPTIONS = [ | ||
{ value: 'string', label: 'Text value' }, | ||
{ value: 'variable', label: variablesLabel, disabled: !variablesExist }, | ||
] | ||
|
||
return ( | ||
<> | ||
<FieldGroup name="value.type" errors={errors} label="Replace with"> | ||
<ControlledSelect | ||
control={control} | ||
name="value.type" | ||
options={VALUE_TYPE_OPTIONS} | ||
/> | ||
</FieldGroup> | ||
<ValueTypeSwitch /> | ||
</> | ||
) | ||
} | ||
|
||
function ValueTypeSwitch() { | ||
const { | ||
watch, | ||
register, | ||
formState: { errors }, | ||
} = useFormContext<ParameterizationRule>() | ||
|
||
const type = watch('value.type') | ||
|
||
switch (type) { | ||
case 'string': | ||
return ( | ||
<FieldGroup name="value.value" errors={errors} label="Value"> | ||
<TextField.Root placeholder="Value" {...register('value.value')} /> | ||
</FieldGroup> | ||
) | ||
case 'variable': | ||
return <VariableSelect /> | ||
|
||
case 'array': | ||
case 'customCode': | ||
return null | ||
|
||
default: | ||
return exhaustive(type) | ||
} | ||
} | ||
|
||
function VariableSelect() { | ||
const variables = useGeneratorStore((store) => store.variables) | ||
|
||
const options = useMemo(() => { | ||
return variables.map((variable) => ({ | ||
value: variable.name, | ||
label: ( | ||
<Flex gap="1" align="center"> | ||
<Code size="2" truncate> | ||
{variable.name} | ||
</Code> | ||
<Text truncate size="1" css={{ flex: '1' }}> | ||
{variable.value} | ||
</Text> | ||
</Flex> | ||
), | ||
})) | ||
}, [variables]) | ||
|
||
const { | ||
control, | ||
watch, | ||
formState: { errors }, | ||
} = useFormContext<ParameterizationRule>() | ||
|
||
const variableName = watch('value.variableName') | ||
|
||
return ( | ||
<FieldGroup name="value.variableName" errors={errors} label="Variable"> | ||
<ControlledSelect | ||
options={options} | ||
control={control} | ||
name="value.variableName" | ||
selectProps={{ | ||
// Automatically open the select when switching to variable type | ||
// in new parameterization rule | ||
defaultOpen: !variableName, | ||
}} | ||
contentProps={{ | ||
css: { maxWidth: 'var(--radix-select-trigger-width)' }, | ||
position: 'popper', | ||
}} | ||
/> | ||
</FieldGroup> | ||
) | ||
} |
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
Oops, something went wrong.