/*
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 { 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 (
}
onClick={onClick}
/>
);
}
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 (
}
>