/* 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, useMemo, useState } from 'react'; import { API } from '../../helpers/api'; import { UserContext } from '../../context/User'; import { StatusContext } from '../../context/Status'; import { resolvePartnerWorkspaceIdentity } from '../../pages/PartnerPortal/redirect'; import { partnerScope } from '../../pages/CommissionV2/partnerScope'; // 顶栏入口与工作台使用同一套服务端鉴权,不根据平台角色或旧版伙伴记录推断。 export function usePartnerWorkspaceAccess(organizationId = null) { const [userState] = useContext(UserContext); const [statusState] = useContext(StatusContext); const user = userState?.user; const scope = useMemo( () => partnerScope(user?.id, organizationId), [user?.id, organizationId], ); const portalEnabled = statusState?.status?.commission_partner_portal_enabled; // 分佣总开关关闭时不发起伙伴身份探测;旧后端不返回该字段,视为开启。 const commissionEnabled = statusState?.status?.commission_enabled !== false; const enabled = commissionEnabled && (portalEnabled === true || portalEnabled === 'true') && scope.valid && !userState?.authChecking; const [access, setAccess] = useState(null); const [attempt, setAttempt] = useState(0); useEffect(() => { let cancelled = false; setAccess(null); if (!enabled) return undefined; resolvePartnerWorkspaceIdentity((url, config) => API.get(scope.url(url), { ...config, headers: scope.headers, }), ).then(({ status, partnerType }) => { if (!cancelled) setAccess({ user, status, partnerType, attempt, scopeKey: scope.key }); }); return () => { cancelled = true; }; }, [enabled, user, attempt, scope]); // 绑定本次登录的用户快照,切换账号时在 effect 执行前就隐藏旧入口。 let status = 'loading'; let partnerType = ''; if (statusState?.status !== undefined && !userState?.authChecking) { if (!enabled) status = 'denied'; else if ( access?.user === user && access?.attempt === attempt && access?.scopeKey === scope.key ) { status = access.status; partnerType = access.partnerType || ''; } } return { status, partnerType, retry: () => setAttempt((value) => value + 1), }; } // 顶栏「伙伴工作台」与控制台侧栏「我的提成」只给个人伙伴: // 企业伙伴(含企业登录与管理企业的个人会话)统一从企业中心「企业分佣」进入,避免两处入口。 export function usePartnerWorkspaceEntry() { const { status, partnerType } = usePartnerWorkspaceAccess(); return status === 'allowed' && partnerType === 'individual'; }