Skip to content

Commit

Permalink
support enable or disable model error (#2693)
Browse files Browse the repository at this point in the history
* support-auto-open-model

* Spotless Apply

---------

Co-authored-by: Zzm0809 <[email protected]>
  • Loading branch information
Zzm0809 and Zzm0809 authored Dec 20, 2023
1 parent 177330d commit 8b6c469
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 9 deletions.
35 changes: 32 additions & 3 deletions dinky-web/src/components/RightContent/AvatarDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@
*/

import { chooseTenantSubmit, outLogin } from '@/services/BusinessCrud';
import { setTenantStorageAndCookie } from '@/utils/function';
import { ENABLE_MODEL_TIP } from '@/services/constants';
import {
getValueFromLocalStorage,
setKeyToLocalStorage,
setTenantStorageAndCookie
} from '@/utils/function';
import { l } from '@/utils/intl';
import { ErrorNotification, SuccessNotification } from '@/utils/messages';
import { ErrorNotification, SuccessNotification, WarningNotification } from '@/utils/messages';
import {
BugOutlined,
ClearOutlined,
CloseCircleOutlined,
LogoutOutlined,
TeamOutlined,
UserOutlined,
Expand All @@ -35,7 +42,7 @@ import { Avatar, Modal, Spin } from 'antd';
import { stringify } from 'querystring';
import { ItemType } from 'rc-menu/es/interface';
import type { MenuInfo } from 'rc-menu/lib/interface';
import { useCallback } from 'react';
import { useCallback, useState } from 'react';
import HeaderDropdown from '../HeaderDropdown';

export const loginOut = async () => {
Expand Down Expand Up @@ -120,6 +127,10 @@ const AvatarDropdown = () => {
});
const { initialState, setInitialState } = useModel('@@initialState');

const [enableModelTip, setEnableModelTip] = useState<boolean>(
Boolean(getValueFromLocalStorage(ENABLE_MODEL_TIP))
);

const loginOutHandler = useCallback(
async (event: MenuInfo) => {
const { key } = event;
Expand Down Expand Up @@ -188,6 +199,16 @@ const AvatarDropdown = () => {
return chooseTenantList;
};

const handleClickEnableModelTip = () => {
setKeyToLocalStorage(ENABLE_MODEL_TIP, String(!enableModelTip));
setEnableModelTip(!enableModelTip);
if (!enableModelTip) {
SuccessNotification(l('menu.account.openGlobalMessageTip'));
} else {
WarningNotification(l('menu.account.closeGlobalMessageTip'));
}
};

const menuItems = [
{
key: 'currentTenant',
Expand Down Expand Up @@ -226,6 +247,14 @@ const AvatarDropdown = () => {
window.location.reload();
}
},
{
key: 'enableModelTip',
icon: enableModelTip ? <CloseCircleOutlined /> : <BugOutlined />,
label: enableModelTip
? l('menu.account.closeGlobalMessage')
: l('menu.account.openGlobalMessage'),
onClick: () => handleClickEnableModelTip()
},
{
type: 'divider' as const
},
Expand Down
6 changes: 6 additions & 0 deletions dinky-web/src/locales/en-US/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export default {
'menu.account.changePassword': 'Change Password',
'menu.account.checkTenant': 'Switch Tenant',
'menu.account.checkTenantConfirm': 'Are you sure to switch tenant [ {tenantCode} ] ?',
'menu.account.closeGlobalMessage': 'Close developer mode',
'menu.account.openGlobalMessage': 'Turn on developer mode',
'menu.account.closeGlobalMessageTip':
'You have turned off the developer mode and will not receive all exception information returned by the background. You can only receive warning information.',
'menu.account.openGlobalMessageTip':
'Developer mode is turned on. You can receive all exception/warning information returned by the background. If you do not want to receive the exception stack, please turn off this function.',

'menu.datastudio': 'Data Studio',

Expand Down
6 changes: 6 additions & 0 deletions dinky-web/src/locales/zh-CN/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export default {
'menu.account.changePassword': '修改密码',
'menu.account.checkTenant': '切换租户',
'menu.account.checkTenantConfirm': '你确定切换租户【{tenantCode}】吗?',
'menu.account.closeGlobalMessage': '关闭开发者模式',
'menu.account.openGlobalMessage': '开启开发者模式',
'menu.account.closeGlobalMessageTip':
'你已关闭开发者模式,将不会接收到后台返回的所有异常信息,只能收到警告信息.',
'menu.account.openGlobalMessageTip':
'已开启开发者模式,你可以接收到后台返回的所有异常/警告信息,如果你不想接收异常堆栈,请关闭此功能.',

'menu.datastudio': '数据开发',

Expand Down
15 changes: 15 additions & 0 deletions dinky-web/src/pages/Other/Login/function.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
*
*/

import { ENABLE_MODEL_TIP } from '@/services/constants';
import {
hasKeyofLocalStorage,
setKeyToLocalStorage,
setLocalThemeToStorage
} from '@/utils/function';
import { history } from '@@/core/history';

/** 此方法会跳转到 redirect 参数所在的位置 */
Expand All @@ -32,3 +38,12 @@ export const redirectToLogin = () => {
//TODO: 弹出确认框
window.location.href = '/login';
};

export const initSomeThing = () => {
// 初始化设置主题
setLocalThemeToStorage();
// 取出本地存储是否有启用消息提示的 key , 没有的话设置一下
if (hasKeyofLocalStorage(ENABLE_MODEL_TIP)) {
setKeyToLocalStorage(ENABLE_MODEL_TIP, 'true');
}
};
9 changes: 4 additions & 5 deletions dinky-web/src/pages/Other/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

import Footer from '@/components/Footer';
import ChooseModal from '@/pages/Other/Login/ChooseModal';
import { gotoRedirectUrl, redirectToLogin } from '@/pages/Other/Login/function';
import { gotoRedirectUrl, initSomeThing, redirectToLogin } from '@/pages/Other/Login/function';
import LangSwitch from '@/pages/Other/Login/LangSwitch';
import { chooseTenantSubmit, login, queryDataByParams } from '@/services/BusinessCrud';
import { API } from '@/services/data';
import { API_CONSTANTS } from '@/services/endpoints';
import { UserBaseInfo } from '@/types/AuthCenter/data';
import { setLocalThemeToStorage, setTenantStorageAndCookie } from '@/utils/function';
import { setTenantStorageAndCookie } from '@/utils/function';
import { useLocalStorage } from '@/utils/hook/useLocalStorage';
import { l } from '@/utils/intl';
import { ErrorMessage, SuccessMessageAsync } from '@/utils/messages';
Expand Down Expand Up @@ -90,8 +90,6 @@ const Login: React.FC = () => {
tenantCode: chooseTenantResult.data.tenantCode
})
);
// 补偿设置,设置主题色
setLocalThemeToStorage();
/**
* After the selection is complete, refresh all user information
*/
Expand Down Expand Up @@ -177,7 +175,8 @@ const Login: React.FC = () => {
await handleChooseTenant(result);
handleTenantVisible(false);
};

// 进入登录页初始化一些东西
initSomeThing();
return (
<div className={containerClassName}>
<HelmetTitle />
Expand Down
10 changes: 9 additions & 1 deletion dinky-web/src/requestErrorConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
*
*/

import { ENABLE_MODEL_TIP } from '@/services/constants';
import { API_CONSTANTS } from '@/services/endpoints';
import { getValueFromLocalStorage } from '@/utils/function';
import { l } from '@/utils/intl';
import { ErrorNotification, WarningNotification } from '@/utils/messages';
import { history } from '@@/core/history';
Expand All @@ -43,6 +45,8 @@ interface ResponseStructure {

const handleBizError = (result: ResponseStructure) => {
const { msg, code, data } = result;
console.log(JSON.stringify(msg));

switch (code) {
case ErrorCode.SUCCESS:
//don't deal with it, just be happy
Expand All @@ -52,7 +56,9 @@ const handleBizError = (result: ResponseStructure) => {
break;
case ErrorCode.EXCEPTION:
//TODO 可配置化,dev换弹出错误,release不弹
//ErrorNotification(JSON.stringify(data), l('app.response.error'));
if (Boolean(getValueFromLocalStorage(ENABLE_MODEL_TIP))) {
ErrorNotification(JSON.stringify(data), l('app.response.error'));
}
break;
case ErrorCode.PARAMS_ERROR:
ErrorNotification(msg, l('app.response.error'));
Expand Down Expand Up @@ -83,6 +89,8 @@ export const errorConfig: RequestConfig = {
},
// 错误接收及处理
errorHandler: (error: any, opts: any) => {
console.log(error);

if (opts?.skipErrorHandler) throw error;
// 我们的 errorThrower 抛出的错误。
if (error.name === 'BizError') {
Expand Down
2 changes: 2 additions & 0 deletions dinky-web/src/services/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export const LANGUAGE_EN = 'en-US';
export const DATE_FORMAT = 'YYYY-MM-DD';
export const DATETIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';

export const ENABLE_MODEL_TIP = 'enableModelTip';

/**
* REQUEST METHOD CONSTANTS
*/
Expand Down
4 changes: 4 additions & 0 deletions dinky-web/src/utils/function.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export function setKeyToLocalStorage(key: string, value: string) {
localStorage.setItem(key, value);
}

export function hasKeyofLocalStorage(key: string): boolean {
return localStorage.getItem(key) === undefined || localStorage.getItem(key) === null;
}

/**
* get value by localStorage's key
* @param key
Expand Down

0 comments on commit 8b6c469

Please sign in to comment.