| 1 |
/** |
| 2 |
* Conditional Render Component |
| 3 |
* Renders children based on permissions and Pro version status |
| 4 |
* Supports extensibility for Pro version modifications |
| 5 |
*/ |
| 6 |
|
| 7 |
import React from "react"; |
| 8 |
import { usePermissions } from "../../hooks/usePermissions"; |
| 9 |
|
| 10 |
interface ConditionalRenderProps { |
| 11 |
children: React.ReactNode; |
| 12 |
capability?: string; |
| 13 |
role?: string; |
| 14 |
requirePro?: boolean; |
| 15 |
fallback?: React.ReactNode; |
| 16 |
className?: string; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Conditionally renders content based on permissions, roles, or Pro status |
| 21 |
* |
| 22 |
* @example |
| 23 |
* <ConditionalRender capability="yatra_edit_trips"> |
| 24 |
* <EditButton /> |
| 25 |
* </ConditionalRender> |
| 26 |
* |
| 27 |
* @example |
| 28 |
* <ConditionalRender requirePro fallback={<UpgradeMessage />}> |
| 29 |
* <ProFeature /> |
| 30 |
* </ConditionalRender> |
| 31 |
*/ |
| 32 |
export const ConditionalRender: React.FC<ConditionalRenderProps> = ({ |
| 33 |
children, |
| 34 |
capability, |
| 35 |
role, |
| 36 |
requirePro = false, |
| 37 |
fallback = null, |
| 38 |
className = "", |
| 39 |
}) => { |
| 40 |
const { can, hasRole, isPro } = usePermissions(); |
| 41 |
|
| 42 |
// Check if should render |
| 43 |
let shouldRender = true; |
| 44 |
|
| 45 |
if (capability && !can(capability)) { |
| 46 |
shouldRender = false; |
| 47 |
} |
| 48 |
|
| 49 |
if (role && !hasRole(role)) { |
| 50 |
shouldRender = false; |
| 51 |
} |
| 52 |
|
| 53 |
if (requirePro && !isPro) { |
| 54 |
shouldRender = false; |
| 55 |
} |
| 56 |
|
| 57 |
if (!shouldRender) { |
| 58 |
return <>{fallback}</>; |
| 59 |
} |
| 60 |
|
| 61 |
return <div className={className}>{children}</div>; |
| 62 |
}; |
| 63 |
|
| 64 |
export default ConditionalRender; |
| 65 |
|