2026-05-27 23:54:32 +00:00
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
2026-05-28 09:08:52 +00:00
|
|
|
import { Alert, Input, Modal, Select, Space, Table, Tag, Typography, message } from 'antd';
|
|
|
|
|
import type { ColumnsType } from 'antd/es/table';
|
2026-05-27 23:54:32 +00:00
|
|
|
|
|
|
|
|
import { HttpUtil } from '@/utils';
|
|
|
|
|
import { coerceInboundJsonField, type DBInbound } from '@/models/dbinbound';
|
|
|
|
|
import { isInboundMultiUser } from './InboundList';
|
|
|
|
|
|
|
|
|
|
interface AttachClientsModalProps {
|
|
|
|
|
open: boolean;
|
|
|
|
|
source: DBInbound | null;
|
|
|
|
|
dbInbounds: DBInbound[];
|
|
|
|
|
onClose: () => void;
|
|
|
|
|
onAttached?: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface BulkAttachResult {
|
|
|
|
|
attached?: string[];
|
|
|
|
|
skipped?: string[];
|
|
|
|
|
errors?: string[];
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-28 09:08:52 +00:00
|
|
|
interface ClientRow {
|
|
|
|
|
email: string;
|
|
|
|
|
comment: string;
|
|
|
|
|
enable: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function readClientRows(settings: unknown): ClientRow[] {
|
|
|
|
|
const parsed = coerceInboundJsonField(settings) as {
|
|
|
|
|
clients?: Array<{ email?: string; comment?: string; enable?: boolean }>;
|
|
|
|
|
};
|
2026-05-27 23:54:32 +00:00
|
|
|
const clients = Array.isArray(parsed?.clients) ? parsed.clients : [];
|
2026-05-28 09:08:52 +00:00
|
|
|
return clients
|
|
|
|
|
.map((c) => ({
|
|
|
|
|
email: (c?.email || '').trim(),
|
|
|
|
|
comment: (c?.comment || '').trim(),
|
|
|
|
|
enable: c?.enable !== false,
|
|
|
|
|
}))
|
|
|
|
|
.filter((r) => r.email);
|
2026-05-27 23:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function AttachClientsModal({
|
|
|
|
|
open,
|
|
|
|
|
source,
|
|
|
|
|
dbInbounds,
|
|
|
|
|
onClose,
|
|
|
|
|
onAttached,
|
|
|
|
|
}: AttachClientsModalProps) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const [messageApi, messageContextHolder] = message.useMessage();
|
|
|
|
|
const [targetIds, setTargetIds] = useState<number[]>([]);
|
|
|
|
|
const [saving, setSaving] = useState(false);
|
2026-05-28 09:08:52 +00:00
|
|
|
const [clientRows, setClientRows] = useState<ClientRow[]>([]);
|
|
|
|
|
const [selectedEmails, setSelectedEmails] = useState<string[]>([]);
|
|
|
|
|
const [search, setSearch] = useState('');
|
2026-05-27 23:54:32 +00:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-05-28 09:08:52 +00:00
|
|
|
if (!open) return;
|
|
|
|
|
const rows = source ? readClientRows(source.settings) : [];
|
|
|
|
|
setClientRows(rows);
|
|
|
|
|
setSelectedEmails(rows.map((r) => r.email));
|
|
|
|
|
setTargetIds([]);
|
|
|
|
|
setSearch('');
|
|
|
|
|
}, [open, source]);
|
2026-05-27 23:54:32 +00:00
|
|
|
|
|
|
|
|
const targetOptions = useMemo(() => {
|
|
|
|
|
if (!source) return [];
|
|
|
|
|
return (dbInbounds || [])
|
|
|
|
|
.filter((ib) => ib.id !== source.id && isInboundMultiUser(ib))
|
|
|
|
|
.map((ib) => ({ value: ib.id, label: `${ib.remark} (${ib.protocol}@${ib.port})` }));
|
|
|
|
|
}, [dbInbounds, source]);
|
|
|
|
|
|
2026-05-28 09:08:52 +00:00
|
|
|
const filteredRows = useMemo(() => {
|
|
|
|
|
const q = search.trim().toLowerCase();
|
|
|
|
|
if (!q) return clientRows;
|
|
|
|
|
return clientRows.filter(
|
|
|
|
|
(r) => r.email.toLowerCase().includes(q) || r.comment.toLowerCase().includes(q),
|
|
|
|
|
);
|
|
|
|
|
}, [clientRows, search]);
|
|
|
|
|
|
|
|
|
|
const columns: ColumnsType<ClientRow> = useMemo(
|
|
|
|
|
() => [
|
|
|
|
|
{
|
|
|
|
|
title: t('pages.inbounds.email'),
|
|
|
|
|
dataIndex: 'email',
|
|
|
|
|
key: 'email',
|
|
|
|
|
ellipsis: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: t('comment'),
|
|
|
|
|
dataIndex: 'comment',
|
|
|
|
|
key: 'comment',
|
|
|
|
|
ellipsis: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: t('enable'),
|
|
|
|
|
dataIndex: 'enable',
|
|
|
|
|
key: 'enable',
|
|
|
|
|
width: 90,
|
|
|
|
|
render: (enabled: boolean) =>
|
|
|
|
|
enabled ? (
|
|
|
|
|
<Tag color="success">{t('enable')}</Tag>
|
|
|
|
|
) : (
|
|
|
|
|
<Tag>{t('pages.inbounds.attachClientsStatusDisabled')}</Tag>
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
[t],
|
|
|
|
|
);
|
|
|
|
|
|
2026-05-27 23:54:32 +00:00
|
|
|
async function submit() {
|
2026-05-28 09:08:52 +00:00
|
|
|
if (!source || targetIds.length === 0 || selectedEmails.length === 0) return;
|
2026-05-27 23:54:32 +00:00
|
|
|
setSaving(true);
|
|
|
|
|
try {
|
2026-05-28 09:08:52 +00:00
|
|
|
const msg = await HttpUtil.post(
|
|
|
|
|
'/panel/api/clients/bulkAttach',
|
|
|
|
|
{ emails: selectedEmails, inboundIds: targetIds },
|
|
|
|
|
{ headers: { 'Content-Type': 'application/json' } },
|
|
|
|
|
);
|
2026-05-27 23:54:32 +00:00
|
|
|
if (!msg?.success) {
|
|
|
|
|
messageApi.error(msg?.msg || t('somethingWentWrong'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const result = (msg.obj || {}) as BulkAttachResult;
|
|
|
|
|
const attached = result.attached?.length ?? 0;
|
|
|
|
|
const skipped = result.skipped?.length ?? 0;
|
|
|
|
|
const errors = result.errors?.length ?? 0;
|
|
|
|
|
if (errors > 0) {
|
|
|
|
|
messageApi.warning(t('pages.inbounds.attachClientsResultMixed', { attached, skipped, errors }));
|
|
|
|
|
} else {
|
|
|
|
|
messageApi.success(t('pages.inbounds.attachClientsResult', { attached, skipped }));
|
|
|
|
|
}
|
|
|
|
|
onAttached?.();
|
|
|
|
|
onClose();
|
|
|
|
|
} finally {
|
|
|
|
|
setSaving(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Modal
|
|
|
|
|
open={open}
|
|
|
|
|
onCancel={onClose}
|
|
|
|
|
onOk={submit}
|
2026-05-28 09:08:52 +00:00
|
|
|
okButtonProps={{
|
|
|
|
|
disabled: targetIds.length === 0 || selectedEmails.length === 0,
|
|
|
|
|
loading: saving,
|
|
|
|
|
}}
|
2026-05-27 23:54:32 +00:00
|
|
|
okText={t('pages.inbounds.attachClients')}
|
|
|
|
|
cancelText={t('cancel')}
|
|
|
|
|
title={t('pages.inbounds.attachClientsTitle', { remark: source?.remark ?? '' })}
|
2026-05-28 09:08:52 +00:00
|
|
|
width={680}
|
2026-05-27 23:54:32 +00:00
|
|
|
>
|
|
|
|
|
{messageContextHolder}
|
|
|
|
|
<Typography.Paragraph type="secondary">
|
2026-05-28 09:08:52 +00:00
|
|
|
{t('pages.inbounds.attachClientsDesc', { count: clientRows.length })}
|
2026-05-27 23:54:32 +00:00
|
|
|
</Typography.Paragraph>
|
2026-05-28 09:08:52 +00:00
|
|
|
|
|
|
|
|
<Space direction="vertical" size="small" style={{ width: '100%', marginBottom: 12 }}>
|
|
|
|
|
<Typography.Text strong>{t('pages.inbounds.attachClientsSelectLabel')}</Typography.Text>
|
|
|
|
|
<Space style={{ width: '100%', justifyContent: 'space-between' }} wrap>
|
|
|
|
|
<Input.Search
|
|
|
|
|
allowClear
|
|
|
|
|
value={search}
|
|
|
|
|
onChange={(e) => setSearch(e.target.value)}
|
|
|
|
|
placeholder={t('pages.inbounds.attachClientsSearchPlaceholder')}
|
|
|
|
|
style={{ maxWidth: 320 }}
|
|
|
|
|
/>
|
|
|
|
|
<Typography.Text type="secondary">
|
|
|
|
|
{t('pages.inbounds.attachClientsSelectedCount', {
|
|
|
|
|
selected: selectedEmails.length,
|
|
|
|
|
total: clientRows.length,
|
|
|
|
|
})}
|
|
|
|
|
</Typography.Text>
|
|
|
|
|
</Space>
|
|
|
|
|
<Table<ClientRow>
|
|
|
|
|
size="small"
|
|
|
|
|
rowKey="email"
|
|
|
|
|
columns={columns}
|
|
|
|
|
dataSource={filteredRows}
|
|
|
|
|
pagination={false}
|
|
|
|
|
scroll={{ y: 280 }}
|
|
|
|
|
rowSelection={{
|
|
|
|
|
selectedRowKeys: selectedEmails,
|
|
|
|
|
onChange: (keys) => setSelectedEmails(keys as string[]),
|
|
|
|
|
preserveSelectedRowKeys: true,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</Space>
|
|
|
|
|
|
2026-05-27 23:54:32 +00:00
|
|
|
{targetOptions.length === 0 ? (
|
|
|
|
|
<Alert type="info" showIcon message={t('pages.inbounds.attachClientsNoTargets')} />
|
|
|
|
|
) : (
|
|
|
|
|
<Select
|
|
|
|
|
mode="multiple"
|
|
|
|
|
style={{ width: '100%' }}
|
|
|
|
|
value={targetIds}
|
|
|
|
|
onChange={setTargetIds}
|
|
|
|
|
options={targetOptions}
|
|
|
|
|
placeholder={t('pages.inbounds.attachClientsTargets')}
|
|
|
|
|
optionFilterProp="label"
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</Modal>
|
|
|
|
|
);
|
|
|
|
|
}
|