import { Classes } from '@blueprintjs/core'; import { useField } from '@tanstack/react-form'; import { createColumnHelper } from '@tanstack/react-table'; import { EXTERNAL_API_KEYS } from '@zakodium/nmrium-core'; import { useMemo } from 'react'; import { FaPlus, FaRegTrashAlt } from 'react-icons/fa'; import { Button, withForm } from 'react-science/ui'; import type { z } from 'zod'; import { Select2 } from '../../../../elements/Select2.js'; import { CellActions, CellActionsButton, CellInput, TableSettings, } from '../ui/table.js'; import { TableSection } from '../ui/table_section.js'; import type { externalAPIWithUUIDValidation } from '../validation/external_apis_validation.js'; import { defaultGeneralSettingsFormValues } from '../validation.js'; type API = z.input; function emptyApi(): API { return { key: 'CT', serverLink: '', APIKey: '', uuid: crypto.randomUUID() }; } const itemsAPI = EXTERNAL_API_KEYS.slice(); export const ExternalApiTab = withForm({ defaultValues: defaultGeneralSettingsFormValues, render: function ExternalApiTab({ form }) { const { Field } = form; const field = useField({ form, name: 'externalAPIs', mode: 'array' }); const { name, pushValue, insertValue, removeValue } = field; const columns = useMemo(() => { const helper = createColumnHelper(); return [ helper.accessor('key', { header: 'Service', cell: ({ row: { index } }) => ( {(field) => ( field.handleChange(key)} intent={!field.state.meta.isValid ? 'danger' : 'none'} fill /> )} ), }), helper.accessor('serverLink', { header: 'Server link', cell: ({ row: { index } }) => ( {(field) => ( )} ), }), helper.accessor('APIKey', { header: 'API key', cell: ({ row: { index } }) => ( {(field) => ( )} ), }), helper.display({ id: 'actions', header: '', cell: ({ row: { index } }) => ( insertValue(index, emptyApi())} > removeValue(index)} > ), }), ]; }, [Field, insertValue, name, removeValue]); function onAddField() { pushValue(emptyApi()); } return ( Add an external API } > ); }, }); function getRowId(row: API) { return row.uuid; }