import { Checkbox, Classes } from '@blueprintjs/core'; import styled from '@emotion/styled'; import type { CustomWorkspaces, Database } from '@zakodium/nmrium-core'; import { useCallback, useMemo } from 'react'; import { Controller, useFormContext } from 'react-hook-form'; import { FaLink, 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 type { NMRiumWorkspace } from '../../../main/index.js'; import type { WorkspaceWithSource } from '../../../reducer/preferences/preferencesReducer.js'; import { getPreferencesByWorkspace } from '../../../reducer/preferences/utilities/getPreferencesByWorkspace.js'; import { isGoogleDocument } from '../../../utility/isGoogleDocument.js'; import { Section } from '../general_settings.js'; const StyledButton = styled(Button, { shouldForwardProp(propName) { return propName !== 'marginHorizontal'; }, })<{ marginHorizontal: number }>` margin: 0 ${({ marginHorizontal }) => marginHorizontal}px; `; function getKeyPath( index: number, key: T, ): `databases.data.${number}.${T}` { return `databases.data.${index}.${key}`; } interface DatabasesTabContentProps { currentWorkspace: NMRiumWorkspace; originalWorkspaces: CustomWorkspaces; } export default function DatabasesTabContent({ currentWorkspace, originalWorkspaces, }: DatabasesTabContentProps) { const { setValue, register, control, watch } = useFormContext(); const databases: Database[] = watch('databases.data') || []; const addHandler = useCallback( (data: readonly any[], index = 0) => { let databases: any[] = []; const emptyField = { key: crypto.randomUUID(), label: '', url: '', enabled: true, }; if (data && Array.isArray(data)) { databases = [...data.slice(0, index), emptyField, ...data.slice(index)]; } else { databases.push(emptyField); } setValue('databases.data', databases); }, [setValue], ); const deleteHandler = useCallback( (data: any, index: number) => { const databases = data.filter( (_: any, columnIndex: any) => columnIndex !== index, ); setValue('databases.data', databases); }, [setValue], ); const COLUMNS = useMemo>>( () => [ { Header: '#', style: { width: '25px', textAlign: 'center' }, accessor: (_, index) => index + 1, }, { Header: 'Label', style: { minWidth: '150px' }, Cell: ({ row }: CellProps) => { const name = getKeyPath(row.index, 'label'); return ( ); }, }, { Header: 'URL', style: { width: '100%' }, Cell: ({ row }: CellProps) => { const name = getKeyPath(row.index, 'url'); return ( ); }, }, { Header: 'Enabled', style: { width: '30px', textAlign: 'center' }, Cell: ({ row }: CellProps) => ( ), }, { Header: 'Auto Load', style: { width: '30px', textAlign: 'center' }, Cell: ({ row }: CellProps) => ( { const { value, name } = field; const databaseKey = row.original.key; return ( { setValue(name, databaseKey === value ? '' : databaseKey); }} /> ); }} /> ), }, { Header: '', style: { maxWidth: '100px', width: '85px' }, id: 'op-buttons', Cell: ({ data, row }: CellProps) => { const { index, original: record } = row; return (
deleteHandler(data, index)} > {isGoogleDocument(record.url) && ( )}
); }, }, ], [addHandler, control, deleteHandler, register, setValue], ); function resetHandler() { const workSpaceDisplayPreferences = getPreferencesByWorkspace( currentWorkspace, originalWorkspaces, ); const database = workSpaceDisplayPreferences.databases.data; setValue('databases.data', database); } return ( { return ( addHandler(databases)} /> ); }} > ); } interface DataBaseHeaderProps { onReset: () => void; onAdd: () => void; text: string; } function DataBaseHeader(props: DataBaseHeaderProps) { const { onReset, onAdd, text } = props; return (

{text}

); }