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

150 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-06-14 19:01:00 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { ThunkAction } from 'redux-thunk';
import type { ReadonlyDeep } from 'type-fest';
import type { StateType as RootStateType } from '../reducer';
import type { BoundActionCreatorsMapObject } from '../../hooks/useBoundActions';
import { useBoundActions } from '../../hooks/useBoundActions';
import * as log from '../../logging/log';
2024-09-04 02:56:13 +00:00
import {
START_INSTALLER,
type StartInstallerActionType,
SHOW_BACKUP_IMPORT,
type ShowBackupImportActionType,
} from './installer';
2021-06-14 19:01:00 +00:00
// State
export enum AppViewType {
Blank = 'Blank',
Inbox = 'Inbox',
Installer = 'Installer',
Standalone = 'Standalone',
}
2024-09-04 02:56:13 +00:00
export type AppStateType = ReadonlyDeep<{
hasInitialLoadCompleted: boolean;
appView: AppViewType;
}>;
2021-06-14 19:01:00 +00:00
// Actions
const INITIAL_LOAD_COMPLETE = 'app/INITIAL_LOAD_COMPLETE';
const OPEN_INBOX = 'app/OPEN_INBOX';
2024-09-04 02:56:13 +00:00
export const OPEN_INSTALLER = 'app/OPEN_INSTALLER';
2021-06-14 19:01:00 +00:00
const OPEN_STANDALONE = 'app/OPEN_STANDALONE';
type InitialLoadCompleteActionType = ReadonlyDeep<{
2021-06-14 19:01:00 +00:00
type: typeof INITIAL_LOAD_COMPLETE;
}>;
2021-06-14 19:01:00 +00:00
type OpenInboxActionType = ReadonlyDeep<{
2021-06-14 19:01:00 +00:00
type: typeof OPEN_INBOX;
}>;
2021-06-14 19:01:00 +00:00
type OpenStandaloneActionType = ReadonlyDeep<{
2021-06-14 19:01:00 +00:00
type: typeof OPEN_STANDALONE;
}>;
2021-06-14 19:01:00 +00:00
export type AppActionType = ReadonlyDeep<
2024-09-04 02:56:13 +00:00
InitialLoadCompleteActionType | OpenInboxActionType | OpenStandaloneActionType
>;
2021-06-14 19:01:00 +00:00
export const actions = {
initialLoadComplete,
openInbox,
openStandalone,
};
export const useAppActions = (): BoundActionCreatorsMapObject<typeof actions> =>
useBoundActions(actions);
2021-06-14 19:01:00 +00:00
function initialLoadComplete(): InitialLoadCompleteActionType {
return {
type: INITIAL_LOAD_COMPLETE,
};
}
function openInbox(): ThunkAction<
void,
RootStateType,
unknown,
OpenInboxActionType
> {
return async dispatch => {
log.info('open inbox');
2021-06-14 19:01:00 +00:00
await window.ConversationController.load();
2021-06-14 19:01:00 +00:00
dispatch({
type: OPEN_INBOX,
});
};
}
function openStandalone(): ThunkAction<
void,
RootStateType,
unknown,
OpenStandaloneActionType
> {
return dispatch => {
if (window.getEnvironment() === 'production') {
return;
}
2023-01-13 00:24:59 +00:00
window.IPC.addSetupMenuItems();
2021-06-14 19:01:00 +00:00
dispatch({
type: OPEN_STANDALONE,
});
};
}
// Reducer
export function getEmptyState(): AppStateType {
return {
appView: AppViewType.Blank,
hasInitialLoadCompleted: false,
};
}
export function reducer(
state: Readonly<AppStateType> = getEmptyState(),
2024-09-04 02:56:13 +00:00
action: Readonly<
AppActionType | StartInstallerActionType | ShowBackupImportActionType
>
2021-06-14 19:01:00 +00:00
): AppStateType {
if (action.type === OPEN_INBOX) {
return {
...state,
appView: AppViewType.Inbox,
};
}
if (action.type === INITIAL_LOAD_COMPLETE) {
return {
...state,
hasInitialLoadCompleted: true,
};
}
if (action.type === OPEN_STANDALONE) {
return {
...state,
appView: AppViewType.Standalone,
};
}
2024-09-04 02:56:13 +00:00
// Foreign action
if (action.type === START_INSTALLER || action.type === SHOW_BACKUP_IMPORT) {
2024-08-27 21:00:41 +00:00
return {
...state,
2024-09-04 02:56:13 +00:00
appView: AppViewType.Installer,
2024-08-27 21:00:41 +00:00
};
}
2021-06-14 19:01:00 +00:00
return state;
}