Files
monkey-ai-web/src/components/common/table/ColumnSettings.jsx
T

218 lines
5.9 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 { Settings as ActionSettings } from '@/components/common/icons/ActionIcons';
import React, { useState } from 'react';
import { Alert, Button, Checkbox, Modal, Tooltip, theme } from 'antd';
import { useTranslation } from 'react-i18next';
import './column-settings.css';
const titleText = (title) =>
React.Children.toArray(title)
.map((child) =>
React.isValidElement(child) ? titleText(child.props.children) : child,
)
.join('');
const columnOptions = (columns) =>
columns.map((column) => ({
value: column.key,
label: column.label ?? titleText(column.title),
disabled: column.disabled,
}));
const selectedKeys = (options, value) =>
options
.filter((option) => value.includes(option.value))
.map((option) => option.value);
export function ColumnSettingsButton({ onClick, open = false }) {
const { t } = useTranslation();
return (
<Tooltip title={t('列设置')}>
<Button
className='table-column-settings-trigger'
type='text'
size='middle'
aria-label={t('列设置')}
aria-haspopup='dialog'
aria-expanded={open}
icon={<ActionSettings size={16} />}
onClick={onClick}
/>
</Tooltip>
);
}
export function ColumnSettingsModal({
open,
onCancel,
columns,
value,
defaultValue,
onConfirm,
}) {
const { t } = useTranslation();
const { token } = theme.useToken();
const options = columnOptions(columns);
const [session, setSession] = useState({
open: false,
draft: [],
error: false,
});
// Snapshot on opening only: background table refreshes must not reset the draft.
if (session.open !== open) {
setSession({ open, draft: selectedKeys(options, value), error: false });
}
const draft = selectedKeys(options, session.draft);
const selectable = options.filter((option) => !option.disabled);
const checkedCount = selectable.filter((option) =>
draft.includes(option.value),
).length;
const changeDraft = (keys) => setSession({ open, draft: keys, error: false });
const confirm = () => {
if (!draft.length) {
setSession({ ...session, error: true });
return;
}
onConfirm(draft);
onCancel();
};
return (
<Modal
open={open}
title={t('列设置')}
width={520}
rootClassName='table-column-settings'
className='table-column-settings-dialog'
style={{
'--cs-theme-text': token.colorText,
'--cs-theme-border': token.colorBorderSecondary,
}}
onCancel={onCancel}
keyboard
footer={
<div className='table-column-settings-footer'>
<Button
onClick={() => changeDraft(selectedKeys(options, defaultValue))}
>
{t('重置')}
</Button>
<Button onClick={onCancel}>{t('取消')}</Button>
<Button type='primary' onClick={confirm}>
{t('确定')}
</Button>
</div>
}
mask={{ closable: true }}
>
<div className='table-column-settings-all'>
<Checkbox
checked={selectable.length > 0 && checkedCount === selectable.length}
indeterminate={checkedCount > 0 && checkedCount < selectable.length}
disabled={!selectable.length}
onChange={(event) =>
changeDraft(
options
.filter((option) =>
option.disabled
? draft.includes(option.value)
: event.target.checked,
)
.map((option) => option.value),
)
}
>
{t('全选')}
</Checkbox>
</div>
<Checkbox.Group
className='table-column-settings-grid'
options={options}
value={draft}
onChange={changeDraft}
/>
{session.error && (
<Alert
className='table-column-settings-error'
type='error'
showIcon
title={t('请至少保留一列')}
/>
)}
</Modal>
);
}
export function ColumnVisibilitySettings({
showColumnSelector,
setShowColumnSelector,
columns,
visibleColumns,
setVisibleColumns,
getDefaultColumnVisibility,
}) {
const defaults = getDefaultColumnVisibility();
return (
<ColumnSettingsModal
open={showColumnSelector}
onCancel={() => setShowColumnSelector(false)}
columns={columns}
value={columns
.filter((column) => visibleColumns[column.key])
.map((column) => column.key)}
defaultValue={columns
.filter((column) => defaults[column.key])
.map((column) => column.key)}
onConfirm={(keys) =>
setVisibleColumns((current) => ({
...current,
...Object.fromEntries(
columns.map((column) => [column.key, keys.includes(column.key)]),
),
}))
}
/>
);
}
export default function TableColumnSettings({
columns,
value,
defaultValue,
onChange,
}) {
const [open, setOpen] = useState(false);
return (
<>
<ColumnSettingsButton open={open} onClick={() => setOpen(true)} />
<ColumnSettingsModal
open={open}
onCancel={() => setOpen(false)}
columns={columns}
value={value}
defaultValue={defaultValue}
onConfirm={onChange}
/>
</>
);
}