signal-desktop/ts/state/ducks/user.ts

151 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-01-26 17:05:26 -06:00
// Copyright 2019-2022 Signal Messenger, LLC
2020-10-30 15:34:04 -05:00
// SPDX-License-Identifier: AGPL-3.0-only
import { trigger } from '../../shims/events';
import type { NoopActionType } from './noop';
import type { LocalizerType } from '../../types/Util';
import type { LocaleMessagesType } from '../../types/I18N';
import { ThemeType } from '../../types/Util';
2021-10-26 15:59:08 -07:00
import type { UUIDStringType } from '../../types/UUID';
import type { MenuOptionsType } from '../../types/menu';
2019-01-14 13:49:58 -08:00
// State
export type UserStateType = {
attachmentsPath: string;
stickersPath: string;
tempPath: string;
ourConversationId: string | undefined;
ourDeviceId: number | undefined;
2022-07-08 13:46:25 -07:00
ourACI: UUIDStringType | undefined;
ourPNI: UUIDStringType | undefined;
ourNumber: string | undefined;
2019-11-07 13:36:16 -08:00
platform: string;
regionCode: string | undefined;
2019-01-14 13:49:58 -08:00
i18n: LocalizerType;
localeMessages: LocaleMessagesType;
interactionMode: 'mouse' | 'keyboard';
isMainWindowMaximized: boolean;
isMainWindowFullScreen: boolean;
menuOptions: MenuOptionsType;
theme: ThemeType;
2022-01-26 17:05:26 -06:00
version: string;
2019-01-14 13:49:58 -08:00
};
// Actions
type UserChangedActionType = {
type: 'USER_CHANGED';
payload: {
ourConversationId?: string;
ourDeviceId?: number;
2022-07-08 13:46:25 -07:00
ourACI?: UUIDStringType;
ourPNI?: UUIDStringType;
ourNumber?: string;
regionCode?: string;
interactionMode?: 'mouse' | 'keyboard';
theme?: ThemeType;
isMainWindowMaximized?: boolean;
isMainWindowFullScreen?: boolean;
menuOptions?: MenuOptionsType;
2019-01-14 13:49:58 -08:00
};
};
export type UserActionType = UserChangedActionType;
2019-01-14 13:49:58 -08:00
// Action Creators
export const actions = {
userChanged,
manualReconnect,
2019-01-14 13:49:58 -08:00
};
function userChanged(attributes: {
interactionMode?: 'mouse' | 'keyboard';
ourConversationId?: string;
ourDeviceId?: number;
ourNumber?: string;
2022-07-08 13:46:25 -07:00
ourACI?: UUIDStringType;
ourPNI?: UUIDStringType;
regionCode?: string;
theme?: ThemeType;
isMainWindowMaximized?: boolean;
isMainWindowFullScreen?: boolean;
menuOptions?: MenuOptionsType;
2019-01-14 13:49:58 -08:00
}): UserChangedActionType {
return {
type: 'USER_CHANGED',
payload: attributes,
};
}
function manualReconnect(): NoopActionType {
trigger('manualConnect');
return {
type: 'NOOP',
payload: null,
};
}
2019-01-14 13:49:58 -08:00
// Reducer
export function getEmptyState(): UserStateType {
2019-01-14 13:49:58 -08:00
return {
attachmentsPath: 'missing',
stickersPath: 'missing',
tempPath: 'missing',
ourConversationId: 'missing',
ourDeviceId: 0,
2022-07-08 13:46:25 -07:00
ourACI: undefined,
ourPNI: undefined,
2019-01-14 13:49:58 -08:00
ourNumber: 'missing',
regionCode: 'missing',
2019-11-07 13:36:16 -08:00
platform: 'missing',
interactionMode: 'mouse',
isMainWindowMaximized: false,
isMainWindowFullScreen: false,
menuOptions: {
development: false,
devTools: false,
includeSetup: false,
isProduction: true,
platform: 'unknown',
},
theme: ThemeType.light,
i18n: Object.assign(
() => {
throw new Error('i18n not yet set up');
},
{
getLocale() {
throw new Error('i18n not yet set up');
},
}
),
localeMessages: {},
2022-01-26 17:05:26 -06:00
version: '0.0.0',
2019-01-14 13:49:58 -08:00
};
}
export function reducer(
state: Readonly<UserStateType> = getEmptyState(),
action: Readonly<UserActionType>
2019-01-14 13:49:58 -08:00
): UserStateType {
if (!state) {
return getEmptyState();
}
if (action.type === 'USER_CHANGED') {
const { payload } = action;
return {
...state,
...payload,
};
}
return state;
}