2023-03-04 11:36:58 +00:00
|
|
|
import { registerValidateRules } from '@formily/core';
|
|
|
|
import {
|
|
|
|
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-06-06 11:33:04 +00:00
|
|
|
const Charts = 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-06-06 11:33:04 +00:00
|
|
|
Charts.displayName = 'Charts';
|
|
|
|
|
|
|
|
export default Charts;
|