PermissionButton.tsx 746 B

12345678910111213141516171819202122232425262728
  1. import { Button } from "@contentful/f36-components";
  2. import { useAuth } from "@/contexts/AuthContext";
  3. import type { ComponentProps } from "react";
  4. interface PermissionButtonProps extends Omit<ComponentProps<typeof Button>, "children"> {
  5. menuId: string;
  6. action: string; // 'add' | 'edit' | 'delete' | 'view' | 'export' | 'import' | ...
  7. children: React.ReactNode;
  8. }
  9. /**
  10. * 带权限控制的按钮组件
  11. * 如果用户没有权限,按钮将不会渲染
  12. */
  13. export default function PermissionButton({
  14. menuId,
  15. action,
  16. children,
  17. ...buttonProps
  18. }: PermissionButtonProps) {
  19. const { hasPermission } = useAuth();
  20. if (!hasPermission(menuId, action)) {
  21. return null;
  22. }
  23. return <Button {...buttonProps}>{children}</Button>;
  24. }