import React, { useEffect, useState } from "react"; import { Box, Button, Paper, TextField, Typography } from "@mui/material"; import { api } from "../api"; import { useToast } from "../toast"; import { useDialogActions } from "../dialogs"; import { useI18n } from "../i18n/I18nProvider"; type UserDto = { id: string; email?: string | null; userName?: string | null; emailConfirmed: boolean; roles: string[]; }; export default function UserManagementCard() { const { toast } = useToast(); const { confirmAction } = useDialogActions(); const { t } = useI18n(); const [supported, setSupported] = useState(null); const [users, setUsers] = useState([]); const [loading, setLoading] = useState(false); const [newEmail, setNewEmail] = useState(""); const [newPassword, setNewPassword] = useState(""); const [newIsAdmin, setNewIsAdmin] = useState(false); async function load() { setLoading(true); try { const res = await api.get("/users"); setUsers(res.data ?? []); setSupported(true); } catch (e: any) { const status = e?.response?.status; if (status === 401 || status === 403) { setSupported(false); } else { toast(t("adminUsersLoadFailed"), "error"); setSupported(false); } } finally { setLoading(false); } } useEffect(() => { void load(); const onAuthChanged = () => { void load(); }; window.addEventListener("auth-changed", onAuthChanged); return () => window.removeEventListener("auth-changed", onAuthChanged); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); if (supported === false) return null; if (supported === null) return null; return ( {t("adminUsersTitle")} {t("adminUsersSubtitle")} setNewEmail(e.target.value)} fullWidth /> setNewPassword(e.target.value)} type="password" fullWidth /> {users.length === 0 ? ( {t("adminUsersNoUsers")} ) : ( users.map((u) => ( {u.userName || u.email || u.id} {t("adminUsersRolesLabel")}: {u.roles?.length ? u.roles.join(", ") : "—"} )) )} ); }