2023-03-04 11:36:58 +00:00
|
|
|
import { registerValidateRules } from '@formily/core';
|
|
|
|
import {
|
2023-07-07 06:35:22 +00:00
|
|
|
BlockSchemaComponentPlugin,
|
|
|
|
Plugin,
|
2023-03-04 11:36:58 +00:00
|
|
|
SchemaComponentOptions,
|
|
|
|
SchemaInitializerContext,
|
|
|
|
SettingsCenterProvider,
|
|
|
|
useAPIClient,
|
|
|
|
} from '@nocobase/client';
|
|
|
|
import JSON5 from 'json5';
|
|
|
|
import React, { useContext } from 'react';
|
|
|
|
import { ChartBlockEngine } from './ChartBlockEngine';
|
|
|
|
import { ChartBlockInitializer } from './ChartBlockInitializer';
|
|
|
|
import { ChartQueryMetadataProvider } from './ChartQueryMetadataProvider';
|
|
|
|
import './Icons';
|
|
|
|
import { lang } from './locale';
|
|
|
|
import { CustomSelect } from './select';
|
|
|
|
import { QueriesTable } from './settings/QueriesTable';
|
|
|
|
|
|
|
|
registerValidateRules({
|
|
|
|
json5: (value, rule) => {
|
|
|
|
if (!value) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const val = JSON5.parse(value);
|
|
|
|
if (!isNaN(val)) {
|
|
|
|
return {
|
|
|
|
type: 'error',
|
|
|
|
message: lang('Invalid JSON format'),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
return {
|
|
|
|
type: 'error',
|
|
|
|
message: lang('Invalid JSON format'),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-07-07 06:35:22 +00:00
|
|
|
const ChartsProvider = React.memo((props) => {
|
2023-03-04 11:36:58 +00:00
|
|
|
const api = useAPIClient();
|
2023-06-20 03:48:02 +00:00
|
|
|
const items = useContext<any>(SchemaInitializerContext);
|
2023-03-04 11:36:58 +00:00
|
|
|
const children = items.BlockInitializers.items[0].children;
|
|
|
|
if (children) {
|
|
|
|
const hasChartItem = children.some((child) => child?.component === 'ChartBlockInitializer');
|
|
|
|
if (!hasChartItem) {
|
|
|
|
children.push({
|
|
|
|
key: 'chart',
|
|
|
|
type: 'item',
|
|
|
|
icon: 'PieChartOutlined',
|
2023-06-30 12:49:44 +00:00
|
|
|
title: '{{t("Chart (Old)",{ns:"charts"})}}',
|
2023-03-04 11:36:58 +00:00
|
|
|
component: 'ChartBlockInitializer',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const validateSQL = (sql) => {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
api
|
|
|
|
.request({
|
|
|
|
url: 'chartsQueries:validate',
|
|
|
|
method: 'post',
|
|
|
|
data: {
|
|
|
|
sql,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
|
|
|
resolve(data?.data?.errorMessage);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
resolve('Invalid SQL');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<ChartQueryMetadataProvider>
|
|
|
|
<SettingsCenterProvider
|
|
|
|
settings={{
|
|
|
|
charts: {
|
|
|
|
title: '{{t("Charts", {ns:"charts"})}}',
|
|
|
|
icon: 'PieChartOutlined',
|
|
|
|
tabs: {
|
|
|
|
queries: {
|
|
|
|
title: '{{t("Queries", {ns:"charts"})}}',
|
|
|
|
component: QueriesTable,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<SchemaComponentOptions
|
|
|
|
scope={{ validateSQL }}
|
|
|
|
components={{ CustomSelect, ChartBlockInitializer, ChartBlockEngine }}
|
|
|
|
>
|
|
|
|
<SchemaInitializerContext.Provider value={items}>{props.children}</SchemaInitializerContext.Provider>
|
|
|
|
</SchemaComponentOptions>
|
|
|
|
</SettingsCenterProvider>
|
|
|
|
</ChartQueryMetadataProvider>
|
|
|
|
);
|
|
|
|
});
|
2023-07-07 06:35:22 +00:00
|
|
|
ChartsProvider.displayName = 'ChartsProvider';
|
2023-06-06 11:33:04 +00:00
|
|
|
|
2023-07-07 06:35:22 +00:00
|
|
|
export class ChartsPlugin extends Plugin {
|
|
|
|
async load() {
|
|
|
|
this.app.use(ChartsProvider);
|
|
|
|
|
|
|
|
// TODO: 主应用应该会有这个插件,按道理不需要再注册
|
|
|
|
this.app.pm.add(BlockSchemaComponentPlugin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ChartsPlugin;
|