nocobase/packages/plugins/@nocobase/plugin-data-source-manager/src/client/component/CreateDatabaseConnectAction.tsx

110 lines
4.0 KiB
TypeScript
Raw Normal View History

2024-01-22 07:02:27 +00:00
import { PlusOutlined } from '@ant-design/icons';
import { uid } from '@formily/shared';
import { ActionContext, SchemaComponent, useCompile, usePlugin } from '@nocobase/client';
import { Button, Dropdown } from 'antd';
import _ from 'lodash';
import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import PluginDatabaseConnectionsClient from '../';
import { NAMESPACE } from '../locale';
import { useTestConnectionAction } from '../hooks';
export const CreateDatabaseConnectAction = () => {
const [schema, setSchema] = useState({});
const plugin = usePlugin(PluginDatabaseConnectionsClient);
const compile = useCompile();
const [visible, setVisible] = useState(false);
const { t } = useTranslation();
const [dialect, setDialect] = useState(null);
const useDialectDataSource = (field) => {
2024-01-22 12:55:55 +00:00
const options = [...plugin.databaseTypes.keys()].map((key) => {
const databaseType = plugin.databaseTypes.get(key);
2024-01-22 07:02:27 +00:00
return {
2024-01-22 12:55:55 +00:00
value: databaseType.name,
label: compile(databaseType.label),
2024-01-22 07:02:27 +00:00
};
});
field.dataSource = options;
};
return (
<div>
<ActionContext.Provider value={{ visible, setVisible }}>
<Dropdown
menu={{
onClick(info) {
const databaseType = plugin.databaseTypes.get(info.key);
setDialect(info.key);
setVisible(true);
setSchema({
type: 'object',
properties: {
[uid()]: {
type: 'void',
'x-component': 'Action.Drawer',
'x-decorator': 'Form',
'x-decorator-props': {
initialValue: {
2024-01-22 12:55:55 +00:00
dialect: info.key,
2024-01-22 07:02:27 +00:00
},
},
2024-01-22 12:55:55 +00:00
title: compile("{{t('Add new')}}") + ' - ' + compile(databaseType.label),
2024-01-22 07:02:27 +00:00
properties: {
2024-01-22 12:55:55 +00:00
body: {
type: 'void',
'x-component': databaseType.DataSourceSettingsForm,
},
2024-01-22 07:02:27 +00:00
footer: {
type: 'void',
'x-component': 'Action.Drawer.Footer',
properties: {
testConnectiion: {
title: `{{ t("Test Connection",{ ns: "${NAMESPACE}" }) }}`,
'x-component': 'Action',
'x-component-props': {
useAction: '{{ useTestConnectionAction }}',
},
},
cancel: {
title: '{{t("Cancel")}}',
'x-component': 'Action',
'x-component-props': {
useAction: '{{ cm.useCancelAction }}',
},
},
submit: {
title: '{{t("Submit")}}',
'x-component': 'Action',
'x-component-props': {
type: 'primary',
useAction: '{{ cm.useCreateAction }}',
},
},
},
},
},
},
},
});
},
2024-01-22 12:55:55 +00:00
items: [...plugin.databaseTypes.keys()].map((key) => {
const databaseType = plugin.databaseTypes.get(key);
2024-01-22 07:02:27 +00:00
return {
2024-01-22 12:55:55 +00:00
key: key,
label: compile(databaseType?.label),
2024-01-22 07:02:27 +00:00
};
}),
}}
>
<Button type={'primary'} icon={<PlusOutlined />}>
{t('Add new')}
</Button>
</Dropdown>
<SchemaComponent
scope={{ createOnly: false, useTestConnectionAction, dialect, useDialectDataSource }}
schema={schema}
/>
</ActionContext.Provider>
</div>
);
};