1 import { useContext } from "react"
2 import { PermissionsContext } from "./permissions-context"
3
4 /**
5 * Hook to access permission checking utilities.
6 *
7 * @returns PermissionsContextValue with permission checking methods
8 * @throws Error if used outside of PermissionsProvider
9 *
10 * @example
11 * ```tsx
12 * const { can, hasPermission } = usePermissions()
13 *
14 * // Check using resource and operation
15 * if (can("customer", "create")) {
16 * // Show create button
17 * }
18 *
19 * // Check using permission string
20 * if (hasPermission("customer:read")) {
21 * // Show customer list
22 * }
23 * ```
24 */
25 export const usePermissions = () => {
26 const context = useContext(PermissionsContext)
27
28 if (!context) {
29 throw new Error(
30 "usePermissions must be used within a PermissionsProvider"
31 )
32 }
33
34 return context
35 }