import { Classes } from '@blueprintjs/core'; import type { ExternalAPI } from '@zakodium/nmrium-core'; import { EXTERNAL_API_KEYS } 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 { GroupPane } from '../../../elements/GroupPane.js'; import { Input2Controller } from '../../../elements/Input2Controller.js'; import type { Column } from '../../../elements/ReactTable/ReactTable.js'; import ReactTable from '../../../elements/ReactTable/ReactTable.js'; import { Select2Controller } from '../../../elements/Select2Controller.js'; import type { WorkspaceWithSource } from '../../../reducer/preferences/preferencesReducer.js'; import { Section } from '../general_settings.js'; function getKeyPath( index: number, key: T, ): `externalAPIs.${number}.${T}` { return `externalAPIs.${index}.${key}`; } export function ExternalAPIsTabContent() { const { control, setValue, watch } = useFormContext(); const fields: ExternalAPI[] = watch('externalAPIs') || []; const addHandler = useCallback( (data: readonly any[], index: number) => { let columns: any[] = []; const emptyField = { key: '', serverLink: '', APIKey: '', }; if (data && Array.isArray(data)) { columns = [...data.slice(0, index), emptyField, ...data.slice(index)]; } else { columns.push(emptyField); } setValue('externalAPIs', columns); }, [setValue], ); const deleteHandler = useCallback( (data: any, index: number) => { const _fields = data.filter( (_: any, columnIndex: any) => columnIndex !== index, ); setValue('externalAPIs', _fields); }, [setValue], ); const COLUMNS = useMemo>>( () => [ { Header: '#', style: { width: '25px', textAlign: 'center' }, accessor: (_: any, index) => index + 1, }, { Header: 'Service', Cell: ({ row }: CellProps) => { const name = getKeyPath(row.index, 'key'); return ( placeholder="Select API provider" control={control} name={name} items={[...EXTERNAL_API_KEYS]} itemTextKey="description" itemValueKey="key" fill /> ); }, }, { Header: 'Server link', Cell: ({ row }: CellProps) => { const name = getKeyPath(row.index, 'serverLink'); return ( ); }, }, { Header: 'API key', Cell: ({ row }: CellProps) => { const name = getKeyPath(row.index, 'APIKey'); return ( ); }, }, { Header: '', style: { width: '60px' }, id: 'op-buttons', Cell: ({ data, row }: CellProps) => { const record: any = row.original; return (
{!record?.name && ( )}
); }, }, ], [addHandler, control, deleteHandler], ); return ( { return
addHandler(fields, 0)} />; }} > ); } interface HeaderProps { onAdd: () => void; text: string; } function Header(props: HeaderProps) { const { onAdd, text } = props; return (

{text}

); }