import { useContext } from 'react' import { PermissionContext } from './PermissionContext' import { PermissionContextType } from './types' /** * 权限Hook * * @description 提供便捷的权限检查功能 * @version Week 2 Day 7 - 任务17 * * @example * ```tsx * const MyComponent = () => { * const { user, checkModulePermission } = usePermission() * * if (!checkModulePermission('advanced')) { * return * } * * return
欢迎 {user?.name}
* } * ``` */ export const usePermission = (): PermissionContextType => { const context = useContext(PermissionContext) if (context === undefined) { throw new Error( 'usePermission must be used within a PermissionProvider. ' + 'Please wrap your app with .' ) } return context } /** * 导出权限相关类型(方便使用) */ export type { UserInfo, UserVersion, PermissionContextType } from './types'