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';
|
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
|
|
|
|
|
|
|
|
export type UpdatesStateType = {
|
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;
|
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
|
|
|
|
2021-08-19 22:56:29 +00:00
|
|
|
export type UpdateDialogOptionsType = {
|
|
|
|
downloadSize?: number;
|
|
|
|
downloadedSize?: number;
|
|
|
|
version?: string;
|
2020-02-12 21:30:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type DismissDialogAction = {
|
2021-08-19 22:56:29 +00:00
|
|
|
type: typeof DISMISS_DIALOG;
|
2020-02-12 21:30:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type ShowUpdateDialogAction = {
|
2021-08-19 22:56:29 +00:00
|
|
|
type: typeof SHOW_UPDATE_DIALOG;
|
|
|
|
payload: {
|
|
|
|
dialogType: DialogType;
|
|
|
|
otherState: UpdateDialogOptionsType;
|
|
|
|
};
|
2020-02-12 21:30:58 +00:00
|
|
|
};
|
|
|
|
|
2020-02-21 00:09:50 +00:00
|
|
|
type SnoozeUpdateActionType = {
|
2021-08-19 22:56:29 +00:00
|
|
|
type: typeof SNOOZE_UPDATE;
|
2020-02-21 00:09:50 +00:00
|
|
|
};
|
|
|
|
|
2020-02-12 21:30:58 +00:00
|
|
|
type StartUpdateAction = {
|
2021-08-19 22:56:29 +00:00
|
|
|
type: typeof START_UPDATE;
|
|
|
|
};
|
|
|
|
|
|
|
|
type UnsnoozeUpdateActionType = {
|
|
|
|
type: typeof UNSNOOZE_UPDATE;
|
|
|
|
payload: DialogType;
|
2020-02-12 21:30:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type UpdatesActionType =
|
|
|
|
| DismissDialogAction
|
|
|
|
| ShowUpdateDialogAction
|
2020-02-21 00:09:50 +00:00
|
|
|
| SnoozeUpdateActionType
|
2021-08-19 22:56:29 +00:00
|
|
|
| StartUpdateAction
|
|
|
|
| UnsnoozeUpdateActionType;
|
2020-02-12 21:30:58 +00:00
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
|
|
|
|
function dismissDialog(): DismissDialogAction {
|
|
|
|
return {
|
|
|
|
type: DISMISS_DIALOG,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:56:29 +00:00
|
|
|
function showUpdateDialog(
|
|
|
|
dialogType: DialogType,
|
|
|
|
updateDialogOptions: UpdateDialogOptionsType = {}
|
|
|
|
): ShowUpdateDialogAction {
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:30:58 +00:00
|
|
|
function startUpdate(): StartUpdateAction {
|
|
|
|
updateIpc.startUpdate();
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: START_UPDATE,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
dismissDialog,
|
|
|
|
showUpdateDialog,
|
2020-02-21 00:09:50 +00:00
|
|
|
snoozeUpdate,
|
2020-02-12 21:30:58 +00:00
|
|
|
startUpdate,
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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,
|
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;
|
|
|
|
}
|