mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-26 15:36:28 +00:00
extensions refactor
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import React from 'react';
|
||||
import { useDropzone } from 'react-dropzone';
|
||||
import { findFileFormat } from '../fileformats';
|
||||
import ImportExportModal from '../modals/ImportExportModal';
|
||||
import useShowModal from '../modals/showModal';
|
||||
import { findFileFormat } from './fileformats';
|
||||
import resolveApi from './resolveApi';
|
||||
import useExtensions from './useExtensions';
|
||||
|
||||
const UploadsContext = React.createContext(null);
|
||||
|
||||
@@ -19,6 +20,7 @@ export function useUploadsProvider() {
|
||||
export function useUploadsZone() {
|
||||
const { uploadListener } = useUploadsProvider();
|
||||
const showModal = useShowModal();
|
||||
const extensions = useExtensions();
|
||||
|
||||
const onDrop = React.useCallback(
|
||||
(files) => {
|
||||
@@ -43,7 +45,7 @@ export function useUploadsZone() {
|
||||
if (uploadListener) {
|
||||
uploadListener(fileData);
|
||||
} else {
|
||||
if (findFileFormat(fileData.storageType)) {
|
||||
if (findFileFormat(extensions, fileData.storageType)) {
|
||||
showModal((modalState) => (
|
||||
<ImportExportModal
|
||||
uploadedFile={fileData}
|
||||
@@ -73,7 +75,7 @@ export function useUploadsZone() {
|
||||
// reader.readAsArrayBuffer(file);
|
||||
});
|
||||
},
|
||||
[uploadListener]
|
||||
[uploadListener, extensions]
|
||||
);
|
||||
const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop });
|
||||
|
||||
|
||||
@@ -1,6 +1,82 @@
|
||||
import { usePlugins } from '../plugins/PluginsProvider';
|
||||
import axios from './axios';
|
||||
import { FormSchemaSelect } from './forms';
|
||||
|
||||
export function useFileFormats() {
|
||||
const plugins = usePlugins();
|
||||
return [];
|
||||
const excelFormat = {
|
||||
storageType: 'excel',
|
||||
extension: 'xlsx',
|
||||
name: 'MS Excel',
|
||||
readerFunc: 'excelSheetReader',
|
||||
writerFunc: 'excelSheetWriter',
|
||||
|
||||
addFilesToSourceList: async (file, newSources, newValues) => {
|
||||
const resp = await axios.get(`files/analyse-excel?filePath=${encodeURIComponent(file.full)}`);
|
||||
const sheetNames = resp.data;
|
||||
for (const sheetName of sheetNames) {
|
||||
newSources.push(sheetName);
|
||||
newValues[`sourceFile_${sheetName}`] = {
|
||||
fileName: file.full,
|
||||
sheetName,
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
args: [
|
||||
{
|
||||
type: 'checkbox',
|
||||
name: 'singleFile',
|
||||
label: 'Create single file',
|
||||
direction: 'target',
|
||||
},
|
||||
],
|
||||
|
||||
getDefaultOutputName: (sourceName, values) => {
|
||||
if (values.target_excel_singleFile) {
|
||||
return sourceName;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
|
||||
getOutputParams: (sourceName, values) => {
|
||||
if (values.target_excel_singleFile) {
|
||||
return {
|
||||
sheetName: values[`targetName_${sourceName}`] || sourceName,
|
||||
fileName: 'data.xlsx',
|
||||
};
|
||||
}
|
||||
return null;
|
||||
},
|
||||
};
|
||||
|
||||
const jsonlFormat = {
|
||||
storageType: 'jsonl',
|
||||
extension: 'jsonl',
|
||||
name: 'JSON lines',
|
||||
readerFunc: 'jsonLinesReader',
|
||||
writerFunc: 'jsonLinesWriter',
|
||||
};
|
||||
|
||||
export function buildFileFormats(plugins) {
|
||||
const res = [excelFormat, jsonlFormat];
|
||||
for (const { content } of plugins) {
|
||||
const { fileFormats } = content;
|
||||
if (fileFormats) res.push(...fileFormats);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
export function findFileFormat(extensions, storageType) {
|
||||
return extensions.fileFormats.find((x) => x.storageType == storageType);
|
||||
}
|
||||
|
||||
export function getFileFormatDirections(format) {
|
||||
if (!format) return [];
|
||||
const res = [];
|
||||
if (format.readerFunc) res.push('source');
|
||||
if (format.writerFunc) res.push('target');
|
||||
return res;
|
||||
}
|
||||
|
||||
export function getDefaultFileFormat(extensions) {
|
||||
return extensions.fileFormats.find((x) => x.storageType == 'csv') || jsonlFormat;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import { useConnectionInfo, useConfig } from './metadataLoaders';
|
||||
import usePrevious from './usePrevious';
|
||||
import useNewQuery from '../query/useNewQuery';
|
||||
import useShowModal from '../modals/showModal';
|
||||
import useExtensions from './useExtensions';
|
||||
|
||||
function createGlobalState(defaultValue) {
|
||||
const Context = React.createContext(null);
|
||||
@@ -90,6 +91,7 @@ export function useAppObjectParams() {
|
||||
const currentArchive = useCurrentArchive();
|
||||
const showModal = useShowModal();
|
||||
const config = useConfig();
|
||||
const extensions = useExtensions();
|
||||
|
||||
return {
|
||||
setOpenedTabs,
|
||||
|
||||
@@ -88,7 +88,7 @@ const connectionListLoader = () => ({
|
||||
reloadTrigger: `connection-list-changed`,
|
||||
});
|
||||
|
||||
const insttalledPluginsLoader = () => ({
|
||||
const installedPluginsLoader = () => ({
|
||||
url: 'plugins/installed',
|
||||
params: {},
|
||||
reloadTrigger: `installed-plugins-changed`,
|
||||
@@ -251,8 +251,8 @@ export function useArchiveFolders(args) {
|
||||
}
|
||||
|
||||
export function getInstalledPlugins(args) {
|
||||
return getCore(insttalledPluginsLoader, args) || [];
|
||||
return getCore(installedPluginsLoader, args) || [];
|
||||
}
|
||||
export function useInstalledPlugins(args) {
|
||||
return useCore(insttalledPluginsLoader, args) || [];
|
||||
return useCore(installedPluginsLoader, args) || [];
|
||||
}
|
||||
|
||||
15
packages/web/src/utility/useExtensions.js
Normal file
15
packages/web/src/utility/useExtensions.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import React from 'react';
|
||||
import { usePlugins } from '../plugins/PluginsProvider';
|
||||
import { buildFileFormats } from './fileformats';
|
||||
|
||||
export default function useExtensions() {
|
||||
const plugins = usePlugins();
|
||||
const extensions = React.useMemo(
|
||||
() => ({
|
||||
plugins,
|
||||
fileFormats: buildFileFormats(plugins),
|
||||
}),
|
||||
[plugins]
|
||||
);
|
||||
return extensions;
|
||||
}
|
||||
Reference in New Issue
Block a user