/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import { useContext, useEffect } from 'react'; import { AuthzContext, currentOrgViewKey } from './AuthzProvider'; import { checkPerm, checkAnyPerm, viewForCode } from './permLogic'; // usePerm(code, legacy):模块已切到新体系时按权限点判断,否则按 legacy(旧判断)判断。 export function usePerm(code, legacy) { const ctx = useContext(AuthzContext); return checkPerm(viewForCode(ctx, code), code, legacy); } // useAnyPerm(codes, legacy):持有其中任一权限点。 export function useAnyPerm(codes, legacy) { const ctx = useContext(AuthzContext); if (!codes.length) return false; return checkAnyPerm(viewForCode(ctx, codes[0]), codes, legacy); } // useAuthzView(domain):'platform' | 'org' 的原始视图(菜单树渲染等)。 export function useAuthzView(domain) { const ctx = useContext(AuthzContext); return domain === 'org' ? ctx.org : ctx.platform; } export function useAuthzReload() { return useContext(AuthzContext).reload; } // useOrgViewReady():企业视图是否已按当前企业(含代理会话)加载过(成败均算)。 // 企业中心在它就绪前不做路由守卫,避免新体系下持有菜单的人在视图到达前被旧判断重定向走。 // 当前企业刚由解析器写入、Provider 尚未感知时主动触发一次加载。 export function useOrgViewReady() { const ctx = useContext(AuthzContext); const key = currentOrgViewKey(); const ready = ctx.orgLoadedKey === key; const { reload } = ctx; useEffect(() => { if (!ready) reload(); }, [ready, key, reload]); return ready; }