1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-09 00:18:00 +01:00

feat: advanced playground multi value context fields (#4053)

<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->
Makes the autocomplete component in the playground context field - when
selecting a custom context field that has legal values - able to
select/edit multiple values.

## About the changes
<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

<!-- Does it close an issue? Multiple? -->
Closes #
[1-1006](https://linear.app/unleash/issue/1-1006/multi-value-context-field)

<!-- (For internal contributors): Does it relate to an issue on public
roadmap? -->
<!--
Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#
-->

### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->

---------

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
This commit is contained in:
andreas-unleash 2023-06-22 11:06:25 +03:00 committed by GitHub
parent 81babbfd6d
commit 566b91e298
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 5 deletions

View File

@ -27,6 +27,8 @@ import useToast from 'hooks/useToast';
import { PlaygroundEditor } from './PlaygroundEditor/PlaygroundEditor'; import { PlaygroundEditor } from './PlaygroundEditor/PlaygroundEditor';
import { GuidanceIndicator } from 'component/common/GuidanceIndicator/GuidanceIndicator'; import { GuidanceIndicator } from 'component/common/GuidanceIndicator/GuidanceIndicator';
import { parseDateValue, parseValidDate } from 'component/common/util'; import { parseDateValue, parseValidDate } from 'component/common/util';
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
import { isStringOrStringArray } from '../../playground.utils';
interface IPlaygroundCodeFieldsetProps { interface IPlaygroundCodeFieldsetProps {
context: string | undefined; context: string | undefined;
setContext: Dispatch<SetStateAction<string | undefined>>; setContext: Dispatch<SetStateAction<string | undefined>>;
@ -37,6 +39,9 @@ export const PlaygroundCodeFieldset: VFC<IPlaygroundCodeFieldsetProps> = ({
setContext, setContext,
}) => { }) => {
const theme = useTheme(); const theme = useTheme();
const { uiConfig } = useUiConfig();
const isAdvancedPlayground = Boolean(uiConfig.flags.advancedPlayground);
const { setToastData } = useToast(); const { setToastData } = useToast();
const { context: contextData } = useUnleashContext(); const { context: contextData } = useUnleashContext();
const contextOptions = contextData const contextOptions = contextData
@ -103,6 +108,44 @@ export const PlaygroundCodeFieldset: VFC<IPlaygroundCodeFieldsetProps> = ({
} }
}; };
const onAutoCompleteChange = (
e: FormEvent,
newValue: string | (string | string[])[] | null
) => {
if (!isStringOrStringArray(newValue)) return;
if (Array.isArray(newValue)) {
const temp =
(newValue || []).length > 1 ? newValue.join(',') : newValue[0];
return setContextValue(temp);
}
setContextValue(newValue);
};
const resolveAutocompleteValue = (): string | string[] | undefined => {
//This is needed for clearing the Autocomplete Chips when changing the context field
//and the new field also has legal values
if (contextValue === '') {
return undefined;
}
if (isAdvancedPlayground) {
// Split comma separated strings to array for fields with legal values
const foundField = contextData.find(
contextData => contextData.name === contextField
);
const hasLegalValues = (foundField?.legalValues || []).length > 1;
if (contextValue.includes(',') && hasLegalValues) {
return contextValue.split(',');
}
return [contextValue as string];
}
return contextValue;
};
const resolveInput = () => { const resolveInput = () => {
if (contextField === 'currentTime') { if (contextField === 'currentTime') {
const validDate = parseValidDate(contextValue); const validDate = parseValidDate(contextValue);
@ -146,12 +189,10 @@ export const PlaygroundCodeFieldset: VFC<IPlaygroundCodeFieldsetProps> = ({
disablePortal disablePortal
id="context-legal-values" id="context-legal-values"
size="small" size="small"
onChange={(e: FormEvent, newValue) => { value={resolveAutocompleteValue()}
if (typeof newValue === 'string') { onChange={onAutoCompleteChange}
return setContextValue(newValue);
}
}}
options={options} options={options}
multiple={isAdvancedPlayground}
sx={{ width: 200, maxWidth: '100%' }} sx={{ width: 200, maxWidth: '100%' }}
renderInput={(params: any) => ( renderInput={(params: any) => (
<TextField {...params} label="Value" /> <TextField {...params} label="Value" />

View File

@ -59,3 +59,17 @@ export const resolveResultsWidth = (
return '50%'; return '50%';
}; };
export const isStringOrStringArray = (
value: unknown
): value is string | string[] => {
if (typeof value === 'string') {
return true;
}
if (Array.isArray(value)) {
return value.every(item => typeof item === 'string');
}
return false;
};