|
| 1 | +import React from "react"; |
| 2 | +import { InputPicker, InputPickerProps } from "rsuite"; |
| 3 | + |
| 4 | +import { Action } from "devtools/types/normandyApi"; |
| 5 | +import { CompareFunc, makeCompare } from "devtools/utils/helpers"; |
| 6 | +import NormandyAPI from "devtools/utils/normandyApi"; |
| 7 | + |
| 8 | +const actionGroups = { |
| 9 | + "addon-rollback": "Development", |
| 10 | + "addon-rollout": "Development", |
| 11 | + "addon-study": "Legacy", |
| 12 | + "branched-addon-study": "Primary", |
| 13 | + "console-log": "Development", |
| 14 | + "messaging-experiment": "Development", |
| 15 | + "multi-preference-experiment": "Primary", |
| 16 | + "opt-out-study": "Legacy", |
| 17 | + "preference-experiment": "Legacy", |
| 18 | + "preference-rollback": "Primary", |
| 19 | + "preference-rollout": "Primary", |
| 20 | + "show-heartbeat": "Primary", |
| 21 | +}; |
| 22 | + |
| 23 | +type Props = Omit<InputPickerProps, "data" | "sort" | "onChange"> & { |
| 24 | + value: string; |
| 25 | + normandyApi: NormandyAPI; |
| 26 | + onChangeName?: (newValue: string) => void; |
| 27 | + onChangeAction?: (newValue: Action) => void; |
| 28 | +}; |
| 29 | + |
| 30 | +// default export |
| 31 | +const ActionSelector: React.FC<Props> = ({ |
| 32 | + normandyApi, |
| 33 | + onChangeName, |
| 34 | + onChangeAction, |
| 35 | + value, |
| 36 | + placeholder = "Any", |
| 37 | + defaultValue = null, |
| 38 | + ...inputPickerProps |
| 39 | +}) => { |
| 40 | + const [actions, setActions] = React.useState<Array<Action>>(null); |
| 41 | + |
| 42 | + React.useEffect(() => { |
| 43 | + normandyApi.fetchAllActions().then(setActions); |
| 44 | + }, [normandyApi]); |
| 45 | + |
| 46 | + interface DataItem { |
| 47 | + value: string; |
| 48 | + label: string; |
| 49 | + group: string; |
| 50 | + groupTitle?: string; |
| 51 | + } |
| 52 | + |
| 53 | + const actionSelectData: InputPickerProps["data"] = actions?.map( |
| 54 | + (action): DataItem => { |
| 55 | + return { |
| 56 | + value: action.name, |
| 57 | + label: action.name, |
| 58 | + group: actionGroups[action.name], |
| 59 | + }; |
| 60 | + }, |
| 61 | + ); |
| 62 | + |
| 63 | + const actionGroupOrder = ["Primary", "Development", "Legacy"]; |
| 64 | + |
| 65 | + function sort(isGroup: boolean): CompareFunc<DataItem> { |
| 66 | + if (isGroup) { |
| 67 | + return makeCompare((v) => actionGroupOrder.indexOf(v.groupTitle)); |
| 68 | + } |
| 69 | + |
| 70 | + return makeCompare((v) => v.label); |
| 71 | + } |
| 72 | + |
| 73 | + function handleChange(newName: string): void { |
| 74 | + if (onChangeName) { |
| 75 | + onChangeName(newName); |
| 76 | + } |
| 77 | + |
| 78 | + if (onChangeAction) { |
| 79 | + const action = actions.find((a) => a.name === newName); |
| 80 | + onChangeAction(action); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return ( |
| 85 | + <InputPicker |
| 86 | + {...inputPickerProps} |
| 87 | + data={actionSelectData} |
| 88 | + defaultValue={defaultValue} |
| 89 | + groupBy="group" |
| 90 | + placeholder={placeholder} |
| 91 | + sort={sort} |
| 92 | + value={value} |
| 93 | + onChange={handleChange} |
| 94 | + /> |
| 95 | + ); |
| 96 | +}; |
| 97 | + |
| 98 | +export default ActionSelector; |
0 commit comments