import { useState, type ReactElement } from 'react' import { fieldLabel, groupFields, MAX_COLUMNS, type Field } from './screener' // Which fields the table shows besides the ones filtered and sorted on, which are shown whatever is // picked here. The list is the server's guide, group by group; what is picked goes into the panel's // state, and from there into the screen's arguments, since the server sends only the columns asked for. interface Props { columns: string[] fields: Field[] fieldsError: string | null onApply: (columns: string[]) => void onClose: () => void } export function ColumnsDialog({ columns, fields, fieldsError, onApply, onClose }: Props): ReactElement { const [picked, setPicked] = useState(columns) // The identity columns are the company cell; the table draws them whatever is asked for. const groups = groupFields(fields.filter((field) => !['cik', 'name', 'sic_description'].includes(field.field))) function toggle(name: string, on: boolean): void { setPicked((previous) => (on ? [...previous, name] : previous.filter((item) => item !== name))) } return (
event.stopPropagation()}>
Columns
{fields.length === 0 &&

{fieldsError ?? 'The server sent no list of fields.'}

} {groups.map(([group, members]) => (
{group}
{members.map((field) => ( ))}
))}
{picked.length} picked{picked.length > MAX_COLUMNS ? `, ${MAX_COLUMNS} at most` : ''}
) }