mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-04 16:34:52 +03:00
1ac11c7783
Keep group client tables and bulk requests bounded with paged queries, frozen selections, and a streamed subscription export. Reuse existing bulk mutations and preserve the explicit-email contracts.
274 lines
7.8 KiB
TypeScript
274 lines
7.8 KiB
TypeScript
import { useMemo, useState } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { Alert, Button, Modal, Table, Tooltip, Typography, message } from 'antd';
|
|
import type { TableColumnType } from 'antd';
|
|
import { CopyOutlined, DownloadOutlined } from '@ant-design/icons';
|
|
|
|
import {
|
|
GROUP_CLIENT_PAGE_SIZE,
|
|
groupSubscriptionExportPath,
|
|
useGroupClientPage,
|
|
} from '@/api/groupClients';
|
|
|
|
import type { ClientRecord } from '@/hooks/useClients';
|
|
|
|
interface SubSettings {
|
|
enable: boolean;
|
|
subURI: string;
|
|
subJsonURI: string;
|
|
subJsonEnable: boolean;
|
|
}
|
|
|
|
type SubLinksModalProps = {
|
|
open: boolean;
|
|
subSettings?: SubSettings;
|
|
onOpenChange: (open: boolean) => void;
|
|
} & (
|
|
| { emails: string[]; clients: ClientRecord[]; group?: never }
|
|
| { group: string; emails?: never; clients?: never }
|
|
);
|
|
|
|
interface Row {
|
|
key: string;
|
|
email: string;
|
|
subId: string;
|
|
link: string;
|
|
jsonLink: string;
|
|
}
|
|
|
|
export default function SubLinksModal({
|
|
open,
|
|
emails,
|
|
clients,
|
|
group,
|
|
subSettings,
|
|
onOpenChange,
|
|
}: SubLinksModalProps) {
|
|
const { t } = useTranslation();
|
|
const [messageApi, messageContextHolder] = message.useMessage();
|
|
|
|
const enabled = !!subSettings?.enable && !!subSettings?.subURI;
|
|
const jsonEnabled = !!subSettings?.subJsonEnable && !!subSettings?.subJsonURI;
|
|
|
|
const [page, setPage] = useState(1);
|
|
const [copying, setCopying] = useState(false);
|
|
const groupPage = useGroupClientPage(
|
|
{ group: group ?? '', mode: 'links', page },
|
|
!!group && open && enabled,
|
|
);
|
|
const exportPath = group ? groupSubscriptionExportPath(group) : undefined;
|
|
const rows = useMemo<Row[]>(() => {
|
|
if (!enabled) return [];
|
|
function row(email: string, subId: string): Row {
|
|
return {
|
|
key: email,
|
|
email,
|
|
subId,
|
|
link: subSettings!.subURI + subId,
|
|
jsonLink: jsonEnabled ? subSettings!.subJsonURI + subId : '',
|
|
};
|
|
}
|
|
if (group) return (groupPage.data?.items ?? []).map(({ email, subId }) => row(email, subId));
|
|
const byEmail = new Map((clients ?? []).map((client) => [client.email, client.subId]));
|
|
const out: Row[] = [];
|
|
for (const email of emails ?? []) {
|
|
const subId = byEmail.get(email);
|
|
if (subId) out.push(row(email, subId));
|
|
}
|
|
return out;
|
|
}, [emails, clients, enabled, group, groupPage.data?.items, jsonEnabled, subSettings]);
|
|
const total = group ? (groupPage.data?.total ?? 0) : rows.length;
|
|
|
|
function allText() {
|
|
return rows
|
|
.map(({ email, link, jsonLink }) =>
|
|
jsonEnabled ? `${email}\t${link}\t${jsonLink}` : `${email}\t${link}`,
|
|
)
|
|
.join('\n');
|
|
}
|
|
|
|
async function copyAll() {
|
|
if (!exportPath) {
|
|
await copy(allText(), t('pages.clients.subLinksCopiedAll', { count: total }));
|
|
return;
|
|
}
|
|
setCopying(true);
|
|
try {
|
|
const content = async () => {
|
|
const response = await fetch(exportPath, { credentials: 'same-origin' });
|
|
if (!response.ok || !response.headers.get('Content-Type')?.startsWith('text/plain')) {
|
|
throw new Error(t('somethingWentWrong'));
|
|
}
|
|
return response.blob();
|
|
};
|
|
if (typeof ClipboardItem !== 'undefined' && navigator.clipboard?.write) {
|
|
await navigator.clipboard.write([
|
|
new ClipboardItem({
|
|
'text/plain': content().then((blob) => new Blob([blob], { type: 'text/plain' })),
|
|
}),
|
|
]);
|
|
} else {
|
|
await navigator.clipboard.writeText(await (await content()).text());
|
|
}
|
|
messageApi.success(t('copied'));
|
|
} catch {
|
|
messageApi.error(t('somethingWentWrong'));
|
|
} finally {
|
|
setCopying(false);
|
|
}
|
|
}
|
|
|
|
async function copy(text: string, label?: string) {
|
|
try {
|
|
await navigator.clipboard.writeText(text);
|
|
messageApi.success(label || t('copied'));
|
|
} catch {
|
|
messageApi.error(t('somethingWentWrong'));
|
|
}
|
|
}
|
|
|
|
function download() {
|
|
const blob = new Blob([allText()], { type: 'text/plain;charset=utf-8' });
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
const stamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
|
|
a.href = url;
|
|
a.download = `sub-links-${stamp}.txt`;
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
a.remove();
|
|
URL.revokeObjectURL(url);
|
|
}
|
|
|
|
const columns: TableColumnType<Row>[] = [
|
|
{
|
|
title: t('pages.clients.client'),
|
|
dataIndex: 'email',
|
|
key: 'email',
|
|
width: 180,
|
|
ellipsis: true,
|
|
},
|
|
{
|
|
title: t('pages.clients.subLinkColumn'),
|
|
dataIndex: 'link',
|
|
key: 'link',
|
|
ellipsis: true,
|
|
render: (link: string) => (
|
|
<Tooltip title={link} placement="topLeft">
|
|
<Typography.Text copyable={false} ellipsis>
|
|
{link}
|
|
</Typography.Text>
|
|
</Tooltip>
|
|
),
|
|
},
|
|
{
|
|
title: '',
|
|
key: 'actions',
|
|
width: 64,
|
|
render: (_v, row) => (
|
|
<Button
|
|
size="small"
|
|
type="text"
|
|
aria-label={t('copy')}
|
|
icon={<CopyOutlined aria-hidden />}
|
|
onClick={() => copy(row.link, t('copied'))}
|
|
/>
|
|
),
|
|
},
|
|
];
|
|
|
|
if (jsonEnabled) {
|
|
columns.splice(2, 0, {
|
|
title: t('pages.clients.subJsonLinkColumn'),
|
|
dataIndex: 'jsonLink',
|
|
key: 'jsonLink',
|
|
ellipsis: true,
|
|
render: (link: string) => (
|
|
<Tooltip title={link} placement="topLeft">
|
|
<Typography.Text copyable={false} ellipsis>
|
|
{link}
|
|
</Typography.Text>
|
|
</Tooltip>
|
|
),
|
|
});
|
|
}
|
|
|
|
return (
|
|
<>
|
|
{messageContextHolder}
|
|
<Modal
|
|
open={open}
|
|
title={t('pages.clients.subLinksTitle', { count: total })}
|
|
width={780}
|
|
footer={
|
|
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
|
|
<Button onClick={() => onOpenChange(false)}>{t('close')}</Button>
|
|
<div style={{ display: 'flex', gap: 8 }}>
|
|
<Button
|
|
icon={<CopyOutlined aria-hidden />}
|
|
disabled={!total}
|
|
loading={copying}
|
|
onClick={copyAll}
|
|
>
|
|
{t('pages.clients.subLinksCopyAll')}
|
|
</Button>
|
|
<Button
|
|
type="primary"
|
|
icon={<DownloadOutlined aria-hidden />}
|
|
disabled={!total}
|
|
href={exportPath}
|
|
download={exportPath ? 'sub-links.txt' : undefined}
|
|
onClick={exportPath ? undefined : download}
|
|
>
|
|
{t('download')}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
}
|
|
onCancel={() => onOpenChange(false)}
|
|
>
|
|
{!enabled && (
|
|
<Alert
|
|
type="warning"
|
|
showIcon
|
|
title={t('pages.clients.subLinksDisabled')}
|
|
description={t('pages.clients.subLinksDisabledHint')}
|
|
style={{ marginBottom: 12 }}
|
|
/>
|
|
)}
|
|
{enabled && !total && !groupPage.isFetching && !groupPage.isError && (
|
|
<Alert
|
|
type="info"
|
|
showIcon
|
|
title={t('pages.clients.subLinksEmpty')}
|
|
style={{ marginBottom: 12 }}
|
|
/>
|
|
)}
|
|
{groupPage.isError && (
|
|
<Alert showIcon type="error" title={groupPage.error.message || t('somethingWentWrong')} />
|
|
)}
|
|
{enabled && (total > 0 || groupPage.isFetching) && (
|
|
<Table<Row>
|
|
dataSource={rows}
|
|
columns={columns}
|
|
size="small"
|
|
loading={groupPage.isFetching}
|
|
pagination={
|
|
group
|
|
? {
|
|
current: page,
|
|
pageSize: GROUP_CLIENT_PAGE_SIZE,
|
|
total,
|
|
showSizeChanger: false,
|
|
onChange: setPage,
|
|
}
|
|
: false
|
|
}
|
|
scroll={{ y: 360 }}
|
|
/>
|
|
)}
|
|
</Modal>
|
|
</>
|
|
);
|
|
}
|