a57aae3043
- 合入 weiyuan 新功能前端:分佣 V2 / 伙伴工作台、智能路由、企业资金与分析、官网设置与预约演示等, semi-ui 已全部迁移到 antd 6 / @/components/ui,不重新引入 Semi - 分佣二级菜单与页头去重、伙伴入口探测(企业伙伴走企业中心「企业分佣」)、超管侧栏入口恢复 - 适配层 DatePicker / TimePicker 统一 onChange(Date|Date[]|null, 文本) 契约;生产构建剔除调试日志 - 图片链接刷新:同任务请求复用、后台刷新不弹全局错误、离页停止批量请求 - 恢复 bun.lock(与 package-lock.json 共存);docs 规范与 CHANGELOG 更新 - 停止跟踪 .env.playwright.local(本地登录凭据,已加入 .gitignore)
184 lines
5.4 KiB
React
184 lines
5.4 KiB
React
/*
|
||
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 React, {
|
||
forwardRef,
|
||
useEffect,
|
||
useImperativeHandle,
|
||
useRef,
|
||
useState,
|
||
} from 'react';
|
||
import { Alert, Button, Select } from 'antd';
|
||
import { useTranslation } from 'react-i18next';
|
||
import {
|
||
createRequestGeneration,
|
||
mergeIdentityOptions,
|
||
identityOptionLabel,
|
||
} from './latestTiers';
|
||
|
||
const RemoteIdentitySelect = forwardRef(function RemoteIdentitySelect(
|
||
{ type, value, onChange, fetchJSON },
|
||
ref,
|
||
) {
|
||
const { t } = useTranslation();
|
||
const gate = useRef(createRequestGeneration());
|
||
const timer = useRef();
|
||
const [query, setQuery] = useState('');
|
||
const [state, setState] = useState({
|
||
items: [],
|
||
total: 0,
|
||
page: 0,
|
||
busy: false,
|
||
error: '',
|
||
});
|
||
const selected = useRef(null);
|
||
const active = useRef(true);
|
||
useImperativeHandle(
|
||
ref,
|
||
() => ({
|
||
invalidate() {
|
||
active.current = false;
|
||
gate.current.next();
|
||
clearTimeout(timer.current);
|
||
},
|
||
}),
|
||
[],
|
||
);
|
||
const load = async (q, page, generation) => {
|
||
setState((old) => ({ ...old, busy: true, error: '' }));
|
||
try {
|
||
const result = await fetchJSON(
|
||
`/api/commission/v2/options/${type === 'company' ? 'organizations' : 'users'}?${new URLSearchParams({ q, p: String(page), page_size: '20' })}`,
|
||
);
|
||
if (!gate.current.current(generation)) return;
|
||
setState((old) => ({
|
||
items: mergeIdentityOptions(
|
||
page === 1 ? [] : old.items,
|
||
result.items || [],
|
||
selected.current,
|
||
),
|
||
total: result.total,
|
||
page,
|
||
busy: false,
|
||
error: '',
|
||
}));
|
||
} catch (error) {
|
||
if (gate.current.current(generation))
|
||
setState((old) => ({ ...old, busy: false, error: error.message }));
|
||
}
|
||
};
|
||
const search = (q) => {
|
||
clearTimeout(timer.current);
|
||
const generation = gate.current.next();
|
||
setQuery(q);
|
||
setState({
|
||
items: selected.current ? [selected.current] : [],
|
||
total: 0,
|
||
page: 0,
|
||
busy: true,
|
||
error: '',
|
||
});
|
||
timer.current = setTimeout(() => load(q, 1, generation), 300);
|
||
};
|
||
useEffect(() => {
|
||
active.current = true;
|
||
search('');
|
||
return () => {
|
||
active.current = false;
|
||
// 卸载时作废进行中的请求(原有逻辑)。
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||
gate.current.next();
|
||
clearTimeout(timer.current);
|
||
};
|
||
// 仅在类型或数据源变化时重新搜索(原有逻辑,依赖为有意设置)。
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||
}, [type, fetchJSON]);
|
||
const selectIdentity = (id) => {
|
||
if (!active.current) return;
|
||
selected.current = state.items.find((item) => item.id === id) || null;
|
||
onChange(id);
|
||
};
|
||
return (
|
||
<div>
|
||
<Select
|
||
aria-label={t(type === 'company' ? '关联企业' : '关联个人用户')}
|
||
placeholder={t(
|
||
type === 'company'
|
||
? '输入企业 ID、名称或编码'
|
||
: '输入用户 ID、用户名或显示名',
|
||
)}
|
||
showSearch={{ filterOption: false, onSearch: search }}
|
||
allowClear
|
||
style={{ width: '100%' }}
|
||
value={value}
|
||
loading={state.busy}
|
||
// 选中即提交,保证快速保存时读取到的就是界面显示的 ID。
|
||
onSelect={selectIdentity}
|
||
onChange={(id) => {
|
||
if (id == null) selectIdentity(id);
|
||
}}
|
||
options={state.items.map((item) => ({
|
||
value: item.id,
|
||
label: identityOptionLabel(item),
|
||
disabled: item.disabled,
|
||
}))}
|
||
notFoundContent={
|
||
state.error
|
||
? t('加载失败')
|
||
: state.busy
|
||
? t('加载中')
|
||
: t(
|
||
type === 'company'
|
||
? '未找到可关联企业,请核对 ID 或企业状态'
|
||
: '未找到可关联用户,请核对 ID 或账号状态',
|
||
)
|
||
}
|
||
onOpenChange={(visible) => {
|
||
if (visible && !state.page && !state.busy) search(query);
|
||
}}
|
||
/>
|
||
{state.error && (
|
||
<Alert
|
||
type='error'
|
||
showIcon
|
||
className='commission-inline-alert'
|
||
title={state.error}
|
||
/>
|
||
)}
|
||
{state.error && (
|
||
<Button
|
||
aria-label={t('重试加载身份')}
|
||
onClick={() => load(query, state.page + 1, gate.current.next())}
|
||
>
|
||
{t('重试')}
|
||
</Button>
|
||
)}
|
||
{!state.error && state.page * 20 < state.total && (
|
||
<Button
|
||
aria-label={t('加载更多身份')}
|
||
loading={state.busy}
|
||
onClick={() => load(query, state.page + 1, gate.current.next())}
|
||
>
|
||
{t('加载更多')}
|
||
</Button>
|
||
)}
|
||
</div>
|
||
);
|
||
});
|
||
export default RemoteIdentitySelect;
|