|
| 1 | +import React, { useState, useEffect, useContext } from "react"; |
| 2 | +import { useOutletContext } from "react-router-dom"; |
| 3 | +import axios from "axios"; |
| 4 | +import { |
| 5 | + Box, |
| 6 | + Card, |
| 7 | + CardContent, |
| 8 | + IconButton, |
| 9 | + Stack, |
| 10 | + Tooltip, |
| 11 | + Typography, |
| 12 | +} from "@mui/material"; |
| 13 | + |
| 14 | +import { UserContext } from "Contexts"; |
| 15 | +import { useErrorAlert } from "components/Alerts"; |
| 16 | +import { NameWithAvatar } from "pages/User/User"; |
| 17 | +import { Time } from "components/Basic"; |
| 18 | +import { PageNav } from "components/Nav"; |
| 19 | +import { Loading } from "components/Loading"; |
| 20 | + |
| 21 | +export default function FlaggedIntake() { |
| 22 | + const { user } = useOutletContext() || {}; |
| 23 | + const currentUser = useContext(UserContext); |
| 24 | + const activeUser = user || currentUser; |
| 25 | + |
| 26 | + const [users, setUsers] = useState([]); |
| 27 | + const [page, setPage] = useState(1); |
| 28 | + const [loading, setLoading] = useState(true); |
| 29 | + const [actionLoading, setActionLoading] = useState({}); |
| 30 | + |
| 31 | + const errorAlert = useErrorAlert(); |
| 32 | + |
| 33 | + const canWhitelist = activeUser?.perms?.whitelist; |
| 34 | + |
| 35 | + useEffect(() => { |
| 36 | + loadFlaggedUsers(1); |
| 37 | + }, []); |
| 38 | + |
| 39 | + function loadFlaggedUsers(targetPage) { |
| 40 | + setLoading(true); |
| 41 | + |
| 42 | + const params = targetPage === 1 ? "" : `?last=${users[users.length - 1]?.joined || ""}`; |
| 43 | + |
| 44 | + axios |
| 45 | + .get(`/api/user/flagged${params}`) |
| 46 | + .then((res) => { |
| 47 | + if (res.data.length > 0 || targetPage === 1) { |
| 48 | + setUsers(res.data); |
| 49 | + setPage(targetPage); |
| 50 | + } |
| 51 | + setLoading(false); |
| 52 | + }) |
| 53 | + .catch((e) => { |
| 54 | + errorAlert(e); |
| 55 | + setLoading(false); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + function onPageNav(newPage) { |
| 60 | + if (newPage === page) return; |
| 61 | + |
| 62 | + if (newPage > page && users.length > 0) { |
| 63 | + const lastJoined = users[users.length - 1]?.joined; |
| 64 | + axios |
| 65 | + .get(`/api/user/flagged?last=${lastJoined}`) |
| 66 | + .then((res) => { |
| 67 | + if (res.data.length > 0) { |
| 68 | + setUsers(res.data); |
| 69 | + setPage(newPage); |
| 70 | + } |
| 71 | + }) |
| 72 | + .catch(errorAlert); |
| 73 | + } else if (newPage < page && users.length > 0) { |
| 74 | + const firstJoined = users[0]?.joined; |
| 75 | + axios |
| 76 | + .get(`/api/user/flagged?first=${firstJoined}`) |
| 77 | + .then((res) => { |
| 78 | + if (res.data.length > 0) { |
| 79 | + setUsers(res.data); |
| 80 | + setPage(newPage); |
| 81 | + } |
| 82 | + }) |
| 83 | + .catch(errorAlert); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + function onWhitelist(userId) { |
| 88 | + if (!canWhitelist) return; |
| 89 | + |
| 90 | + setActionLoading((prev) => ({ ...prev, [userId]: "whitelist" })); |
| 91 | + |
| 92 | + axios |
| 93 | + .post("/api/mod/whitelist", { userId }) |
| 94 | + .then(() => { |
| 95 | + setUsers((prev) => prev.filter((u) => u.id !== userId)); |
| 96 | + setActionLoading((prev) => { |
| 97 | + const next = { ...prev }; |
| 98 | + delete next[userId]; |
| 99 | + return next; |
| 100 | + }); |
| 101 | + }) |
| 102 | + .catch((e) => { |
| 103 | + errorAlert(e); |
| 104 | + setActionLoading((prev) => { |
| 105 | + const next = { ...prev }; |
| 106 | + delete next[userId]; |
| 107 | + return next; |
| 108 | + }); |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + function onBlacklist(userId) { |
| 113 | + if (!canWhitelist) return; |
| 114 | + |
| 115 | + setActionLoading((prev) => ({ ...prev, [userId]: "blacklist" })); |
| 116 | + |
| 117 | + axios |
| 118 | + .post("/api/mod/blacklist", { userId }) |
| 119 | + .then(() => { |
| 120 | + setUsers((prev) => prev.filter((u) => u.id !== userId)); |
| 121 | + setActionLoading((prev) => { |
| 122 | + const next = { ...prev }; |
| 123 | + delete next[userId]; |
| 124 | + return next; |
| 125 | + }); |
| 126 | + }) |
| 127 | + .catch((e) => { |
| 128 | + errorAlert(e); |
| 129 | + setActionLoading((prev) => { |
| 130 | + const next = { ...prev }; |
| 131 | + delete next[userId]; |
| 132 | + return next; |
| 133 | + }); |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + if (loading && users.length === 0) { |
| 138 | + return <Loading small />; |
| 139 | + } |
| 140 | + |
| 141 | + const userRows = users.map((flaggedUser) => { |
| 142 | + const isProcessing = actionLoading[flaggedUser.id]; |
| 143 | + |
| 144 | + return ( |
| 145 | + <Card |
| 146 | + key={flaggedUser.id} |
| 147 | + variant="outlined" |
| 148 | + sx={{ mb: 1 }} |
| 149 | + > |
| 150 | + <CardContent |
| 151 | + sx={{ |
| 152 | + display: "flex", |
| 153 | + alignItems: "center", |
| 154 | + justifyContent: "space-between", |
| 155 | + py: 1.5, |
| 156 | + "&:last-child": { pb: 1.5 }, |
| 157 | + }} |
| 158 | + > |
| 159 | + <Stack direction="row" spacing={2} sx={{ alignItems: "center", flexGrow: 1 }}> |
| 160 | + <Box sx={{ minWidth: 150 }}> |
| 161 | + <NameWithAvatar |
| 162 | + id={flaggedUser.id} |
| 163 | + name={flaggedUser.name} |
| 164 | + avatar={flaggedUser.avatar} |
| 165 | + /> |
| 166 | + </Box> |
| 167 | + <Stack direction="column" spacing={0.5}> |
| 168 | + <Typography variant="caption" color="textSecondary"> |
| 169 | + Joined: {new Date(flaggedUser.joined).toLocaleDateString()} |
| 170 | + {" ("} |
| 171 | + <Time minSec millisec={Date.now() - flaggedUser.joined} suffix=" ago" /> |
| 172 | + {")"} |
| 173 | + </Typography> |
| 174 | + </Stack> |
| 175 | + </Stack> |
| 176 | + |
| 177 | + {canWhitelist && ( |
| 178 | + <Stack direction="row" spacing={1}> |
| 179 | + <Tooltip title="Whitelist (approve user)"> |
| 180 | + <span> |
| 181 | + <IconButton |
| 182 | + size="small" |
| 183 | + color="success" |
| 184 | + onClick={() => onWhitelist(flaggedUser.id)} |
| 185 | + disabled={!!isProcessing} |
| 186 | + sx={{ |
| 187 | + border: "1px solid", |
| 188 | + borderColor: "success.main", |
| 189 | + "&:hover": { |
| 190 | + backgroundColor: "success.main", |
| 191 | + color: "white", |
| 192 | + }, |
| 193 | + }} |
| 194 | + > |
| 195 | + <i |
| 196 | + className={isProcessing === "whitelist" ? "fas fa-spinner fa-spin" : "fas fa-check"} |
| 197 | + /> |
| 198 | + </IconButton> |
| 199 | + </span> |
| 200 | + </Tooltip> |
| 201 | + <Tooltip title="Blacklist (permanent IP ban)"> |
| 202 | + <span> |
| 203 | + <IconButton |
| 204 | + size="small" |
| 205 | + color="error" |
| 206 | + onClick={() => onBlacklist(flaggedUser.id)} |
| 207 | + disabled={!!isProcessing} |
| 208 | + sx={{ |
| 209 | + border: "1px solid", |
| 210 | + borderColor: "error.main", |
| 211 | + "&:hover": { |
| 212 | + backgroundColor: "error.main", |
| 213 | + color: "white", |
| 214 | + }, |
| 215 | + }} |
| 216 | + > |
| 217 | + <i |
| 218 | + className={isProcessing === "blacklist" ? "fas fa-spinner fa-spin" : "fas fa-times"} |
| 219 | + /> |
| 220 | + </IconButton> |
| 221 | + </span> |
| 222 | + </Tooltip> |
| 223 | + </Stack> |
| 224 | + )} |
| 225 | + </CardContent> |
| 226 | + </Card> |
| 227 | + ); |
| 228 | + }); |
| 229 | + |
| 230 | + return ( |
| 231 | + <Box className="flagged-intake"> |
| 232 | + <Typography variant="h4" sx={{ mb: 2 }}> |
| 233 | + Flagged User Intake |
| 234 | + </Typography> |
| 235 | + <Typography variant="body2" color="textSecondary" sx={{ mb: 2 }}> |
| 236 | + Review flagged users and decide whether to whitelist (approve) or blacklist (permanently ban) them. |
| 237 | + </Typography> |
| 238 | + |
| 239 | + <PageNav page={page} onNav={onPageNav} inverted /> |
| 240 | + |
| 241 | + {users.length === 0 ? ( |
| 242 | + <Typography variant="body1" sx={{ py: 2, textAlign: "center" }}> |
| 243 | + No flagged users to review. |
| 244 | + </Typography> |
| 245 | + ) : ( |
| 246 | + <Box sx={{ my: 1 }}>{userRows}</Box> |
| 247 | + )} |
| 248 | + |
| 249 | + <PageNav page={page} onNav={onPageNav} inverted /> |
| 250 | + </Box> |
| 251 | + ); |
| 252 | +} |
0 commit comments