/*
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 {
Home as ActionHome,
RefreshCw as ActionRefreshCw,
} from '@/components/common/icons/ActionIcons';
import React, { useState, useCallback, useEffect, useContext } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import { Building2, AlertTriangle, HelpCircle } from 'lucide-react';
import { Button } from '@/components/ui';
import { API } from '../../helpers/api';
import { UserContext } from '../../context/User';
import Loading from '../../components/common/ui/Loading';
import PageBackground from '../../components/common/PageBackground';
// 企业中心入口解析:本地没有可用的 current_org_id 时,由企业中心就地渲染本组件(不再跳转独立页面)。
// 企业与成员均由管理员创建,不提供用户自助加入。按归属状态区分:
// none —— 用户不属于任何企业,引导联系企业管理员开通账号
// inactive —— 用户有企业但均非活跃(冻结/欠费/待激活),引导联系管理员
// error —— 企业列表加载失败,提供重试
const ORG_STATUS_TEXT = {
2: '已被冻结',
3: '已欠费',
4: '待激活',
};
const OrgResolver = ({
onResolved,
excludeOrgIds = [],
hasMarquee = false,
}) => {
const { t } = useTranslation();
const navigate = useNavigate();
const [userState] = useContext(UserContext);
const isOrgOnly = userState?.user?.org_only_mode === true;
// 'checking' | 'none' | 'inactive' | 'error'
const [mode, setMode] = useState('checking');
const [inactiveOrgs, setInactiveOrgs] = useState([]);
// 有 active(status=1) 企业 → 写入 current_org_id 并交回企业中心(仅认 active,防冻结企业死循环)。
// excludeOrgIds 为本次加载失败过的企业:自动解析时一律跳过,避免"选中→失败→再选中"的循环;
// 可用企业若全部失败过,按加载失败处理(可能只是网络抖动),由用户手动重试时才再次尝试。
const runCheck = useCallback(
async (retryExcluded = false) => {
setMode('checking');
try {
const res = await API.get('/api/org/list');
if (res.data.success) {
const orgs = res.data.data || [];
const activeOrgs = orgs.filter((o) => o.status === 1);
const candidates = retryExcluded
? activeOrgs
: activeOrgs.filter((o) => !excludeOrgIds.includes(o.id));
if (candidates.length > 0) {
const savedOrgId = parseInt(
localStorage.getItem('current_org_id') || '0',
10,
);
const savedOrg =
savedOrgId > 0
? candidates.find((o) => o.id === savedOrgId)
: null;
const targetOrg = savedOrg || candidates[0];
localStorage.setItem('current_org_id', String(targetOrg.id));
onResolved(targetOrg.id);
return;
}
if (activeOrgs.length > 0) {
setMode('error');
return;
}
if (orgs.length > 0) {
setInactiveOrgs(orgs);
setMode('inactive');
return;
}
setMode('none');
return;
}
setMode('error');
} catch {
setMode('error');
}
},
[onResolved, excludeOrgIds],
);
useEffect(() => {
runCheck();
}, [runCheck]);
if (mode === 'checking') {
return ;
}
// 非 org_only 用户提供「返回首页」出口,避免被困在本页;org_only 员工受守卫限制在 /org,不提供。
const backHome = !isOrgOnly && (