| 1 | import { useState } from "react"; |
| 2 | import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; |
| 3 | import { api } from "../api"; |
| 4 | import { S } from "../i18n"; |
| 5 | import { toast } from "../toast"; |
| 6 | import { Dropdown, Pager, pageSlice, SearchSelect } from "../ui"; |
| 7 | |
| 8 | const ROLES = ["owner", "admin", "editor", "viewer"] as const; |
| 9 | const ROLE_OPTIONS = ROLES.map((r) => ({ value: r, label: S.members.roles[r] })); |
| 10 | const MEMBER_PAGE = 10; |
| 11 | |
| 12 | export function Members({ workspaceId }: { workspaceId: string }) { |
| 13 | const queryClient = useQueryClient(); |
| 14 | const [addUserId, setAddUserId] = useState(""); |
| 15 | const [addRole, setAddRole] = useState("viewer"); |
| 16 | const [memberPage, setMemberPage] = useState(0); |
| 17 | const [filter, setFilter] = useState(""); |
| 18 | const me = useQuery({ queryKey: ["me"], queryFn: api.me }); |
| 19 | const [error, setError] = useState<string | null>(null); |
| 20 | |
| 21 | const members = useQuery({ |
| 22 | queryKey: ["members", workspaceId], |
| 23 | queryFn: () => api.members(workspaceId), |
| 24 | }); |
| 25 | const orgUsers = useQuery({ queryKey: ["orgUsers"], queryFn: api.orgUsers }); |
| 26 | |
| 27 | const refresh = () => { |
| 28 | setError(null); |
| 29 | queryClient.invalidateQueries({ queryKey: ["members", workspaceId] }); |
| 30 | }; |
| 31 | const onError = (e: unknown) => setError((e as Error).message); |
| 32 | |
| 33 | const setRole = useMutation({ |
| 34 | mutationFn: ({ userId, role }: { userId: string; role: string }) => |
| 35 | api.setMemberRole(workspaceId, userId, role), |
| 36 | onSuccess: refresh, |
| 37 | onError, |
| 38 | }); |
| 39 | const remove = useMutation({ |
| 40 | mutationFn: (userId: string) => api.removeMember(workspaceId, userId), |
| 41 | onSuccess: refresh, |
| 42 | onError, |
| 43 | }); |
| 44 | const deactivate = useMutation({ |
| 45 | mutationFn: (userId: string) => api.adminDeactivateUser(userId), |
| 46 | onSuccess: refresh, |
| 47 | onError, |
| 48 | }); |
| 49 | |
| 50 | const memberIds = new Set(members.data?.map((m) => m.user_id)); |
| 51 | const addable = orgUsers.data?.filter((u) => !memberIds.has(u.id)) ?? []; |
| 52 | const q = filter.trim().toLowerCase(); |
| 53 | const memberList = (members.data ?? []).filter( |
| 54 | (m) => |
| 55 | !q || m.display_name.toLowerCase().includes(q) || m.email.toLowerCase().includes(q), |
| 56 | ); |
| 57 | const { rows: pagedMembers, safe: safeMemberPage } = pageSlice(memberList, memberPage, MEMBER_PAGE); |
| 58 | |
| 59 | return ( |
| 60 | <div className="glass rounded-xl p-5"> |
| 61 | <div className="flex items-center gap-3 mb-3"> |
| 62 | <h3 className="text-sm font-bold text-neutral-200">{S.members.title}</h3> |
| 63 | <input |
| 64 | className="input-dark ml-auto w-56 px-2.5 py-1 text-xs" |
| 65 | placeholder={S.settings.searchUsers} |
| 66 | value={filter} |
| 67 | onChange={(e) => { |
| 68 | setFilter(e.target.value); |
| 69 | setMemberPage(0); |
| 70 | }} |
| 71 | /> |
| 72 | </div> |
| 73 | |
| 74 | {error && <p className="mb-3 text-sm text-rose-400">{error}</p>} |
| 75 | |
| 76 | <table className="w-full text-sm"> |
| 77 | <tbody> |
| 78 | {pagedMembers.map((m) => ( |
| 79 | <tr key={m.user_id} className="border-b border-white/5"> |
| 80 | <td className="py-2 pr-3"> |
| 81 | <div className="text-neutral-200"> |
| 82 | {m.display_name} |
| 83 | {m.is_admin && ( |
| 84 | <span className="ml-1.5 rounded bg-[rgba(74,163,255,0.12)] px-1.5 py-0.5 text-[10px] text-[var(--u-accent)]"> |
| 85 | {S.members.systemAdmin} |
| 86 | </span> |
| 87 | )} |
| 88 | </div> |
| 89 | <div className="text-xs text-neutral-500">{m.email}</div> |
| 90 | </td> |
| 91 | <td className="py-2 pr-3 text-right"> |
| 92 | <Dropdown |
| 93 | size="sm" |
| 94 | className="w-24 ml-auto" |
| 95 | value={m.role} |
| 96 | onChange={(role) => setRole.mutate({ userId: m.user_id, role })} |
| 97 | options={ROLE_OPTIONS} |
| 98 | /> |
| 99 | </td> |
| 100 | <td className="py-2 text-right whitespace-nowrap"> |
| 101 | <button |
| 102 | onClick={() => remove.mutate(m.user_id)} |
| 103 | className="text-xs text-neutral-500 hover:text-rose-400" |
| 104 | > |
| 105 | {S.members.remove} |
| 106 | </button> |
| 107 | {/* 停用账号跟「移出工作区」是两件事:前者断掉整个系统的访问, |
| 108 | 后者只是这个工作区不再有他。所以分开两个按钮,而且停用 |
| 109 | 只给管理员看——它的影响面大得多 */} |
| 110 | {me.data?.is_admin && me.data.id !== m.user_id && ( |
| 111 | <button |
| 112 | onClick={() => { |
| 113 | if (confirm(S.members.deactivateConfirm(m.display_name))) |
| 114 | deactivate.mutate(m.user_id); |
| 115 | }} |
| 116 | className="ml-3 text-xs text-neutral-600 hover:text-rose-400" |
| 117 | title={S.members.deactivateHint} |
| 118 | > |
| 119 | {S.members.deactivate} |
| 120 | </button> |
| 121 | )} |
| 122 | </td> |
| 123 | </tr> |
| 124 | ))} |
| 125 | </tbody> |
| 126 | </table> |
| 127 | <div className="mb-4"> |
| 128 | <Pager |
| 129 | total={memberList.length} |
| 130 | pageSize={MEMBER_PAGE} |
| 131 | page={safeMemberPage} |
| 132 | onPage={setMemberPage} |
| 133 | /> |
| 134 | </div> |
| 135 | |
| 136 | {/* picker 常驻。理由同 KbSettings 里那段:控件消失读作"坏了", |
| 137 | 而不是"没人可加";空列表 SearchSelect 自己会说 */} |
| 138 | <div className="flex gap-2 items-center"> |
| 139 | <SearchSelect |
| 140 | className="flex-1" |
| 141 | value={addUserId} |
| 142 | onChange={setAddUserId} |
| 143 | placeholder={S.members.pickUser} |
| 144 | options={addable.map((u) => ({ |
| 145 | value: u.id, |
| 146 | label: u.display_name, |
| 147 | hint: u.email, |
| 148 | }))} |
| 149 | /> |
| 150 | <Dropdown |
| 151 | className="w-28" |
| 152 | value={addRole} |
| 153 | onChange={setAddRole} |
| 154 | options={ROLE_OPTIONS} |
| 155 | /> |
| 156 | <button |
| 157 | onClick={() => addUserId && setRole.mutate({ userId: addUserId, role: addRole })} |
| 158 | disabled={!addUserId} |
| 159 | className="u-btn u-btn-primary px-3 py-1.5 text-sm" |
| 160 | > |
| 161 | {S.members.add} |
| 162 | </button> |
| 163 | </div> |
| 164 | |
| 165 | {me.data?.is_admin && <CreateUser onCreated={refresh} />} |
| 166 | {me.data?.is_admin && <DeactivatedUsers onChanged={refresh} />} |
| 167 | </div> |
| 168 | ); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /** 已停用的账号,以及恢复它们。 |
| 173 | * |
| 174 | * **这一块存在的理由是「否则恢复够不着」**:停用之后那个人从成员表、选人器、 |
| 175 | * 每一个列表里消失,管理员拿不到他的 id,而恢复接口要的正是那个 id。 |
| 176 | * |
| 177 | * 一个都没有时整块不出现——没有停用过的部署不该看到一个永远空的区块。 |
| 178 | */ |
| 179 | function DeactivatedUsers({ onChanged }: { onChanged: () => void }) { |
| 180 | const list = useQuery({ |
| 181 | queryKey: ["deactivatedUsers"], |
| 182 | queryFn: api.deactivatedUsers, |
| 183 | }); |
| 184 | const queryClient = useQueryClient(); |
| 185 | const revive = useMutation({ |
| 186 | mutationFn: (userId: string) => api.adminReactivateUser(userId), |
| 187 | onSuccess: () => { |
| 188 | queryClient.invalidateQueries({ queryKey: ["deactivatedUsers"] }); |
| 189 | onChanged(); |
| 190 | }, |
| 191 | onError: (e: Error) => toast.error(e.message), |
| 192 | }); |
| 193 | |
| 194 | const users = list.data ?? []; |
| 195 | if (users.length === 0) return null; |
| 196 | |
| 197 | return ( |
| 198 | <div className="mt-6"> |
| 199 | <h4 className="text-[13px] text-neutral-300"> |
| 200 | {S.members.deactivatedTitle} |
| 201 | </h4> |
| 202 | <p className="mt-0.5 text-[11px] leading-relaxed text-neutral-500"> |
| 203 | {S.members.deactivatedHint} |
| 204 | </p> |
| 205 | <div className="mt-2 space-y-1"> |
| 206 | {users.map((u) => ( |
| 207 | <div |
| 208 | key={u.id} |
| 209 | className="flex items-center gap-2 rounded border border-white/10 px-2.5 py-1.5" |
| 210 | > |
| 211 | <span className="text-[13px] text-neutral-300"> |
| 212 | {u.display_name} |
| 213 | </span> |
| 214 | <span className="text-[11px] text-neutral-600">{u.email}</span> |
| 215 | <button |
| 216 | className="ml-auto u-btn u-btn-ghost px-2 py-0.5 text-xs" |
| 217 | disabled={revive.isPending} |
| 218 | onClick={() => revive.mutate(u.id)} |
| 219 | > |
| 220 | {S.members.reactivate} |
| 221 | </button> |
| 222 | </div> |
| 223 | ))} |
| 224 | </div> |
| 225 | </div> |
| 226 | ); |
| 227 | } |
| 228 | /** 管理员代开账号(注册关闭后的唯一入口)。 */ |
| 229 | function CreateUser({ onCreated }: { onCreated: () => void }) { |
| 230 | const queryClient = useQueryClient(); |
| 231 | const [email, setEmail] = useState(""); |
| 232 | const [name, setName] = useState(""); |
| 233 | const [password, setPassword] = useState(""); |
| 234 | const [role, setRole] = useState("editor"); |
| 235 | const [error, setError] = useState<string | null>(null); |
| 236 | |
| 237 | const create = useMutation({ |
| 238 | mutationFn: () => |
| 239 | api.adminCreateUser({ email: email.trim(), display_name: name.trim(), password, role }), |
| 240 | onSuccess: () => { |
| 241 | setEmail(""); |
| 242 | setName(""); |
| 243 | setPassword(""); |
| 244 | setError(null); |
| 245 | queryClient.invalidateQueries({ queryKey: ["orgUsers"] }); |
| 246 | onCreated(); |
| 247 | }, |
| 248 | onError: (e) => setError((e as Error).message), |
| 249 | }); |
| 250 | |
| 251 | const valid = email.includes("@") && name.trim() && password.length >= 8; |
| 252 | |
| 253 | return ( |
| 254 | <div className="mt-5 border-t border-white/10 pt-4"> |
| 255 | <h4 className="text-xs font-bold text-neutral-400 mb-2">{S.settings.newUser}</h4> |
| 256 | <div className="grid grid-cols-2 gap-2 mb-2"> |
| 257 | <input |
| 258 | className="input-dark px-3 py-2 text-sm" |
| 259 | placeholder={S.login.email} |
| 260 | value={email} |
| 261 | onChange={(e) => setEmail(e.target.value)} |
| 262 | /> |
| 263 | <input |
| 264 | className="input-dark px-3 py-2 text-sm" |
| 265 | placeholder={S.login.displayName} |
| 266 | value={name} |
| 267 | onChange={(e) => setName(e.target.value)} |
| 268 | /> |
| 269 | <input |
| 270 | className="input-dark px-3 py-2 text-sm" |
| 271 | type="password" |
| 272 | placeholder={S.settings.initialPassword} |
| 273 | value={password} |
| 274 | onChange={(e) => setPassword(e.target.value)} |
| 275 | /> |
| 276 | <Dropdown |
| 277 | value={role} |
| 278 | onChange={setRole} |
| 279 | options={[ |
| 280 | { value: "admin", label: S.members.roles.admin }, |
| 281 | { value: "editor", label: S.members.roles.editor }, |
| 282 | { value: "viewer", label: S.members.roles.viewer }, |
| 283 | ]} |
| 284 | /> |
| 285 | </div> |
| 286 | <div className="flex items-center gap-3"> |
| 287 | <button |
| 288 | className="u-btn u-btn-primary px-3.5 py-1.5 text-xs" |
| 289 | disabled={!valid || create.isPending} |
| 290 | onClick={() => create.mutate()} |
| 291 | > |
| 292 | {S.settings.createUserBtn} |
| 293 | </button> |
| 294 | {error && <p className="text-xs text-rose-400">{error}</p>} |
| 295 | </div> |
| 296 | </div> |
| 297 | ); |
| 298 | } |