Raw
1 import { useEffect } from "react"
2 import { useNavigate } from "react-router-dom"
3
4 import { useFeatureFlag } from "../providers/feature-flag-provider"
5
6 /**
7 * Guards a route/component on the `rbac` feature flag. When the flag is
8 * disabled the consumer is navigated back. Returns the boolean so callers can
9 * short-circuit JSX (and reuse it in other gates like react-query's
10 * `enabled` option) without redeclaring the flag.
11 *
12 * @example
13 * ```tsx
14 * export const RoleList = () => {
15 * const isRbacEnabled = useRequireRbacFeature()
16 * if (!isRbacEnabled) return null
17 * return <RoleListTable />
18 * }
19 * ```
20 */
21 export const useRequireRbacFeature = (): boolean => {
22 const isRbacEnabled = useFeatureFlag("rbac")
23 const navigate = useNavigate()
24
25 useEffect(() => {
26 if (!isRbacEnabled) {
27 navigate(-1)
28 }
29 }, [isRbacEnabled, navigate])
30
31 return isRbacEnabled
32 }