diff --git a/components/SearchModal/index.tsx b/components/SearchModal/index.tsx index 7a2f9945..a9b59964 100644 --- a/components/SearchModal/index.tsx +++ b/components/SearchModal/index.tsx @@ -26,7 +26,7 @@ const SearchModal = (props: Props) => { let searchInput = React.createRef() - const [pool, setPool] = useState(Array()) + const [objects, setObjects] = useState<{[id: number]: GridCharacter | GridWeapon | GridSummon}>() const [open, setOpen] = useState(false) const [query, setQuery] = useState('') const [results, setResults] = useState({}) @@ -34,14 +34,8 @@ const SearchModal = (props: Props) => { const [message, setMessage] = useState('') const [totalResults, setTotalResults] = useState(0) - useEffect(() => { - if (props.object === 'characters') { - setPool(Object.values(grid.characters).map(o => o.character)) - } else if (props.object === 'weapons') { - setPool(Object.values(grid.weapons.allWeapons).map(o => o.weapon)) - } else if (props.object === 'summons') { - setPool(Object.values(grid.summons.allSummons).map(o => o.summon)) - } + useEffect(() => { + setObjects(grid[props.object]) }, [grid, props.object]) useEffect(() => { @@ -49,13 +43,6 @@ const SearchModal = (props: Props) => { searchInput.current.focus() }, [searchInput]) - function filterExclusions(object: Character | Weapon | Summon) { - if (pool[props.fromPosition] && - object.granblue_id === pool[props.fromPosition].granblue_id) - return null - else return object - } - function inputChanged(event: React.ChangeEvent) { const text = event.target.value if (text.length) { @@ -73,9 +60,13 @@ const SearchModal = (props: Props) => { } function fetchResults() { - const excludes = Object.values(pool) + // Exclude objects in grid from search results + // unless the object is in the position that the user clicked + // so that users can replace object versions with + // compatible other objects. + const excludes = (objects) ? Object.values(objects) .filter(filterExclusions) - .map((o) => { return o.name.en }).join(',') + .map((o) => { return (o.object) ? o.object.name.en : undefined }).join(',') : '' api.search(props.object, query, excludes) .then(response => { @@ -89,6 +80,13 @@ const SearchModal = (props: Props) => { }) } + function filterExclusions(gridObject: GridCharacter | GridWeapon | GridSummon) { + if (objects && gridObject.object && + gridObject.object.granblue_id === objects[props.fromPosition]?.object.granblue_id) + return null + else return gridObject + } + function sendData(result: Character | Weapon | Summon) { props.send(result, props.fromPosition) setOpen(false)