Perm.tsx 554 Bytes
"use client";

import type { ReactNode } from "react";

import { usePerms } from "@/lib/usePerms";

/**
 * 按钮/元素级权限包裹:无权限则不渲染 children。
 *   <Perm code="oa:user:add"><button>新增</button></Perm>
 * 传 fallback 可在无权限时渲染占位。
 */
export default function Perm({
  code,
  children,
  fallback = null,
}: {
  code: string;
  children: ReactNode;
  fallback?: ReactNode;
}) {
  const { has, loaded } = usePerms();
  if (!loaded) return null;
  return has(code) ? <>{children}</> : <>{fallback}</>;
}