diff --git a/monkeyai/admin/src/components/group-manager.tsx b/monkeyai/admin/src/components/group-manager.tsx
deleted file mode 100644
index 092189de..00000000
--- a/monkeyai/admin/src/components/group-manager.tsx
+++ /dev/null
@@ -1,250 +0,0 @@
-import { useCallback, useEffect, useState } from "react"
-import { Button } from "@/components/ui/button"
-import {
- Dialog,
- DialogContent,
- DialogDescription,
- DialogHeader,
- DialogTitle,
-} from "@/components/ui/dialog"
-import { Input } from "@/components/ui/input"
-import { Field, FieldLabel } from "@/components/ui/field"
-import { api } from "@/lib/api"
-
-type Group = { id: string; parent_id: string | null; name: string }
-type Groups = {
- groups: Group[]
- memberships: { group_id: string; user_id: string }[]
-}
-type User = { id: string; name: string; email: string }
-const rootID = "00000000-0000-0000-0000-000000000001"
-const adminID = "00000000-0000-0000-0000-000000000002"
-const selectClass = "h-9 w-full rounded-md border bg-background px-3 text-sm"
-
-export function GroupManager() {
- const [open, setOpen] = useState(false)
- return (
- <>
-
- {open && setOpen(false)} />}
- >
- )
-}
-function GroupEditor({ onClose }: { onClose: () => void }) {
- const [data, setData] = useState(null)
- const [users, setUsers] = useState([])
- const [id, setID] = useState("")
- const [name, setName] = useState("")
- const [parent, setParent] = useState(rootID)
- const [members, setMembers] = useState([])
- const [busy, setBusy] = useState(false)
- const [error, setError] = useState("")
- const [notice, setNotice] = useState("")
- const load = useCallback(
- () =>
- Promise.all([
- api("/api/admin/v1/groups"),
- api<{ users: User[] }>("/api/admin/v1/users"),
- ]).then(([g, u]) => {
- setData(g)
- setUsers(u.users)
- }),
- []
- )
- useEffect(() => {
- void load().catch((e: Error) => setError(e.message))
- }, [load])
- const select = (value: string) => {
- const g = data?.groups.find((item) => item.id === value)
- setID(value)
- setName(g?.name ?? "")
- setParent(g?.parent_id ?? rootID)
- setMembers(
- data?.memberships
- .filter((m) => m.group_id === value)
- .map((m) => m.user_id) ?? []
- )
- setError("")
- setNotice("")
- }
- const save = async (action: "group" | "members" | "delete") => {
- setBusy(true)
- setError("")
- setNotice("")
- try {
- if (action === "group") {
- const result = await api<{ id: string }>(
- `/api/admin/v1/groups${id ? `/${id}` : ""}`,
- {
- method: id ? "PATCH" : "POST",
- body: JSON.stringify({ name, parent_id: parent }),
- }
- )
- setID(result.id)
- }
- if (action === "members")
- await api(`/api/admin/v1/groups/${id}/members`, {
- method: "PUT",
- body: JSON.stringify({ user_ids: members }),
- })
- if (action === "delete") {
- await api(`/api/admin/v1/groups/${id}`, { method: "DELETE" })
- setID("")
- setName("")
- setMembers([])
- }
- await load()
- setNotice(action === "delete" ? "空分组已删除" : "分组设置已保存")
- } catch (e) {
- setError((e as Error).message)
- } finally {
- setBusy(false)
- }
- }
- const fixed = id === rootID || id === adminID
- return (
-
- )
-}
diff --git a/monkeyai/admin/src/components/members/group-action-dialog.tsx b/monkeyai/admin/src/components/members/group-action-dialog.tsx
new file mode 100644
index 00000000..4a81b9f6
--- /dev/null
+++ b/monkeyai/admin/src/components/members/group-action-dialog.tsx
@@ -0,0 +1,304 @@
+import { useState, type FormEvent } from "react"
+import { useTranslation } from "react-i18next"
+
+import { Button } from "@/components/ui/button"
+import { Checkbox } from "@/components/ui/checkbox"
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "@/components/ui/dialog"
+import { Field, FieldLabel } from "@/components/ui/field"
+import { Input } from "@/components/ui/input"
+import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
+import { api } from "@/lib/api"
+import {
+ ROOT_GROUP_ID,
+ descendantIDs,
+ type MemberGroup,
+} from "@/lib/member-groups"
+import { groupActionKeys, type ActiveGroupAction } from "@/lib/member-groups"
+
+const submitKeys = {
+ "add-subgroup": "createGroup",
+ rename: "saveChanges",
+ "adjust-members": "applyChanges",
+ move: "moveAction",
+ delete: "deleteAction",
+}
+
+export function GroupActionDialog({
+ action,
+ group,
+ groups,
+ users,
+ onClose,
+ onSaved,
+}: ActiveGroupAction & {
+ groups: MemberGroup[]
+ users: Array<{ id: string; name: string; email: string }>
+ onClose: () => void
+ onSaved: (group: MemberGroup | null) => void
+}) {
+ const { t } = useTranslation()
+ const [name, setName] = useState(action === "rename" ? group.name : "")
+ const [memberIDs, setMemberIDs] = useState(group.member_ids)
+ const [query, setQuery] = useState("")
+ const [parentID, setParentID] = useState("")
+ const [saving, setSaving] = useState(false)
+ const [error, setError] = useState("")
+ const excluded = descendantIDs(groups, group.id)
+ const targets = groups.filter(
+ (candidate) =>
+ !excluded.has(candidate.id) && candidate.id !== group.parent_id
+ )
+ const normalized = query.trim().toLocaleLowerCase()
+ const visible = users.filter((user) =>
+ `${user.name} ${user.email}`.toLocaleLowerCase().includes(normalized)
+ )
+ const titleKey = groupActionKeys[action]
+ const hasChildren = groups.some(
+ (candidate) => candidate.parent_id === group.id
+ )
+
+ const targetPath = (candidate: MemberGroup) => {
+ const names = [candidate.name]
+ const visited = new Set([candidate.id])
+ let parent = groups.find((item) => item.id === candidate.parent_id)
+ while (parent && !visited.has(parent.id)) {
+ names.unshift(parent.name)
+ visited.add(parent.id)
+ parent = groups.find((item) => item.id === parent?.parent_id)
+ }
+ return names.join(" / ")
+ }
+
+ const submit = async (event: FormEvent) => {
+ event.preventDefault()
+ if (saving) return
+ setSaving(true)
+ setError("")
+ try {
+ const path = `/api/admin/v1/groups/${group.id}`
+ let updated: MemberGroup | null = null
+ if (action === "delete") {
+ await api(path, { method: "DELETE" })
+ } else if (action === "add-subgroup") {
+ updated = await api("/api/admin/v1/groups", {
+ method: "POST",
+ body: JSON.stringify({
+ name: name.trim(),
+ parent_id: group.id === ROOT_GROUP_ID ? null : group.id,
+ }),
+ })
+ } else if (action === "adjust-members") {
+ updated = await api(`${path}/members`, {
+ method: "PUT",
+ body: JSON.stringify({ member_ids: memberIDs }),
+ })
+ } else {
+ updated = await api(path, {
+ method: "PATCH",
+ body: JSON.stringify(
+ action === "rename"
+ ? { name: name.trim() }
+ : { parent_id: parentID === ROOT_GROUP_ID ? null : parentID }
+ ),
+ })
+ }
+ onSaved(updated)
+ } catch (reason) {
+ setError((reason as Error).message)
+ } finally {
+ setSaving(false)
+ }
+ }
+
+ return (
+
+ )
+}
diff --git a/monkeyai/admin/src/components/members/group-tree.tsx b/monkeyai/admin/src/components/members/group-tree.tsx
new file mode 100644
index 00000000..f361cb3d
--- /dev/null
+++ b/monkeyai/admin/src/components/members/group-tree.tsx
@@ -0,0 +1,187 @@
+import { useState } from "react"
+import {
+ Add01Icon,
+ Delete02Icon,
+ Edit02Icon,
+ Folder02Icon,
+ FolderIcon,
+ MoreHorizontalIcon,
+ MoveIcon,
+ UserMultiple02Icon,
+} from "@hugeicons/core-free-icons"
+import { HugeiconsIcon } from "@hugeicons/react"
+import { useTranslation } from "react-i18next"
+
+import { Button } from "@/components/ui/button"
+import {
+ Collapsible,
+ CollapsibleContent,
+ CollapsibleTrigger,
+} from "@/components/ui/collapsible"
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuGroup,
+ DropdownMenuItem,
+ DropdownMenuSeparator,
+ DropdownMenuTrigger,
+} from "@/components/ui/dropdown-menu"
+import {
+ groupActionKeys,
+ type GroupAction,
+ type ActiveGroupAction,
+ ROOT_GROUP_ID,
+ groupMemberIDs,
+ descendantIDs,
+ type MemberGroup,
+} from "@/lib/member-groups"
+import { cn } from "@/lib/utils"
+
+const actionIcons = {
+ "add-subgroup": Add01Icon,
+ rename: Edit02Icon,
+ "adjust-members": UserMultiple02Icon,
+ move: MoveIcon,
+ delete: Delete02Icon,
+}
+
+type Props = {
+ group: MemberGroup
+ groups: MemberGroup[]
+ users: Array<{ id: string; role: "admin" | "user" }>
+ selectedID: string
+ onSelect: (id: string) => void
+ onAction: (action: ActiveGroupAction) => void
+ level?: number
+}
+
+export function GroupTreeItem({
+ group,
+ groups,
+ users,
+ selectedID,
+ onSelect,
+ onAction,
+ level = 0,
+}: Props) {
+ const { t } = useTranslation()
+ const [expanded, setExpanded] = useState(true)
+ const open =
+ expanded ||
+ (selectedID !== group.id && descendantIDs(groups, group.id).has(selectedID))
+ const children = groups.filter((child) => child.parent_id === group.id)
+ const isRoot = group.id === ROOT_GROUP_ID
+ const actions: GroupAction[] = isRoot
+ ? ["add-subgroup"]
+ : ["add-subgroup", "rename", "adjust-members", "move", "delete"]
+ const count = groupMemberIDs(groups, users, group.id).size
+ const label = (
+ <>
+
+ {group.name}
+ >
+ )
+ const selectButton = (
+
)}
+ {loading && (
+
+ {t("common.loading")}
+
+ )}
{visibleUsers.map((user) => {
const isDisabled = user.status === "disabled"
@@ -378,7 +386,7 @@ export function MembersAndGroupsPage() {
)
})}
- {visibleUsers.length === 0 && (
+ {!loading && visibleUsers.length === 0 && (
{t("pages.membersAndGroups.noMembersFound")}
@@ -388,6 +396,40 @@ export function MembersAndGroupsPage() {
+ {activeGroupAction && (
+ setActiveGroupAction(null)}
+ onSaved={(updated) => {
+ if (updated) {
+ setGroups((current) =>
+ current.some((group) => group.id === updated.id)
+ ? current.map((group) =>
+ group.id === updated.id ? updated : group
+ )
+ : [...current, updated]
+ )
+ setSelectedGroupID(updated.id)
+ } else {
+ setGroups((current) =>
+ current.filter(
+ (group) => group.id !== activeGroupAction.group.id
+ )
+ )
+ if (selectedGroupID === activeGroupAction.group.id)
+ setSelectedGroupID(
+ activeGroupAction.group.parent_id ?? ROOT_GROUP_ID
+ )
+ }
+ setError("")
+ setActiveGroupAction(null)
+ }}
+ />
+ )}
+