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

125 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-01-26 23:05:26 +00:00
// Copyright 2019-2022 Signal Messenger, LLC
2020-10-30 20:34:04 +00: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 { ThemeType } from '../../types/Util';
2021-10-26 22:59:08 +00:00
import type { UUIDStringType } from '../../types/UUID';
2019-01-14 21:49:58 +00:00
// State
export type UserStateType = {
attachmentsPath: string;
stickersPath: string;
tempPath: string;
ourConversationId: string;
ourDeviceId: number;
2021-10-26 22:59:08 +00:00
ourUuid: UUIDStringType;
2019-01-14 21:49:58 +00:00
ourNumber: string;
2019-11-07 21:36:16 +00:00
platform: string;
2019-01-14 21:49:58 +00:00
regionCode: string;
i18n: LocalizerType;
interactionMode: 'mouse' | 'keyboard';
theme: ThemeType;
2022-01-26 23:05:26 +00:00
version: string;
2019-01-14 21:49:58 +00:00
};
// Actions
type UserChangedActionType = {
type: 'USER_CHANGED';
payload: {
ourConversationId?: string;
ourDeviceId?: number;
2021-10-26 22:59:08 +00:00
ourUuid?: UUIDStringType;
ourNumber?: string;
regionCode?: string;
interactionMode?: 'mouse' | 'keyboard';
theme?: ThemeType;
2019-01-14 21:49:58 +00:00
};
};
export type UserActionType = UserChangedActionType;
2019-01-14 21:49:58 +00:00
// Action Creators
export const actions = {
userChanged,
manualReconnect,
2019-01-14 21:49:58 +00:00
};
function userChanged(attributes: {
interactionMode?: 'mouse' | 'keyboard';
ourConversationId?: string;
ourDeviceId?: number;
ourNumber?: string;
2021-10-26 22:59:08 +00:00
ourUuid?: UUIDStringType;
regionCode?: string;
theme?: ThemeType;
2019-01-14 21:49:58 +00:00
}): UserChangedActionType {
return {
type: 'USER_CHANGED',
payload: attributes,
};
}
function manualReconnect(): NoopActionType {
trigger('manualConnect');
return {
type: 'NOOP',
payload: null,
};
}
2019-01-14 21:49:58 +00:00
// Reducer
export function getEmptyState(): UserStateType {
2019-01-14 21:49:58 +00:00
return {
attachmentsPath: 'missing',
stickersPath: 'missing',
tempPath: 'missing',
ourConversationId: 'missing',
ourDeviceId: 0,
2021-10-26 22:59:08 +00:00
ourUuid: '00000000-0000-4000-8000-000000000000',
2019-01-14 21:49:58 +00:00
ourNumber: 'missing',
regionCode: 'missing',
2019-11-07 21:36:16 +00:00
platform: 'missing',
interactionMode: 'mouse',
theme: ThemeType.light,
i18n: Object.assign(
() => {
throw new Error('i18n not yet set up');
},
{
getLocale() {
throw new Error('i18n not yet set up');
},
}
),
2022-01-26 23:05:26 +00:00
version: '0.0.0',
2019-01-14 21:49:58 +00:00
};
}
export function reducer(
state: Readonly<UserStateType> = getEmptyState(),
action: Readonly<UserActionType>
2019-01-14 21:49:58 +00:00
): UserStateType {
if (!state) {
return getEmptyState();
}
if (action.type === 'USER_CHANGED') {
const { payload } = action;
return {
...state,
...payload,
};
}
return state;
}