2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ThunkAction } from 'redux-thunk';
|
2023-01-13 20:07:26 +00:00
|
|
|
import type { ReadonlyDeep } from 'type-fest';
|
2023-03-20 20:42:00 +00:00
|
|
|
import type { BoundActionCreatorsMapObject } from '../../hooks/useBoundActions';
|
|
|
|
import { useBoundActions } from '../../hooks/useBoundActions';
|
2020-02-12 21:30:58 +00:00
|
|
|
import * as updateIpc from '../../shims/updateIpc';
|
2021-08-19 22:56:29 +00:00
|
|
|
import { DialogType } from '../../types/Dialogs';
|
2021-10-06 21:59:34 +00:00
|
|
|
import { DAY } from '../../util/durations';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { StateType as RootStateType } from '../reducer';
|
2020-02-12 21:30:58 +00:00
|
|
|
|
|
|
|
// State
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type UpdatesStateType = ReadonlyDeep<{
|
2021-08-19 22:56:29 +00:00
|
|
|
dialogType: DialogType;
|
2020-02-21 00:09:50 +00:00
|
|
|
didSnooze: boolean;
|
2021-08-19 22:56:29 +00:00
|
|
|
downloadSize?: number;
|
|
|
|
downloadedSize?: number;
|
2020-02-21 00:09:50 +00:00
|
|
|
showEventsCount: number;
|
2021-08-19 22:56:29 +00:00
|
|
|
version?: string;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2020-02-12 21:30:58 +00:00
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
|
|
|
const DISMISS_DIALOG = 'updates/DISMISS_DIALOG';
|
|
|
|
const SHOW_UPDATE_DIALOG = 'updates/SHOW_UPDATE_DIALOG';
|
2020-02-21 00:09:50 +00:00
|
|
|
const SNOOZE_UPDATE = 'updates/SNOOZE_UPDATE';
|
2020-02-12 21:30:58 +00:00
|
|
|
const START_UPDATE = 'updates/START_UPDATE';
|
2021-08-19 22:56:29 +00:00
|
|
|
const UNSNOOZE_UPDATE = 'updates/UNSNOOZE_UPDATE';
|
2020-02-12 21:30:58 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type UpdateDialogOptionsType = ReadonlyDeep<{
|
2021-08-19 22:56:29 +00:00
|
|
|
downloadSize?: number;
|
|
|
|
downloadedSize?: number;
|
|
|
|
version?: string;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2020-02-12 21:30:58 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type DismissDialogActionType = ReadonlyDeep<{
|
2021-08-19 22:56:29 +00:00
|
|
|
type: typeof DISMISS_DIALOG;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2020-02-12 21:30:58 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type ShowUpdateDialogActionType = ReadonlyDeep<{
|
2021-08-19 22:56:29 +00:00
|
|
|
type: typeof SHOW_UPDATE_DIALOG;
|
|
|
|
payload: {
|
|
|
|
dialogType: DialogType;
|
|
|
|
otherState: UpdateDialogOptionsType;
|
|
|
|
};
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2020-02-12 21:30:58 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type SnoozeUpdateActionType = ReadonlyDeep<{
|
2021-08-19 22:56:29 +00:00
|
|
|
type: typeof SNOOZE_UPDATE;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2020-02-21 00:09:50 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type StartUpdateActionType = ReadonlyDeep<{
|
2021-08-19 22:56:29 +00:00
|
|
|
type: typeof START_UPDATE;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2021-08-19 22:56:29 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type UnsnoozeUpdateActionType = ReadonlyDeep<{
|
2021-08-19 22:56:29 +00:00
|
|
|
type: typeof UNSNOOZE_UPDATE;
|
|
|
|
payload: DialogType;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2020-02-12 21:30:58 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type UpdatesActionType = ReadonlyDeep<
|
2022-03-04 19:59:47 +00:00
|
|
|
| DismissDialogActionType
|
|
|
|
| ShowUpdateDialogActionType
|
2020-02-21 00:09:50 +00:00
|
|
|
| SnoozeUpdateActionType
|
2022-03-04 19:59:47 +00:00
|
|
|
| StartUpdateActionType
|
2023-01-13 20:07:26 +00:00
|
|
|
| UnsnoozeUpdateActionType
|
|
|
|
>;
|
2020-02-12 21:30:58 +00:00
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
|
2022-03-04 19:59:47 +00:00
|
|
|
function dismissDialog(): DismissDialogActionType {
|
2020-02-12 21:30:58 +00:00
|
|
|
return {
|
|
|
|
type: DISMISS_DIALOG,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:56:29 +00:00
|
|
|
function showUpdateDialog(
|
|
|
|
dialogType: DialogType,
|
|
|
|
updateDialogOptions: UpdateDialogOptionsType = {}
|
2022-03-04 19:59:47 +00:00
|
|
|
): ShowUpdateDialogActionType {
|
2020-02-12 21:30:58 +00:00
|
|
|
return {
|
|
|
|
type: SHOW_UPDATE_DIALOG,
|
2021-08-19 22:56:29 +00:00
|
|
|
payload: {
|
|
|
|
dialogType,
|
|
|
|
otherState: updateDialogOptions,
|
|
|
|
},
|
2020-02-12 21:30:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:56:29 +00:00
|
|
|
function snoozeUpdate(): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
SnoozeUpdateActionType | UnsnoozeUpdateActionType
|
|
|
|
> {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const { dialogType } = getState().updates;
|
2021-10-06 21:59:34 +00:00
|
|
|
setTimeout(() => {
|
2021-08-19 22:56:29 +00:00
|
|
|
dispatch({
|
|
|
|
type: UNSNOOZE_UPDATE,
|
|
|
|
payload: dialogType,
|
|
|
|
});
|
2021-10-06 21:59:34 +00:00
|
|
|
}, DAY);
|
2021-08-19 22:56:29 +00:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: SNOOZE_UPDATE,
|
|
|
|
});
|
2020-02-21 00:09:50 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-04 19:59:47 +00:00
|
|
|
function startUpdate(): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
StartUpdateActionType | ShowUpdateDialogActionType
|
|
|
|
> {
|
|
|
|
return async dispatch => {
|
|
|
|
dispatch({
|
|
|
|
type: START_UPDATE,
|
|
|
|
});
|
2020-02-12 21:30:58 +00:00
|
|
|
|
2022-03-04 19:59:47 +00:00
|
|
|
try {
|
|
|
|
await updateIpc.startUpdate();
|
|
|
|
} catch (_) {
|
|
|
|
dispatch({
|
|
|
|
type: SHOW_UPDATE_DIALOG,
|
|
|
|
payload: {
|
|
|
|
dialogType: DialogType.Cannot_Update,
|
|
|
|
otherState: {},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2020-02-12 21:30:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
dismissDialog,
|
|
|
|
showUpdateDialog,
|
2020-02-21 00:09:50 +00:00
|
|
|
snoozeUpdate,
|
2020-02-12 21:30:58 +00:00
|
|
|
startUpdate,
|
|
|
|
};
|
|
|
|
|
2023-03-20 20:42:00 +00:00
|
|
|
export const useUpdatesActions = (): BoundActionCreatorsMapObject<
|
|
|
|
typeof actions
|
|
|
|
> => useBoundActions(actions);
|
|
|
|
|
2020-02-12 21:30:58 +00:00
|
|
|
// Reducer
|
|
|
|
|
2022-02-23 18:48:40 +00:00
|
|
|
export function getEmptyState(): UpdatesStateType {
|
2020-02-12 21:30:58 +00:00
|
|
|
return {
|
2021-08-19 22:56:29 +00:00
|
|
|
dialogType: DialogType.None,
|
2020-02-21 00:09:50 +00:00
|
|
|
didSnooze: false,
|
|
|
|
showEventsCount: 0,
|
2020-02-12 21:30:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function reducer(
|
2020-12-14 19:47:21 +00:00
|
|
|
state: Readonly<UpdatesStateType> = getEmptyState(),
|
|
|
|
action: Readonly<UpdatesActionType>
|
2020-02-12 21:30:58 +00:00
|
|
|
): UpdatesStateType {
|
|
|
|
if (action.type === SHOW_UPDATE_DIALOG) {
|
2021-08-19 22:56:29 +00:00
|
|
|
const { dialogType, otherState } = action.payload;
|
|
|
|
|
2020-02-12 21:30:58 +00:00
|
|
|
return {
|
2021-08-19 22:56:29 +00:00
|
|
|
...state,
|
|
|
|
...otherState,
|
|
|
|
dialogType,
|
2020-02-21 00:09:50 +00:00
|
|
|
showEventsCount: state.showEventsCount + 1,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === SNOOZE_UPDATE) {
|
|
|
|
return {
|
2021-08-19 22:56:29 +00:00
|
|
|
...state,
|
|
|
|
dialogType: DialogType.None,
|
2020-02-21 00:09:50 +00:00
|
|
|
didSnooze: true,
|
2020-02-12 21:30:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-30 16:13:51 +00:00
|
|
|
if (action.type === START_UPDATE) {
|
|
|
|
return {
|
2021-08-19 22:56:29 +00:00
|
|
|
...state,
|
|
|
|
dialogType: DialogType.None,
|
2022-12-13 22:55:52 +00:00
|
|
|
didSnooze: false,
|
2020-03-30 16:13:51 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:30:58 +00:00
|
|
|
if (
|
|
|
|
action.type === DISMISS_DIALOG &&
|
2021-08-19 22:56:29 +00:00
|
|
|
state.dialogType === DialogType.MacOS_Read_Only
|
2020-02-12 21:30:58 +00:00
|
|
|
) {
|
|
|
|
return {
|
2021-08-19 22:56:29 +00:00
|
|
|
...state,
|
|
|
|
dialogType: DialogType.None,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === UNSNOOZE_UPDATE) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
dialogType: action.payload,
|
|
|
|
didSnooze: false,
|
2020-02-12 21:30:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|