From fca42b6346c12f16d85f5a0add7a27b8a2439a9f Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Fri, 8 May 2020 14:44:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(web):=20=E5=A2=9E=E5=8A=A0=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E9=A1=B9=E4=B8=8D=E5=8F=91=E9=80=81=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/shared/api/event.ts | 9 +++++++ src/shared/project.config.ts | 29 ++++++++++++--------- src/shared/redux/helpers/settings.ts | 27 +++++++++++++++++++ src/shared/redux/types/settings.ts | 14 +++++++--- src/web/components/modal/SystemSettings.tsx | 14 ++++++++++ 5 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 src/shared/redux/helpers/settings.ts diff --git a/src/shared/api/event.ts b/src/shared/api/event.ts index 7e9d41908..f48c0f68d 100644 --- a/src/shared/api/event.ts +++ b/src/shared/api/event.ts @@ -2,6 +2,7 @@ import _throttle from 'lodash/throttle'; import config from '@shared/project.config'; import { ConverseType } from '@redux/types/chat'; import * as trpgApi from './trpg.api'; +import { getSystemSettings } from '@redux/helpers/settings'; const api = trpgApi.getInstance(); /** @@ -9,6 +10,10 @@ const api = trpgApi.getInstance(); */ export const sendStartWriting = _throttle( (type: ConverseType = 'user', uuid: string, currentText?: string) => { + if (getSystemSettings('disableSendWritingState', false) === true) { + return; + } + return api.emit('chat::startWriting', { type, uuid, currentText }); }, config.chat.isWriting.throttle, @@ -20,6 +25,10 @@ export const sendStartWriting = _throttle( */ export const sendStopWriting = _throttle( (type: ConverseType = 'user', uuid: string) => { + if (getSystemSettings('disableSendWritingState', false) === true) { + return; + } + return api.emit('chat::stopWriting', { type, uuid }); }, config.chat.isWriting.throttle, diff --git a/src/shared/project.config.ts b/src/shared/project.config.ts index a41321619..c5c5e05ad 100644 --- a/src/shared/project.config.ts +++ b/src/shared/project.config.ts @@ -98,12 +98,24 @@ interface ProjectConfig { blog: string; portal: string; }; - defaultSettings: { - user: {}; - system: {}; - }; + defaultSettings: DefaultSettings; } +/** + * 默认设置 + */ +const defaultSettings = { + user: { + favoriteDice: [], + }, + system: { + notification: true, + disableSendWritingState: false, + }, +}; + +export type DefaultSettings = typeof defaultSettings; + const config: ProjectConfig = { version: require('../../package.json').version, environment, @@ -210,14 +222,7 @@ const config: ProjectConfig = { blog: 'https://trpgdoc.moonrailgun.com/blog/', portal: portalUrl, }, - defaultSettings: { - user: { - favoriteDice: [], - }, - system: { - notification: true, - }, - }, + defaultSettings, }; config.file.url = `${config.file.protocol}://${config.file.host}:${config.file.port}`; config.url.api = config.file.url; diff --git a/src/shared/redux/helpers/settings.ts b/src/shared/redux/helpers/settings.ts new file mode 100644 index 000000000..964ec77ec --- /dev/null +++ b/src/shared/redux/helpers/settings.ts @@ -0,0 +1,27 @@ +import { getStoreState } from '@redux/configureStore/helper'; +import { SettingType } from '@redux/types/settings'; +import _get from 'lodash/get'; + +/** + * 获取用户设置 + */ +export function getUserSettings( + field: string, + defaultValue: T +): T { + const state = getStoreState(); + + return (_get(state, ['settings', 'user', field]) as T) ?? defaultValue; +} + +/** + * 获取系统设置 + */ +export function getSystemSettings( + field: string, + defaultValue: T +): T { + const state = getStoreState(); + + return (_get(state, ['settings', 'system', field]) as T) ?? defaultValue; +} diff --git a/src/shared/redux/types/settings.ts b/src/shared/redux/types/settings.ts index c08092b31..d187854ef 100644 --- a/src/shared/redux/types/settings.ts +++ b/src/shared/redux/types/settings.ts @@ -1,12 +1,18 @@ +import { DefaultSettings } from '@shared/project.config'; + type NotificationType = 'granted' | 'denied' | 'default'; -export interface ServerConfig { - [name: string]: string | boolean | number | string[]; +export type SettingType = string | boolean | number | string[]; + +interface CustomConfig { + [name: string]: SettingType; } +export type ServerConfig = CustomConfig; + export interface SettingsState { - user: any; - system: any; + user: DefaultSettings['user'] & CustomConfig; + system: DefaultSettings['system'] & CustomConfig; notificationPermission: NotificationType; config: ServerConfig; // 服务端配置 } diff --git a/src/web/components/modal/SystemSettings.tsx b/src/web/components/modal/SystemSettings.tsx index 07cc04215..efe61a4ad 100644 --- a/src/web/components/modal/SystemSettings.tsx +++ b/src/web/components/modal/SystemSettings.tsx @@ -39,6 +39,13 @@ const SystemSettings: React.FC = TMemo(() => { [dispatch] ); + const handleSetDisableSendWritingState = useCallback( + (isChecked: boolean) => { + dispatch(setSystemSettings({ disableSendWritingState: isChecked })); + }, + [dispatch] + ); + return ( @@ -48,6 +55,13 @@ const SystemSettings: React.FC = TMemo(() => { onChange={handleRequestNotificationPermission} /> + + + +