import { Button, Classes } from '@blueprintjs/core'; import type { LegendField, PredefinedLegendField } from '@zakodium/nmrium-core'; import { useCallback, useMemo } from 'react'; import { useFormContext, useWatch } from 'react-hook-form'; import { FaPlus, FaRegTrashAlt } from 'react-icons/fa'; import type { CellProps } from 'react-table'; import { useChartData } from '../../../context/ChartContext.js'; import { CheckController } from '../../../elements/CheckController.js'; import { Input2Controller } from '../../../elements/Input2Controller.js'; import type { Column } from '../../../elements/ReactTable/ReactTable.js'; import ReactTable from '../../../elements/ReactTable/ReactTable.js'; import { convertPathArrayToString } from '../../../utility/convertPathArrayToString.js'; import { getSpectraObjectPaths } from '../../../utility/getSpectraObjectPaths.js'; function LegendsPreferences() { const { setValue, control } = useFormContext(); const { data } = useChartData(); const { datalist, paths } = useMemo( () => getSpectraObjectPaths(data), [data], ); const fields = useWatch({ name: 'legendsFields' }); const addHandler = useCallback( (data: readonly any[], index: number) => { let columns: any[] = []; const emptyField = { jpath: null, visible: true, }; if (data && Array.isArray(data)) { columns = [...data.slice(0, index), emptyField, ...data.slice(index)]; } else { columns.push(emptyField); } setValue('legendsFields', columns); }, [setValue], ); const deleteHandler = useCallback( (data: any, index: number) => { const _fields = data.filter( (_: any, columnIndex: any) => columnIndex !== index, ); setValue('legendsFields', _fields); }, [setValue], ); const COLUMNS = useMemo>>( () => [ { Header: '#', style: { width: '25px' }, accessor: (_, index) => index + 1, }, { Header: 'Field', Cell: ({ row }: CellProps) => { const predefinedColumn = row.original as PredefinedLegendField; if (predefinedColumn?.name) { return (

{predefinedColumn.label}

); } return ( (paths as any)[key] || null} mapValue={convertPathArrayToString} style={{ backgroundColor: 'transparent' }} noShadowBox /> ); }, }, { Header: 'Visible', style: { width: '30px', textAlign: 'center' }, Cell: ({ row }: CellProps) => ( ), }, { Header: '', style: { width: '70px' }, id: 'operation-button', Cell: ({ data, row }: CellProps) => { const record: any = row.original; return (
); }, }, ], [addHandler, control, datalist, deleteHandler, paths], ); return ( ); } export default LegendsPreferences;