diff --git a/frontend/src/app-components/inputs/NlpPatternSelect.tsx b/frontend/src/app-components/inputs/NlpPatternSelect.tsx index 3d98f4b..f8e5782 100644 --- a/frontend/src/app-components/inputs/NlpPatternSelect.tsx +++ b/frontend/src/app-components/inputs/NlpPatternSelect.tsx @@ -6,7 +6,7 @@ * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). */ -import { RemoveOutlined } from "@mui/icons-material"; +import { Cancel } from "@mui/icons-material"; import { Box, CircularProgress, @@ -27,10 +27,13 @@ import { EntityType, Format } from "@/services/types"; import { NlpPattern } from "@/types/block.types"; import { INlpEntity } from "@/types/nlp-entity.types"; -type NlpPatternSelectProps = {}; +type NlpPatternSelectProps = { patterns: NlpPattern[]; onChange: any }; -const NlpPatternSelect = ({}: NlpPatternSelectProps, ref) => { - const [selected, setSelected] = useState([]); +const NlpPatternSelect = ( + { patterns, onChange }: NlpPatternSelectProps, + ref, +) => { + const [selected, setSelected] = useState(patterns); const theme = useTheme(); const { t } = useTranslate(); const { onSearch, searchPayload } = useSearch({ @@ -69,12 +72,6 @@ const NlpPatternSelect = ({}: NlpPatternSelectProps, ref) => { } const handleNlpValueChange = (entity: INlpEntity, valueId: string) => { - const value = getNlpValueFromCache(valueId); - - if (!value) { - throw new Error("Unable to find nlp value in cache"); - } - const newSelection = [...selected]; const update = newSelection.find(({ entity: e }) => e === entity.name); @@ -82,23 +79,32 @@ const NlpPatternSelect = ({}: NlpPatternSelectProps, ref) => { throw new Error("Unable to find nlp entity"); } - if (value.id === entity.id) { + if (valueId === entity.id) { update.match = "entity"; update.value = entity.name; } else { + const value = getNlpValueFromCache(valueId); + + if (!value) { + throw new Error("Unable to find nlp value in cache"); + } update.match = "value"; update.value = value.value; } + + onChange(undefined, newSelection); }; + const defaultValue = + options.filter(({ name }) => + selected.find(({ entity: entityName }) => entityName === name), + ) || {}; return ( - selected.find(({ entity: entityName }) => entityName === name), - )} + defaultValue={defaultValue} multiple={true} options={options} onChange={handleNlpEntityChange} @@ -136,92 +142,121 @@ const NlpPatternSelect = ({}: NlpPatternSelectProps, ref) => { isOptionEqualToValue={(option, value) => option.id === value.id} freeSolo={false} loading={isLoading} - renderTags={(entities, getTagProps) => ( - - {entities.map((entity, index) => { - const { key, onDelete } = getTagProps({ index }); - const handleChange = ( - event: SyntheticEvent, - valueId: string, - ) => { - handleNlpValueChange(entity, valueId); - }; + renderTags={(entities, getTagProps) => { + return ( + + {entities.map((entity, index) => { + const { key, onDelete } = getTagProps({ index }); + const handleChange = ( + event: SyntheticEvent, + valueId: string, + ) => { + handleNlpValueChange(entity, valueId); + }; + const selected = ( + Array.isArray(patterns) + ? patterns?.find((e) => e.entity === entity.name) + : {} + ) as NlpPattern; - return ( - - option - ? getNlpValueFromCache(option)?.value || "-" - : t("label.any") - } - disableClearable - popupIcon={false} - onChange={handleChange} - sx={{ - minWidth: 50, - padding: 0, - ".MuiAutocomplete-input": { - minWidth: "100px !important", - }, - "& .MuiOutlinedInput-root": { - paddingRight: "2rem !important", - "&.MuiInputBase-sizeSmall": { - padding: "0 !important", + return ( + { + const nlpValueCache = getNlpValueFromCache(option); + + if (nlpValueCache) { + return nlpValueCache?.value; + } + + if (selected?.entity === option || option === entity.id) { + return t("label.any"); + } + + return option; + }} + disableClearable + popupIcon={false} + onChange={handleChange} + sx={{ + minWidth: 50, + padding: 0, + ".MuiAutocomplete-input": { + minWidth: "100px !important", }, - }, - }} - renderInput={(props) => ( - onSearch(e.target.value)} - InputProps={{ - ...props.InputProps, - startAdornment: ( - - {entity.name} - - ), - endAdornment: ( - - {isLoading ? ( - - ) : ( - ( + + - - - )} - - ), - }} - /> - )} - /> - ); - })} - - )} + {entity.name} + + + ), + endAdornment: ( + + {isLoading ? ( + + ) : ( + { + onDelete(e); + + onChange( + undefined, + patterns.filter( + (p) => p.entity !== entity.name, + ), + ); + }} + edge="end" + size="small" + > + + + )} + + ), + }} + /> + )} + /> + ); + })} + + ); + }} renderInput={(props) => ( = ({ register, formState: { errors }, } = useFormContext(); - const getNlpEntityFromCache = useGetFromCache(EntityType.NLP_ENTITY); + // const getNlpEntityFromCache = useGetFromCache(EntityType.NLP_ENTITY); const [pattern, setPattern] = useState(value); const [patternType, setPatternType] = useState(getType(value)); const types = [ @@ -148,8 +145,15 @@ const PatternInput: FC = ({ - {patternType === "nlp" && } - {patternType === "nlp" ? ( + {patternType === "nlp" && ( + { + setPattern(data); + }} + /> + )} + {/* {patternType === "nlp" ? ( value={(pattern as NlpPattern[]).map((v) => "value" in v && v.value ? v.value : v.entity, @@ -225,7 +229,7 @@ const PatternInput: FC = ({ }, [] as INlpValue[]); }} /> - ) : null} + ) : null} */} {patternType === "menu" ? ( value={pattern ? (pattern as PayloadPattern).value : null}