/*
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 React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import {
Table,
Card,
Checkbox,
Skeleton,
Pagination,
Empty,
Button,
Collapsible,
} from '@/components/ui';
import { IconChevronDown, IconChevronUp } from '@/components/ui/icons';
import PropTypes from 'prop-types';
import { useIsMobile } from '../../../hooks/common/useIsMobile';
import { useMinimumLoadingTime } from '../../../hooks/common/useMinimumLoadingTime';
import { useConsolePage } from '../../console/ConsoleContext';
/**
* CardTable 响应式表格组件
*
* 在桌面端渲染 Ant Design Table,在移动端则将每一行数据渲染成 Card 形式。
* 该组件与 Table 组件的大部分 API 保持一致,只需将原 Table 换成 CardTable 即可。
*/
const CardTable = ({
columns = [],
dataSource = [],
loading = false,
rowKey = 'key',
hidePagination = false,
...tableProps
}) => {
const consolePage = useConsolePage();
const isMobile = useIsMobile();
const { t } = useTranslation();
const showSkeleton = useMinimumLoadingTime(loading);
const getRowKey = (record, index) => {
if (typeof rowKey === 'function') return rowKey(record);
return record[rowKey] !== undefined ? record[rowKey] : index;
};
// ===== 移动端多选支持 =====
// 移动端卡片分支此前完全没有消费 rowSelection,导致调用方的 selectedKeys 永远是空数组,
// 密钥/兑换码/渠道/模型/管理员密钥五个页面的批量操作在手机上全是死按钮
// (点了只提示「请至少选择一个」,渠道页甚至会对空集合弹出删除确认)。
// 这里补上:兼容 Semi 的受控(传 selectedRowKeys)与非受控(只传 onChange)两种用法。
const rowSelection =
tableProps.rowSelection && typeof tableProps.rowSelection === 'object'
? tableProps.rowSelection
: null;
const selectionEnabled = Boolean(tableProps.rowSelection);
const [internalSelectedKeys, setInternalSelectedKeys] = useState([]);
const selectedKeys = rowSelection?.selectedRowKeys ?? internalSelectedKeys;
const isRowSelectable = (record, index) => {
const props = rowSelection?.getCheckboxProps?.(record, index);
return !props?.disabled;
};
const emitSelection = (nextKeys) => {
if (rowSelection?.selectedRowKeys === undefined) {
setInternalSelectedKeys(nextKeys);
}
const keySet = new Set(nextKeys);
const nextRows = dataSource.filter((record, index) =>
keySet.has(getRowKey(record, index)),
);
rowSelection?.onChange?.(nextKeys, nextRows);
};
const toggleRowSelected = (key, checked) => {
const nextKeys = checked
? [...selectedKeys.filter((k) => k !== key), key]
: selectedKeys.filter((k) => k !== key);
emitSelection(nextKeys);
};
const selectableKeys = dataSource
.map((record, index) =>
isRowSelectable(record, index) ? getRowKey(record, index) : null,
)
.filter((key) => key !== null);
const allSelected =
selectableKeys.length > 0 &&
selectableKeys.every((key) => selectedKeys.includes(key));
if (!isMobile || consolePage) {
const finalTableProps = hidePagination
? { ...tableProps, pagination: false }
: tableProps;
return (