Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 6472ca4

Browse files
authored
Merge pull request #4669 from matrix-org/t3chguy/toasts6_1
Allow deferring of Update Toast until the next morning
2 parents 097f27c + 9431393 commit 6472ca4

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

src/@types/common.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Copyright 2020 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Based on https://stackoverflow.com/a/53229857/3532235
18+
export type Without<T, U> = {[P in Exclude<keyof T, keyof U>] ? : never}
19+
export type XOR<T, U> = (T | U) extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;

src/BasePlatform.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import BaseEventIndexManager from './indexing/BaseEventIndexManager';
2323
import {ActionPayload} from "./dispatcher/payloads";
2424
import {CheckUpdatesPayload} from "./dispatcher/payloads/CheckUpdatesPayload";
2525
import {Action} from "./dispatcher/actions";
26+
import {hideToast as hideUpdateToast} from "./toasts/UpdateToast";
2627

2728
export enum UpdateCheckStatus {
2829
Checking = "CHECKING",
@@ -32,6 +33,8 @@ export enum UpdateCheckStatus {
3233
Ready = "READY",
3334
}
3435

36+
const UPDATE_DEFER_KEY = "mx_defer_update";
37+
3538
/**
3639
* Base class for classes that provide platform-specific functionality
3740
* eg. Setting an application badge or displaying notifications
@@ -74,12 +77,45 @@ export default abstract class BasePlatform {
7477
}
7578

7679
startUpdateCheck() {
80+
hideUpdateToast();
81+
localStorage.removeItem(UPDATE_DEFER_KEY);
7782
dis.dispatch<CheckUpdatesPayload>({
7883
action: Action.CheckUpdates,
7984
status: UpdateCheckStatus.Checking,
8085
});
8186
}
8287

88+
/**
89+
* Update the currently running app to the latest available version
90+
* and replace this instance of the app with the new version.
91+
*/
92+
installUpdate() {
93+
}
94+
95+
/**
96+
* Check if the version update has been deferred and that deferment is still in effect
97+
* @param newVersion the version string to check
98+
*/
99+
protected shouldShowUpdate(newVersion: string): boolean {
100+
try {
101+
const [version, deferUntil] = JSON.parse(localStorage.getItem(UPDATE_DEFER_KEY));
102+
return newVersion !== version || Date.now() > deferUntil;
103+
} catch (e) {
104+
return true;
105+
}
106+
}
107+
108+
/**
109+
* Ignore the pending update and don't prompt about this version
110+
* until the next morning (8am).
111+
*/
112+
deferUpdate(newVersion: string) {
113+
const date = new Date(Date.now() + 24 * 60 * 60 * 1000);
114+
date.setHours(8, 0, 0, 0); // set to next 8am
115+
localStorage.setItem(UPDATE_DEFER_KEY, JSON.stringify([newVersion, date.getTime()]));
116+
hideUpdateToast();
117+
}
118+
83119
/**
84120
* Returns true if the platform supports displaying
85121
* notifications, otherwise false.

src/components/views/settings/UpdateCheckButton.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import React, {useState} from "react";
1818

1919
import {UpdateCheckStatus} from "../../../BasePlatform";
2020
import PlatformPeg from "../../../PlatformPeg";
21-
import {hideToast as hideUpdateToast} from "../../../toasts/UpdateToast";
2221
import {useDispatcher} from "../../../hooks/useDispatcher";
2322
import dis from "../../../dispatcher/dispatcher";
2423
import {Action} from "../../../dispatcher/actions";
@@ -60,7 +59,6 @@ const UpdateCheckButton = () => {
6059
const onCheckForUpdateClick = () => {
6160
setState(null);
6261
PlatformPeg.get().startUpdateCheck();
63-
hideUpdateToast();
6462
};
6563

6664
useDispatcher(dis, ({action, ...params}) => {

src/components/views/toasts/GenericToast.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,21 @@ limitations under the License.
1717
import React, {ReactChild} from "react";
1818

1919
import FormButton from "../elements/FormButton";
20+
import {XOR} from "../../../@types/common";
2021

2122
interface IProps {
2223
description: ReactChild;
2324
acceptLabel: string;
24-
rejectLabel?: string;
2525

2626
onAccept();
27-
onReject?();
2827
}
2928

30-
const GenericToast: React.FC<IProps> = ({description, acceptLabel, rejectLabel, onAccept, onReject}) => {
29+
interface IPropsExtended extends IProps {
30+
rejectLabel: string;
31+
onReject();
32+
}
33+
34+
const GenericToast: React.FC<XOR<IPropsExtended, IProps>> = ({description, acceptLabel, rejectLabel, onAccept, onReject}) => {
3135
return <div>
3236
<div className="mx_Toast_description">
3337
{ description }

src/toasts/UpdateToast.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ function installUpdate() {
4040
}
4141

4242
export const showToast = (version: string, newVersion: string, releaseNotes?: string) => {
43+
function onReject() {
44+
PlatformPeg.get().deferUpdate(newVersion);
45+
}
46+
4347
let onAccept;
4448
let acceptLabel = _t("What's new?");
4549
if (releaseNotes) {
@@ -79,6 +83,8 @@ export const showToast = (version: string, newVersion: string, releaseNotes?: st
7983
description: _t("A new version of Riot is available!"),
8084
acceptLabel,
8185
onAccept,
86+
rejectLabel: _t("Later"),
87+
onReject,
8288
},
8389
component: GenericToast,
8490
priority: 20,

0 commit comments

Comments
 (0)