diff --git a/src/app/(root)/permissions/page.tsx b/src/app/(root)/permissions/page.tsx index f53b822..179df15 100644 --- a/src/app/(root)/permissions/page.tsx +++ b/src/app/(root)/permissions/page.tsx @@ -5,9 +5,11 @@ import { type Row, useReactTable, } from "@tanstack/react-table" +import { Copy } from "lucide-react" import { Suspense, useCallback, useEffect, useState } from "react" import { toast } from "sonner" import { getAllPermissions } from "@/actions/permission" +import { Button } from "@/components/ui/button" import { Table, TableBody, @@ -25,6 +27,34 @@ type Node = { children: Node[] } +function toConstName(name: string): string { + return ( + "Scope" + + name + .split(/[:_]/) + .map(s => s.charAt(0).toUpperCase() + s.slice(1)) + .join("") + ) +} + +function generateScopeCode(roots: Node[]): string { + function generateNode(node: Node, isRoot: boolean): string[] { + const lines: string[] = [] + if (isRoot) { + lines.push(`// ${node.description}`) + } + const constName = toConstName(node.name) + const comment = isRoot ? "" : ` // ${node.description}` + lines.push(`export const ${constName} = "${node.name}"${comment}`) + for (const child of node.children) { + lines.push(...generateNode(child, false)) + } + return lines + } + + return roots.map(root => generateNode(root, true).join("\n")).join("\n\n") +} + export default function PermissionsPage() { return ( @@ -95,9 +125,27 @@ function PermissionTable() { refresh() }, [refresh]) + const handleCopy = useCallback(async () => { + try { + const code = generateScopeCode(data) + await navigator.clipboard.writeText(code) + toast.success("已复制权限代码到剪贴板") + } catch (e) { + toast.error(e instanceof Error ? e.message : "复制失败") + } + }, [data]) + return ( -
- +
+ {process.env.NODE_ENV === "development" && ( +
+ +
+ )} +
编码