mirror of
https://github.com/SamyRai/turash.git
synced 2025-12-26 23:01:33 +00:00
Some checks failed
CI/CD Pipeline / backend-lint (push) Failing after 31s
CI/CD Pipeline / backend-build (push) Has been skipped
CI/CD Pipeline / frontend-lint (push) Failing after 1m22s
CI/CD Pipeline / frontend-build (push) Has been skipped
CI/CD Pipeline / e2e-test (push) Has been skipped
- Fix React Compiler memoization issues in useOrganizationPage.ts - Replace useCallback with useRef pattern in useKeyboard.ts - Remove unnecessary dependencies from useMemo hooks - Fix prettier formatting in api-client.ts and api-config.ts - Replace any types with proper types in error-handling, http-client, security - Remove unused imports and variables - Move ImpactBreakdownChart component outside render in ImpactMetrics.tsx - Fix setState in effect by using useMemo in HeritageBuildingPage.tsx - Memoize getHistoryTitle with useCallback in MatchDetailPage and MatchNegotiationPage - Add i18n for literal strings in community pages and LoginPage - Fix missing dependencies in DashboardPage and DiscoveryPage
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { usePermissions } from '@/hooks/usePermissions';
|
|
import { Permission } from '@/types/permissions';
|
|
import React from 'react';
|
|
import { useTranslation } from '@/hooks/useI18n.tsx';
|
|
|
|
export interface PermissionGateProps {
|
|
children: React.ReactNode;
|
|
permission: Permission | Permission[];
|
|
requireAll?: boolean;
|
|
fallback?: React.ReactNode;
|
|
showError?: boolean;
|
|
}
|
|
|
|
/**
|
|
* Component that conditionally renders children based on permissions
|
|
* Use this for hiding/showing UI elements based on permissions
|
|
*/
|
|
export const PermissionGate = ({
|
|
children,
|
|
permission,
|
|
requireAll = false,
|
|
fallback = null,
|
|
showError = false,
|
|
}: PermissionGateProps) => {
|
|
const { t } = useTranslation();
|
|
const { checkAnyPermission, checkAllPermissions } = usePermissions();
|
|
|
|
const permissions = Array.isArray(permission) ? permission : [permission];
|
|
const hasAccess = requireAll ? checkAllPermissions(permissions) : checkAnyPermission(permissions);
|
|
|
|
if (!hasAccess) {
|
|
if (showError) {
|
|
return (
|
|
<div className="text-sm text-destructive">
|
|
{t('permissionGate.noPermission')}
|
|
</div>
|
|
);
|
|
}
|
|
return <>{fallback}</>;
|
|
}
|
|
|
|
return <>{children}</>;
|
|
};
|