From f6ae0a9463f61745943f37de5556faea184792ca Mon Sep 17 00:00:00 2001 From: luorijun Date: Wed, 8 Apr 2026 16:47:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=8F=91=E7=8E=AF=E5=A2=83=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E6=9D=83=E9=99=90=E5=A4=8D=E5=88=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/(root)/permissions/page.tsx | 52 +++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) 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" && ( +
+ +
+ )} +
编码