import { Classes } from '@blueprintjs/core'; import { useField } from '@tanstack/react-form'; import { createColumnHelper } from '@tanstack/react-table'; import { useCallback, useMemo } from 'react'; import { FaLink, FaPlus, FaRegTrashAlt } from 'react-icons/fa'; import { Button, withForm } from 'react-science/ui'; import type { z } from 'zod'; import { isGoogleDocument } from '../../../../utility/isGoogleDocument.ts'; import { CellActions, CellActionsButton, CellCheckbox, CellInput, TableSettings, } from '../ui/table.tsx'; import { TableSection } from '../ui/table_section.tsx'; import type { databasesValidation } from '../validation/database_tab_validation.ts'; import { defaultGeneralSettingsFormValues } from '../validation.ts'; type DatabaseForm = z.input; type DatabaseFormElement = DatabaseForm['data'][number]; function emptyDatabaseFormElement(): DatabaseFormElement { return { key: crypto.randomUUID(), label: '', url: '', enabled: true, }; } export const DatabaseTab = withForm({ defaultValues: defaultGeneralSettingsFormValues, render: function Render({ form }) { const field = useField({ form, name: 'databases.data', mode: 'array' }); const { removeValue, insertValue } = field; const { resetField, Field } = form; const handleDelete = useCallback( (index: number) => { removeValue(index); }, [removeValue], ); const handleAdd = useCallback( (index: number) => { insertValue(index, emptyDatabaseFormElement()); }, [insertValue], ); const handleReset = useCallback(() => { resetField('databases'); }, [resetField]); const COLUMNS = useMemo(() => { const helper = createColumnHelper(); return [ helper.accessor('label', { meta: { thStyle: { minWidth: '100px', }, }, cell: ({ row: { index } }) => ( {(subField) => ( )} ), }), helper.accessor('url', { header: 'URL', meta: { thStyle: { width: '100%', }, }, cell: ({ row: { index } }) => ( {(subField) => ( )} ), }), helper.accessor('enabled', { header: 'Enabled', cell: ({ row: { index } }) => ( {(subField) => ( { subField.handleChange(event.currentTarget.checked); }} onBlur={subField.handleBlur} /> )} ), }), helper.display({ id: 'defaultDatabase', header: 'Auto Load', cell: ({ row: { index, original: { key }, }, }) => ( {(subField) => ( { subField.handleChange( subField.state.value === key ? '' : key, ); }} onBlur={subField.handleBlur} /> )} ), }), helper.display({ id: 'actions', cell: ({ row: { index, original } }) => ( handleAdd(index + 1)} > handleDelete(index)} > {isGoogleDocument(original.url) && ( window.open(original.url, '_blank')} tooltipProps={{ content: 'Open document', compact: true }} > )} ), }), ]; }, [Field, handleAdd, handleDelete]); return ( } > ); }, }); function getRowId(row: DatabaseFormElement) { return row.key; }