2026-07-08 15:02:03 +08:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { useState, useTransition } from "react";
|
|
|
|
|
import { useRouter } from "next/navigation";
|
|
|
|
|
import Link from "next/link";
|
|
|
|
|
import { toast } from "sonner";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { assignPermissions } from "../../../actions";
|
|
|
|
|
|
|
|
|
|
export type PermNode = {
|
|
|
|
|
id: number;
|
|
|
|
|
title: string;
|
|
|
|
|
name: string;
|
|
|
|
|
children: PermNode[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function AssignPermissions({
|
|
|
|
|
roleId,
|
|
|
|
|
tree,
|
|
|
|
|
checkedIds,
|
|
|
|
|
}: {
|
|
|
|
|
roleId: number;
|
|
|
|
|
tree: PermNode[];
|
|
|
|
|
checkedIds: number[];
|
|
|
|
|
}) {
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
const [checked, setChecked] = useState<Set<number>>(new Set(checkedIds));
|
|
|
|
|
const [pending, start] = useTransition();
|
|
|
|
|
|
|
|
|
|
function toggle(node: PermNode, on: boolean) {
|
|
|
|
|
const next = new Set(checked);
|
|
|
|
|
const walk = (n: PermNode) => {
|
|
|
|
|
if (on) next.add(n.id);
|
|
|
|
|
else next.delete(n.id);
|
|
|
|
|
n.children.forEach(walk);
|
|
|
|
|
};
|
|
|
|
|
walk(node);
|
|
|
|
|
setChecked(next);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function save() {
|
|
|
|
|
start(async () => {
|
|
|
|
|
const res = await assignPermissions(roleId, [...checked]);
|
|
|
|
|
if (res.ok) {
|
|
|
|
|
toast.success("权限已保存");
|
|
|
|
|
router.push("/rbac/roles");
|
|
|
|
|
router.refresh();
|
|
|
|
|
} else toast.error(res.error ?? "保存失败");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const renderNode = (node: PermNode, depth: number) => (
|
|
|
|
|
<div key={node.id}>
|
|
|
|
|
<label
|
2026-08-29 21:55:10 +08:00
|
|
|
className="flex min-h-11 items-center gap-2.5 rounded-md py-2 pr-2 hover:bg-[var(--color-muted)]"
|
|
|
|
|
style={{ paddingLeft: 8 + depth * 14 }}
|
2026-07-08 15:02:03 +08:00
|
|
|
>
|
|
|
|
|
<input
|
|
|
|
|
type="checkbox"
|
2026-08-29 21:55:10 +08:00
|
|
|
className="h-5 w-5 shrink-0 accent-[var(--color-primary)]"
|
2026-07-08 15:02:03 +08:00
|
|
|
checked={checked.has(node.id)}
|
|
|
|
|
onChange={(e) => toggle(node, e.target.checked)}
|
|
|
|
|
/>
|
2026-08-29 21:55:10 +08:00
|
|
|
<span className="min-w-0">
|
|
|
|
|
<span className="text-sm">{node.title}</span>
|
|
|
|
|
<span className="mt-0.5 block truncate text-xs text-[var(--color-muted-foreground)] sm:ml-1.5 sm:mt-0 sm:inline">
|
|
|
|
|
{node.name}
|
|
|
|
|
</span>
|
|
|
|
|
</span>
|
2026-07-08 15:02:03 +08:00
|
|
|
</label>
|
|
|
|
|
{node.children.map((c) => renderNode(c, depth + 1))}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="space-y-4">
|
|
|
|
|
<div className="rounded-[var(--radius)] border p-2">
|
|
|
|
|
{tree.map((n) => renderNode(n, 0))}
|
|
|
|
|
</div>
|
2026-08-29 21:55:10 +08:00
|
|
|
<div className="sticky bottom-0 z-10 -mx-4 flex gap-2 border-t bg-[var(--color-surface)] px-4 py-3 pb-[max(0.75rem,env(safe-area-inset-bottom))] md:static md:mx-0 md:px-0 md:pb-0 md:pt-4">
|
|
|
|
|
<Button onClick={save} disabled={pending} className="flex-1 sm:flex-none">
|
2026-07-08 15:02:03 +08:00
|
|
|
{pending ? "保存中…" : `保存(已选 ${checked.size} 项)`}
|
|
|
|
|
</Button>
|
|
|
|
|
<Link href="/rbac/roles"><Button variant="outline">取消</Button></Link>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|