import React from 'react';
import FormStyledButton from '../widgets/FormStyledButton';
import { Formik, Form, useFormik, useFormikContext } from 'formik';
import styled from 'styled-components';
import Select from 'react-select';
import { FontIcon } from '../icons';
import {
FormButtonRow,
FormSubmit,
FormReactSelect,
FormConnectionSelect,
FormDatabaseSelect,
FormTablesSelect,
FormSelectField,
FormSchemaSelect,
} from '../utility/forms';
import { useConnectionList, useDatabaseList, useDatabaseInfo } from '../utility/metadataLoaders';
import TableControl, { TableColumn } from '../utility/TableControl';
import { TextField, SelectField } from '../utility/inputs';
import { getActionOptions, getTargetName, isFileStorage } from './createImpExpScript';
import getElectron from '../utility/getElectron';
import ErrorInfo from '../widgets/ErrorInfo';
import getAsArray from '../utility/getAsArray';
import axios from '../utility/axios';
import LoadingInfo from '../widgets/LoadingInfo';
const Container = styled.div`
max-height: 50vh;
overflow-y: scroll;
`;
const Wrapper = styled.div`
display: flex;
`;
const Column = styled.div`
margin: 10px;
flex: 1;
`;
const StyledSelect = styled(Select)`
// min-width: 350px;
`;
const OptionsWrapper = styled.div`
margin-left: 10px;
`;
const Label = styled.div`
margin: 5px;
margin-top: 15px;
color: #777;
`;
const SourceNameWrapper = styled.div`
display: flex;
justify-content: space-between;
`;
const TrashWrapper = styled.div`
&:hover {
background-color: #ccc;
}
cursor: pointer;
color: blue;
`;
function DatabaseSelector() {
const connections = useConnectionList();
const connectionOptions = React.useMemo(
() =>
(connections || []).map((conn) => ({
value: conn._id,
label: conn.displayName || conn.server,
})),
[connections]
);
// const databases = useDatabaseList({ conid: _id });
return (
<>
>
);
}
function getFileFilters(storageType) {
const res = [];
if (storageType == 'csv') res.push({ name: 'CSV files', extensions: ['csv'] });
if (storageType == 'jsonl') res.push({ name: 'JSON lines', extensions: ['jsonl'] });
if (storageType == 'excel') res.push({ name: 'MS Excel files', extensions: ['xlsx'] });
res.push({ name: 'All Files', extensions: ['*'] });
return res;
}
async function addFilesToSourceList(files, values, setFieldValue) {
const newSources = [];
const storage = values.sourceStorageType;
for (const file of getAsArray(files)) {
if (isFileStorage(storage)) {
if (storage == 'excel') {
const resp = await axios.get(`files/analyse-excel?filePath=${encodeURIComponent(file.full)}`);
/** @type {import('@dbgate/types').DatabaseInfo} */
const structure = resp.data;
for (const table of structure.tables) {
const sourceName = table.pureName;
newSources.push(sourceName);
setFieldValue(`sourceFile_${sourceName}`, {
fileName: file.full,
sheetName: table.pureName,
});
}
} else {
const sourceName = file.name;
newSources.push(sourceName);
setFieldValue(`sourceFile_${sourceName}`, {
fileName: file.full,
});
}
}
}
setFieldValue('sourceList', [...(values.sourceList || []).filter((x) => !newSources.includes(x)), ...newSources]);
}
function ElectronFilesInput() {
const { values, setFieldValue } = useFormikContext();
const electron = getElectron();
const [isLoading, setIsLoading] = React.useState(false);
const handleClick = async () => {
const files = electron.remote.dialog.showOpenDialogSync(electron.remote.getCurrentWindow(), {
properties: ['openFile', 'multiSelections'],
filters: getFileFilters(values.sourceStorageType),
});
if (files) {
const path = window.require('path');
try {
setIsLoading(true);
await addFilesToSourceList(
files.map((full) => ({
full,
...path.parse(full),
})),
values,
setFieldValue
);
} finally {
setIsLoading(false);
}
}
};
return (
<>
{isLoading && }
>
);
}
function FilesInput() {
const electron = getElectron();
if (electron) {
return ;
}
return ;
}
function SourceTargetConfig({
direction,
storageTypeField,
connectionIdField,
databaseNameField,
schemaNameField,
tablesField = undefined,
}) {
const types = [
{ value: 'database', label: 'Database', directions: ['source', 'target'] },
{ value: 'csv', label: 'CSV file(s)', directions: ['source', 'target'] },
{ value: 'jsonl', label: 'JSON lines file(s)', directions: ['source', 'target'] },
{ value: 'excel', label: 'MS Excel file(s)', directions: ['source'] },
];
const { values } = useFormikContext();
const storageType = values[storageTypeField];
return (
{direction == 'source' && }
{direction == 'target' && }
x.directions.includes(direction))} name={storageTypeField} />
{storageType == 'database' && (
<>
{tablesField && (
<>
>
)}
>
)}
{isFileStorage(storageType) && direction == 'source' && }
);
}
function SourceName({ name }) {
const { values, setFieldValue } = useFormikContext();
const handleDelete = () => {
setFieldValue(
'sourceList',
values.sourceList.filter((x) => x != name)
);
};
return (
{name}
);
}
export default function ImportExportConfigurator() {
const { values, setFieldValue } = useFormikContext();
const targetDbinfo = useDatabaseInfo({ conid: values.targetConnectionId, database: values.targetDatabaseName });
const { sourceList } = values;
return (
} />
(
setFieldValue(`actionType_${row}`, e.target.value)}
/>
)}
/>
(
setFieldValue(`targetName_${row}`, e.target.value)}
/>
)}
/>
);
}