From e72354d9b46c77d530caee303be6ebc1e68101d4 Mon Sep 17 00:00:00 2001 From: Bob Date: Thu, 12 Dec 2024 20:24:44 +0100 Subject: [PATCH] Fix laggy social search --- .../worldmap/players/PlayersPanel.tsx | 61 ++++++++++++------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/client/src/ui/components/worldmap/players/PlayersPanel.tsx b/client/src/ui/components/worldmap/players/PlayersPanel.tsx index 06bef20d7..836ef5afb 100644 --- a/client/src/ui/components/worldmap/players/PlayersPanel.tsx +++ b/client/src/ui/components/worldmap/players/PlayersPanel.tsx @@ -1,12 +1,12 @@ import { useDojo } from "@/hooks/context/DojoContext"; -import { useGetAllPlayers } from "@/hooks/helpers/use-get-all-players"; import { useEntitiesUtils } from "@/hooks/helpers/useEntities"; import { useGuilds } from "@/hooks/helpers/useGuilds"; +import Button from "@/ui/elements/Button"; import TextInput from "@/ui/elements/TextInput"; import { getEntityIdFromKeys, toHexString } from "@/ui/utils/utils"; import { ContractAddress, Player } from "@bibliothecadao/eternum"; import { Has, HasValue, getComponentValue, runQuery } from "@dojoengine/recs"; -import { useMemo, useState } from "react"; +import { KeyboardEvent, useMemo, useState } from "react"; import { PlayerCustom, PlayerList } from "./PlayerList"; export const PlayersPanel = ({ @@ -25,15 +25,12 @@ export const PlayersPanel = ({ } = useDojo(); const { getGuildFromPlayerAddress } = useGuilds(); - + const { getEntityName } = useEntitiesUtils(); const userGuild = getGuildFromPlayerAddress(ContractAddress(account.address)); const [isLoading, setIsLoading] = useState(false); - - const [searchInput, setSearchInput] = useState(""); - - const { getEntityName } = useEntitiesUtils(); - const getPlayers = useGetAllPlayers(); + const [inputValue, setInputValue] = useState(""); + const [searchTerm, setSearchTerm] = useState(""); const playersWithStructures: PlayerCustom[] = useMemo(() => { // Sort players by points in descending order @@ -52,7 +49,7 @@ export const PlayersPanel = ({ const structureName = getEntityName(structure.entity_id); return structureName; }) - .filter((structure) => structure !== undefined); + .filter((structure): structure is string => structure !== undefined); const guild = getGuildFromPlayerAddress(player.address); @@ -73,18 +70,22 @@ export const PlayersPanel = ({ }; }); return playersWithStructures; - }, [getPlayers, isLoading]); + }, [isLoading]); const filteredPlayers = useMemo(() => { - return playersWithStructures.filter( - (player) => - player.name.toLowerCase().includes(searchInput.toLowerCase()) || - player.structures.some( - (structure) => structure && structure.toLowerCase().includes(searchInput.toLowerCase()), - ) || - toHexString(player.address).toLowerCase().includes(searchInput.toLowerCase()), - ); - }, [playersWithStructures, searchInput]); + const term = searchTerm.toLowerCase(); + return searchTerm === "" + ? playersWithStructures + : playersWithStructures.filter((player) => { + const nameMatch = player.name.toLowerCase().includes(term); + if (nameMatch) return true; + + const addressMatch = toHexString(player.address).toLowerCase().includes(term); + if (addressMatch) return true; + + return player.structures.some((structure) => structure && structure.toLowerCase().includes(term)); + }); + }, [playersWithStructures, searchTerm]); const whitelistPlayer = (address: ContractAddress) => { setIsLoading(true); @@ -104,17 +105,31 @@ export const PlayersPanel = ({ }).finally(() => setIsLoading(false)); }; + const handleSearch = () => { + setSearchTerm(inputValue); + }; + + const handleKeyDown = (e: KeyboardEvent) => { + if (e.key === "Enter") { + handleSearch(); + } + }; + return (
-
+
setSearchInput(searchInput)} - className="mb-4" + onChange={(value) => setInputValue(value)} + onKeyDown={handleKeyDown} + className="flex-1" /> +
-
+