Files
sentinel/static/app/utils/convertFromSelect2Choices.tsx
2024-02-15 17:01:50 +00:00

27 lines
1000 B
TypeScript

import type {Choices, SelectValue} from 'sentry/types';
type Input = number | string | undefined | Record<any, any> | string[] | Choices;
function isStringList(maybe: string[] | Choices): maybe is string[] {
return typeof maybe[0] === 'string';
}
/**
* Converts arg from a `select2` choices array to a `react-select` `options` array
* This contains some any hacks as this is creates type errors with the generics
* used in SelectControl as the generics conflict with the concrete types here.
*/
const convertFromSelect2Choices = (choices: Input): SelectValue<any>[] | undefined => {
// TODO(ts): This is to make sure that this function is backwards compatible, ideally,
// this function only accepts arrays
if (!Array.isArray(choices)) {
return undefined;
}
if (isStringList(choices)) {
return choices.map(choice => ({value: choice, label: choice}));
}
return choices.map(choice => ({value: choice[0], label: choice[1]}));
};
export default convertFromSelect2Choices;