97 lines
4.0 KiB
JavaScript
97 lines
4.0 KiB
JavaScript
/*
|
||
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 <https://www.gnu.org/licenses/>.
|
||
|
||
For commercial licensing, please contact support@quantumnous.com
|
||
*/
|
||
|
||
import { useState, useEffect } from 'react';
|
||
import { API } from '../../helpers';
|
||
|
||
const ADMIN_ROLES = ['owner', 'admin', 'super_admin'];
|
||
|
||
/**
|
||
* 拉取当前用户所属企业,用于判断是否在顶部一级导航展示「企业中心」入口。
|
||
* 仅登录且 orgCenter 功能可用时拉取一次;否则返回 hasOrg=false 且不发请求。
|
||
*
|
||
* @param {number|undefined} userId 当前登录用户 id(未登录为 undefined)
|
||
* @param {boolean} enabled orgCenter 功能是否可用(关闭时跳过请求)
|
||
* @returns {{hasOrg: boolean, orgEntryPath: string, activeOrg: object|null,
|
||
* isOrgAdmin: boolean, loaded: boolean}}
|
||
* loaded 区分"确定不是管理员"与"还不知道":调用方若拿 isOrgAdmin 做重定向类
|
||
* 判定,必须等 loaded,否则会在企业列表回来之前就把人重定向走(不可逆)。
|
||
*/
|
||
export const useOrgMembership = (userId, enabled = true) => {
|
||
const [orgs, setOrgs] = useState([]);
|
||
// 记录"已完成加载的那组入参",而不是一个裸布尔。裸布尔在这里是不够的:
|
||
// App 首帧 user 还是 undefined(loadUser 异步 dispatch),enabled 为 false,
|
||
// effect 会立刻把它置 true;等 /api/user/self 回来那一帧 enabled 翻 true,
|
||
// 但已 commit 的布尔仍是 true,调用方当帧就按"已知"做了重定向判定——正是
|
||
// 本标志要防的那条路径。改成在渲染期比对 key,入参一变 loaded 立即为 false。
|
||
const [loadedKey, setLoadedKey] = useState(null);
|
||
const currentKey = `${userId || 0}:${enabled ? 1 : 0}`;
|
||
const loaded = loadedKey === currentKey;
|
||
|
||
useEffect(() => {
|
||
let cancelled = false;
|
||
if (!userId || !enabled) {
|
||
setOrgs([]);
|
||
// 不需要拉取时直接视为"已知",避免调用方永久等待一个不会发生的请求。
|
||
setLoadedKey(currentKey);
|
||
return undefined;
|
||
}
|
||
(async () => {
|
||
try {
|
||
const res = await API.get('/api/org/list');
|
||
if (!cancelled && res?.data?.success) {
|
||
setOrgs(res.data.data || []);
|
||
}
|
||
} catch {
|
||
// 静默失败:拿不到企业列表时不展示入口即可
|
||
} finally {
|
||
// 失败也置位:否则一次网络抖动会让依赖 loaded 的分支永久挂起。
|
||
if (!cancelled) setLoadedKey(currentKey);
|
||
}
|
||
})();
|
||
return () => {
|
||
cancelled = true;
|
||
};
|
||
}, [userId, enabled, currentKey]);
|
||
|
||
const hasOrg = orgs.length > 0;
|
||
|
||
// 选取当前企业:优先 localStorage 记录的,否则取第一个
|
||
let activeOrg = null;
|
||
if (hasOrg) {
|
||
const savedId = parseInt(localStorage.getItem('current_org_id') || '0', 10);
|
||
activeOrg = orgs.find((o) => o.id === savedId) || orgs[0];
|
||
}
|
||
const role = activeOrg?.role || 'member';
|
||
// 按角色决定落地页,保证入口层级最浅:管理员进数据看板,员工进工作台
|
||
const orgEntryPath = ADMIN_ROLES.includes(role)
|
||
? '/org/dashboard'
|
||
: '/org/workbench';
|
||
|
||
// activeOrg / isOrgAdmin 供平台级页面(如技能超市)按企业身份做渐进增强。
|
||
// 复用同一套"当前企业"选取规则,避免各页面各自实现后与顶栏入口指向不同的企业。
|
||
return {
|
||
hasOrg,
|
||
orgEntryPath,
|
||
activeOrg,
|
||
isOrgAdmin: ADMIN_ROLES.includes(role),
|
||
loaded,
|
||
};
|
||
};
|