import { Checkbox, Classes, Switch } from '@blueprintjs/core'; import type { InfoBlockField } from '@zakodium/nmrium-core'; import { useCallback, useMemo } from 'react'; import { useFormContext } from 'react-hook-form'; import { FaPlus, FaRegTrashAlt } from 'react-icons/fa'; import { Button } from 'react-science/ui'; import type { CellProps } from 'react-table'; import { useChartData } from '../../../context/ChartContext.js'; import { GroupPane } from '../../../elements/GroupPane.js'; import { Input2Controller } from '../../../elements/Input2Controller.js'; import Label from '../../../elements/Label.js'; import type { Column } from '../../../elements/ReactTable/ReactTable.js'; import ReactTable from '../../../elements/ReactTable/ReactTable.js'; import type { WorkspaceWithSource } from '../../../reducer/preferences/preferencesReducer.js'; import { convertPathArrayToString } from '../../../utility/convertPathArrayToString.js'; import { getSpectraObjectPaths } from '../../../utility/getSpectraObjectPaths.js'; import { Section, settingLabelStyle } from '../general_settings.js'; function getKeyPath( index: number, key: T, ): `infoBlock.fields.${number}.${T}` { return `infoBlock.fields.${index}.${key}`; } function InfoBlockTabContent() { const { control, setValue, register, watch } = useFormContext(); const { data } = useChartData(); const { datalist, paths } = useMemo( () => getSpectraObjectPaths(data), [data], ); const fields: InfoBlockField[] = watch('infoBlock.fields') || []; const addHandler = useCallback( (data: readonly any[], index: number) => { let columns: any[] = []; const emptyField = { label: '', jpath: null, visible: true, }; if (data && Array.isArray(data)) { columns = [...data.slice(0, index), emptyField, ...data.slice(index)]; } else { columns.push(emptyField); } setValue('infoBlock.fields', columns); }, [setValue], ); const deleteHandler = useCallback( (data: any, index: number) => { const _fields = data.filter( (_: any, columnIndex: any) => columnIndex !== index, ); setValue('infoBlock.fields', _fields); }, [setValue], ); const COLUMNS = useMemo>>( () => [ { Header: '#', style: { width: '25px', textAlign: 'center' }, accessor: (_, index) => index + 1, }, { Header: 'Label', Cell: ({ row }: CellProps) => { const name = getKeyPath(row.index, 'label'); return ( ); }, }, { Header: 'Field', Cell: ({ row }: CellProps) => { const name = getKeyPath(row.index, 'jpath'); return ( (paths as any)?.[value.trim()] || value } mapValue={convertPathArrayToString} /> ); }, }, { Header: 'Format', Cell: ({ row }: CellProps) => { const name = getKeyPath(row.index, 'format'); return ( ); }, }, { Header: 'Visible', style: { width: '30px', textAlign: 'center' }, Cell: ({ row }: CellProps) => ( ), }, { Header: '', style: { width: '60px' }, id: 'add-button', Cell: ({ data, row }: CellProps) => { const record: any = row.original; return (
{!record?.name && ( )}
); }, }, ], [addHandler, control, datalist, deleteHandler, paths, register], ); return (
{ return ( addHandler(fields, 0)} /> ); }} >
); } interface FieldsBlockHeaderProps { onAdd: () => void; text: string; } function FieldsBlockHeader(props: FieldsBlockHeaderProps) { const { onAdd, text } = props; return (

{text}

); } export default InfoBlockTabContent;