From eb1bd27ec0e491c8a747c4bb442eb7dfb0aa6350 Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Sun, 8 May 2022 10:40:45 +0100 Subject: [PATCH] feat: add ban list search (closes #579) --- src/pages/settings/server/Bans.tsx | 66 +++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 14 deletions(-) diff --git a/src/pages/settings/server/Bans.tsx b/src/pages/settings/server/Bans.tsx index 084920a9..a6fefeec 100644 --- a/src/pages/settings/server/Bans.tsx +++ b/src/pages/settings/server/Bans.tsx @@ -6,22 +6,23 @@ import { Server } from "revolt.js"; import styles from "./Panes.module.scss"; import { Text } from "preact-i18n"; -import { useEffect, useState } from "preact/hooks"; +import { useEffect, useMemo, useState } from "preact/hooks"; import UserIcon from "../../../components/common/user/UserIcon"; import IconButton from "../../../components/ui/IconButton"; import Preloader from "../../../components/ui/Preloader"; +import { InputBox } from "@revoltchat/ui"; interface InnerProps { ban: API.ServerBan; - users: Pick[]; + users: Record; server: Server; removeSelf: () => void; } const Inner = observer(({ ban, users, server, removeSelf }: InnerProps) => { const [deleting, setDelete] = useState(false); - const user = users.find((x) => x._id === ban._id.user); + const user = users[ban._id.user]; return (
@@ -51,14 +52,51 @@ interface Props { } export const Bans = observer(({ server }: Props) => { - const [data, setData] = useState(undefined); + const [query, setQuery] = useState(""); + const [result, setData] = useState< + | { + users: Record; + bans: API.BanListResult["bans"]; + } + | undefined + >(undefined); useEffect(() => { - server.fetchBans().then(setData); + server + .fetchBans() + .then((data) => { + const users: Record = {}; + for (const user of data.users) { + users[user._id] = user; + } + + return { + users, + bans: data.bans, + }; + }) + .then(setData); }, [server, setData]); + const bans = useMemo(() => { + if (!result) return; + + if (query) + return result.bans.filter(({ _id }) => + result.users[_id.user]?.username.includes(query), + ); + + return result.bans; + }, [query, result]); + return (
+ setQuery(e.currentTarget.value)} + palette="secondary" + />
@@ -70,25 +108,25 @@ export const Bans = observer(({ server }: Props) => {
- {typeof data === "undefined" && } - {data && ( + {typeof bans === "undefined" && } + {bans && (
( { setData({ - bans: data.bans.filter( + bans: result!.bans.filter( (y) => y._id.user !== - data.bans[index]._id.user, + bans[index]._id.user, ), - users: data.users, + users: result!.users, }); }} />