feat: notice manager (#1131)

Reviewed-on: daoyoucloud/tachybase#1131
This commit is contained in:
sealday 2024-06-06 11:49:04 +08:00
parent 03a1dfdf1e
commit 58248c6ba5
5 changed files with 80 additions and 7 deletions

View File

@ -18,6 +18,7 @@ import { CSSVariableProvider } from '../style/css-variable';
import { AntdAppProvider, GlobalThemeProvider } from '../style/theme'; import { AntdAppProvider, GlobalThemeProvider } from '../style/theme';
import { AppSchemaComponentProvider } from './AppSchemaComponentProvider'; import { AppSchemaComponentProvider } from './AppSchemaComponentProvider';
import { AppComponent, BlankComponent, defaultAppComponents } from './components'; import { AppComponent, BlankComponent, defaultAppComponents } from './components';
import { NoticeManager } from './NoticesManager';
import type { Plugin } from './Plugin'; import type { Plugin } from './Plugin';
import { PluginManager, PluginType } from './PluginManager'; import { PluginManager, PluginType } from './PluginManager';
import { PluginSettingOptions, PluginSettingsManager } from './PluginSettingsManager'; import { PluginSettingOptions, PluginSettingsManager } from './PluginSettingsManager';
@ -79,6 +80,7 @@ export class Application {
public schemaInitializerManager: SchemaInitializerManager; public schemaInitializerManager: SchemaInitializerManager;
public schemaSettingsManager: SchemaSettingsManager; public schemaSettingsManager: SchemaSettingsManager;
public dataSourceManager: DataSourceManager; public dataSourceManager: DataSourceManager;
public noticeManager: NoticeManager;
public name: string; public name: string;
@ -117,11 +119,11 @@ export class Application {
this.pluginManager = new PluginManager(options.plugins, options.loadRemotePlugins, this); this.pluginManager = new PluginManager(options.plugins, options.loadRemotePlugins, this);
this.schemaInitializerManager = new SchemaInitializerManager(options.schemaInitializers, this); this.schemaInitializerManager = new SchemaInitializerManager(options.schemaInitializers, this);
this.dataSourceManager = new DataSourceManager(options.dataSourceManager, this); this.dataSourceManager = new DataSourceManager(options.dataSourceManager, this);
this.noticeManager = new NoticeManager(this);
this.addDefaultProviders(); this.addDefaultProviders();
this.addReactRouterComponents(); this.addReactRouterComponents();
this.addProviders(options.providers || []); this.addProviders(options.providers || []);
this.ws = new WebSocketClient(options.ws); this.ws = new WebSocketClient(options.ws, this);
this.ws.app = this;
this.pluginSettingsManager = new PluginSettingsManager(options.pluginSettings, this); this.pluginSettingsManager = new PluginSettingsManager(options.pluginSettings, this);
this.addRoutes(); this.addRoutes();
this.name = this.options.name || getSubAppName(options.publicPath) || 'main'; this.name = this.options.name || getSubAppName(options.publicPath) || 'main';
@ -231,6 +233,9 @@ export class Application {
this.notification[data.payload?.type || 'info']({ message: data.payload?.message }); this.notification[data.payload?.type || 'info']({ message: data.payload?.message });
return; return;
} }
if (data.type === 'notice') {
this.noticeManager.on(data.payload);
}
const maintaining = data.type === 'maintaining' && data.payload.code !== 'APP_RUNNING'; const maintaining = data.type === 'maintaining' && data.payload.code !== 'APP_RUNNING';
if (maintaining) { if (maintaining) {
this.maintaining = true; this.maintaining = true;

View File

@ -0,0 +1,67 @@
import React, { createContext, ReactNode, useContext } from 'react';
import { message, notification } from 'antd';
import { Application } from './Application';
import { useApp } from './hooks';
import { WebSocketClient } from './WebSocketClient';
export const enum NoticeLevel {
INFO = 'info',
WARNING = 'warning',
SUCCESS = 'success',
ERROR = 'error',
}
export const enum NoticeType {
STATUS = 'status',
TOAST = 'toast',
NOTIFICATION = 'notification',
}
export interface NoticeManagerContextProps {
manager: NoticeManager;
}
export const NoticeManagerContext = createContext<NoticeManagerContextProps>(null);
export const NoticeManagerProvider = ({ children }: { children: ReactNode }) => {
const app = useApp();
return (
<NoticeManagerContext.Provider value={{ manager: app.noticeManager }}>{children}</NoticeManagerContext.Provider>
);
};
export const useNoticeManager = () => {
return useContext(NoticeManagerContext);
};
export class NoticeManager {
private ws: WebSocketClient;
constructor(private app: Application) {
this.ws = app.ws;
}
on(data: { type: NoticeType; title?: string; content: string; level: NoticeLevel }) {
if (data.type === NoticeType.NOTIFICATION) {
this[data.type](data.title, data.content, data.level);
} else {
this[data.type](data.content, data.level);
}
}
status(content: string, level: NoticeLevel) {
// 常驻消息
}
toast(content: string, level: NoticeLevel) {
message[level](content);
}
notification(title: string, content: string, level: NoticeLevel) {
notification[level]({
message: title,
description: content,
});
}
}

View File

@ -18,13 +18,15 @@ export class WebSocketClient {
protected _reconnectTimes = 0; protected _reconnectTimes = 0;
protected events = []; protected events = [];
protected options: WebSocketClientOptions; protected options: WebSocketClientOptions;
app: Application;
enabled: boolean; enabled: boolean;
connected = false; connected = false;
serverDown = false; serverDown = false;
lastMessage = {}; lastMessage = {};
constructor(options: WebSocketClientOptions | boolean) { constructor(
options: WebSocketClientOptions | boolean,
private app: Application,
) {
if (!options) { if (!options) {
this.enabled = false; this.enabled = false;
return; return;

View File

@ -2,7 +2,6 @@ import { createStyles } from 'antd-style';
export const useStyles = createStyles(({ css, token }) => { export const useStyles = createStyles(({ css, token }) => {
return css` return css`
padding-top: ${token.paddingContentVerticalLG}px; padding-top: ${token.padding}px;
background-color: var(--colorBgDrawer);
`; `;
}); });

View File

@ -1,4 +1,5 @@
import { ToposortOptions } from '@tachybase/utils'; import { ToposortOptions } from '@tachybase/utils';
import { DataSource } from './data-source'; import { DataSource } from './data-source';
import { DataSourceFactory } from './data-source-factory'; import { DataSourceFactory } from './data-source-factory';
@ -35,7 +36,6 @@ export class DataSourceManager {
} }
} }
await next(); await next();
console.log('next....');
}; };
} }
} }