feat: set role shortcut in context menu

This commit is contained in:
Tristan Yang
2023-06-09 16:38:26 +08:00
parent a81c087163
commit ffe4d83c6c
5 changed files with 48 additions and 10 deletions
+9 -7
View File
@@ -17,18 +17,20 @@
"manage_tip": "Disabling your account means you can recover it at any time after taking this action.", "manage_tip": "Disabling your account means you can recover it at any time after taking this action.",
"admin": "Admin", "admin": "Admin",
"user": "User", "user": "User",
"copy_email": "Copy Email", "copy_email": "Copy email",
"send_msg": "Send Message", "send_msg": "Send message",
"call": "Call", "call": "Call",
"remove": "Remove from Server", "remove": "Remove from server",
"remove_from_contact": "Remove From Contact", "remove_from_contact": "Remove from contact",
"remove_account": "Remove Account", "remove_account": "Remove account",
"remove_account_desc": "Are you sure remove this account?", "remove_account_desc": "Are you sure remove this account?",
"remove_from_channel": "Remove From Channel", "remove_from_channel": "Remove from channel",
"set_admin": "Set as admin",
"set_normal": "Set as normal user",
"search_not_found": "Not found, or this user is not allowed to be searched.", "search_not_found": "Not found, or this user is not allowed to be searched.",
"search_by_id": "Search by ID", "search_by_id": "Search by ID",
"search_by_id_ph": "Input user ID", "search_by_id_ph": "Input user ID",
"search_by_email": "Search by Email", "search_by_email": "Search by email",
"search_by_email_ph": "Input user email" "search_by_email_ph": "Input user email"
} }
+2
View File
@@ -24,6 +24,8 @@
"remove_account": "注销账号", "remove_account": "注销账号",
"remove_account_desc": "确定永久注销该账号?", "remove_account_desc": "确定永久注销该账号?",
"remove_from_channel": "从频道移除", "remove_from_channel": "从频道移除",
"set_admin": "设置为管理员",
"set_normal": "设置为普通用户",
"search_not_found": "用户不存在,或者不允许搜索", "search_not_found": "用户不存在,或者不允许搜索",
"search_by_id": "用户ID", "search_by_id": "用户ID",
+9 -1
View File
@@ -29,7 +29,10 @@ const Profile: FC<Props> = ({ uid, type = "embed", cid }) => {
removeFromChannel, removeFromChannel,
canRemoveFromChannel, canRemoveFromChannel,
canRemove, canRemove,
removeUser removeUser,
isAdmin,
canUpdateRole,
updateRole
} = useUserOperation({ uid, cid }); } = useUserOperation({ uid, cid });
const { data } = useAppSelector((store) => { const { data } = useAppSelector((store) => {
@@ -99,6 +102,11 @@ const Profile: FC<Props> = ({ uid, type = "embed", cid }) => {
{t("copy_email")} {t("copy_email")}
</li> </li>
)} )}
{canUpdateRole && (
<li className="item" onClick={updateRole}>
{isAdmin ? t("set_normal") : t("set_admin")}
</li>
)}
{canRemoveFromChannel && ( {canRemoveFromChannel && (
<li className="item danger" onClick={removeFromChannel.bind(null, uid)}> <li className="item danger" onClick={removeFromChannel.bind(null, uid)}>
{t("remove_from_channel")} {t("remove_from_channel")}
+8 -1
View File
@@ -28,7 +28,10 @@ const UserContextMenu: FC<Props> = ({ enable = false, uid, cid, visible, hide, c
canBlock, canBlock,
canRemoveFromChannel, canRemoveFromChannel,
removeFromChannel, removeFromChannel,
removeUser removeUser,
isAdmin,
canUpdateRole,
updateRole
} = useUserOperation({ } = useUserOperation({
uid, uid,
cid cid
@@ -56,6 +59,10 @@ const UserContextMenu: FC<Props> = ({ enable = false, uid, cid, visible, hide, c
title: t("copy_email"), title: t("copy_email"),
handler: copyEmail handler: copyEmail
}, },
canUpdateRole && {
title: isAdmin ? t("set_normal") : t("set_admin"),
handler: updateRole
},
canRemoveFromChannel && { canRemoveFromChannel && {
danger: true, danger: true,
title: t("remove_from_channel"), title: t("remove_from_channel"),
+20 -1
View File
@@ -7,7 +7,11 @@ import { useMatch, useNavigate } from "react-router-dom";
import { hideAll } from "tippy.js"; import { hideAll } from "tippy.js";
import { useRemoveMembersMutation } from "@/app/services/channel"; import { useRemoveMembersMutation } from "@/app/services/channel";
import { useLazyDeleteUserQuery, useUpdateContactStatusMutation } from "@/app/services/user"; import {
useLazyDeleteUserQuery,
useUpdateContactStatusMutation,
useUpdateUserMutation
} from "@/app/services/user";
import { updateDMVisibleAside } from "@/app/slices/footprint"; import { updateDMVisibleAside } from "@/app/slices/footprint";
import { updateCallInfo } from "@/app/slices/voice"; import { updateCallInfo } from "@/app/slices/voice";
import { useAppSelector } from "@/app/store"; import { useAppSelector } from "@/app/store";
@@ -30,6 +34,7 @@ const useUserOperation = ({ uid, cid }: IProps) => {
const [passedUid, setPassedUid] = useState<number | undefined>(undefined); const [passedUid, setPassedUid] = useState<number | undefined>(undefined);
const isUserDetailPath = useMatch(`/users/${uid}`); const isUserDetailPath = useMatch(`/users/${uid}`);
const [updateContactStatus] = useUpdateContactStatusMutation(); const [updateContactStatus] = useUpdateContactStatusMutation();
const [updateUser, { isSuccess: updateUserSuccess }] = useUpdateUserMutation();
const [removeUser, { isSuccess: removeUserSuccess }] = useLazyDeleteUserQuery(); const [removeUser, { isSuccess: removeUserSuccess }] = useLazyDeleteUserQuery();
const [removeInChannel, { isSuccess: removeSuccess }] = useRemoveMembersMutation(); const [removeInChannel, { isSuccess: removeSuccess }] = useRemoveMembersMutation();
const navigateTo = useNavigate(); const navigateTo = useNavigate();
@@ -46,6 +51,12 @@ const useUserOperation = ({ uid, cid }: IProps) => {
setPassedUid(uid ?? loginUser?.uid); setPassedUid(uid ?? loginUser?.uid);
}, [uid, loginUser]); }, [uid, loginUser]);
useEffect(() => {
if (updateUserSuccess) {
toast.success(ct("tip.update"));
}
}, [updateUserSuccess]);
useEffect(() => { useEffect(() => {
if (removeSuccess || removeUserSuccess) { if (removeSuccess || removeUserSuccess) {
toast.success(ct("tip.delete")); toast.success(ct("tip.delete"));
@@ -99,6 +110,11 @@ const useUserOperation = ({ uid, cid }: IProps) => {
const startChat = () => { const startChat = () => {
navigateTo(`/chat/dm/${uid}`); navigateTo(`/chat/dm/${uid}`);
}; };
const updateRole = () => {
if (uid) {
updateUser({ id: uid, is_admin: !user?.is_admin });
}
};
const removeFromContact = () => { const removeFromContact = () => {
if (uid) { if (uid) {
updateContactStatus({ target_uid: uid, action: "remove" }); updateContactStatus({ target_uid: uid, action: "remove" });
@@ -126,6 +142,9 @@ const useUserOperation = ({ uid, cid }: IProps) => {
const canRemoveFromContact: boolean = loginUid != uid; const canRemoveFromContact: boolean = loginUid != uid;
const canInviteChannel = !!cid && (loginUser?.is_admin || channel?.owner == loginUser?.uid); const canInviteChannel = !!cid && (loginUser?.is_admin || channel?.owner == loginUser?.uid);
return { return {
isAdmin: !!user?.is_admin,
updateRole,
canUpdateRole: isAdmin && loginUid != uid && uid != 1,
removeFromContact, removeFromContact,
canBlock, canBlock,
canRemoveFromContact, canRemoveFromContact,