开发环境新增权限复制功能
This commit is contained in:
@@ -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 (
|
||||
<Suspense>
|
||||
@@ -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 (
|
||||
<div className="bg-background rounded-lg">
|
||||
<Table>
|
||||
<div className="flex flex-col gap-3">
|
||||
{process.env.NODE_ENV === "development" && (
|
||||
<div>
|
||||
<Button variant="outline" size="sm" onClick={handleCopy}>
|
||||
<Copy className="mr-2 h-4 w-4" />
|
||||
复制权限代码
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
<Table className="bg-background rounded-lg">
|
||||
<TableHeader>
|
||||
<TableRow className="h-10">
|
||||
<TableHead>编码</TableHead>
|
||||
|
||||
Reference in New Issue
Block a user