1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Determine warning from days passed after update available

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2022-05-27 14:02:34 +03:00
parent 613e98cd2f
commit b60893c35a
2 changed files with 90 additions and 33 deletions

View File

@ -4,17 +4,13 @@
*/ */
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import ipcRendererInjectable from "../app-paths/get-value-from-registered-channel/ipc-renderer/ipc-renderer.injectable"; import ipcRendererInjectable from "../app-paths/get-value-from-registered-channel/ipc-renderer/ipc-renderer.injectable";
import appPublishDateInjectable from "./app-publish-date.injectable";
import { AppUpdateWarning } from "./app-update-warning"; import { AppUpdateWarning } from "./app-update-warning";
const appUpdateWarningInjectable = getInjectable({ const appUpdateWarningInjectable = getInjectable({
id: "app-update-warning", id: "app-update-warning",
instantiate: (di) => { instantiate: (di) => {
AppUpdateWarning.resetInstance();
return AppUpdateWarning.createInstance({ return AppUpdateWarning.createInstance({
releaseDate: di.inject(appPublishDateInjectable),
ipcRenderer: di.inject(ipcRendererInjectable), ipcRenderer: di.inject(ipcRendererInjectable),
}); });
}, },

View File

@ -2,61 +2,122 @@
* Copyright (c) OpenLens Authors. All rights reserved. * Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import { computed, makeObservable, observable } from "mobx"; import { makeObservable, observable } from "mobx";
import type { UpdateAvailableFromMain } from "../../common/ipc";
import { UpdateAvailableChannel } from "../../common/ipc"; import { UpdateAvailableChannel } from "../../common/ipc";
import { Singleton } from "../utils"; import { Singleton } from "../utils";
import moment from "moment";
import type { IpcRenderer } from "electron"; import type { IpcRenderer } from "electron";
interface Dependencies { interface Dependencies {
readonly releaseDate: string;
readonly ipcRenderer: IpcRenderer; readonly ipcRenderer: IpcRenderer;
} }
export class AppUpdateWarning extends Singleton { export class AppUpdateWarning extends Singleton {
@observable updateReleaseDate = ""; @observable warningLevel: "high" | "medium" | "light" | "" = "";
@observable private updateAvailableDate: Date | null = null;
private interval: NodeJS.Timeout | null = null;
constructor(private dependencies: Dependencies) { constructor(dependencies: Dependencies) {
super(); super();
makeObservable(this); makeObservable(this);
dependencies.ipcRenderer.on(UpdateAvailableChannel, (event, ...[, updateInfo]: UpdateAvailableFromMain) => { dependencies.ipcRenderer.on(UpdateAvailableChannel, () => {
this.downloadedUpdateDate = updateInfo?.releaseDate; this.setUpdateAvailableDate();
this.setWarningLevel();
this.startRefreshLevelInterval();
}); });
} }
set downloadedUpdateDate(date: string) { setUpdateAvailableDate() {
this.updateReleaseDate = date; if (!this.updateAvailableDate) {
this.updateAvailableDate = new Date();
}
} }
@computed private startRefreshLevelInterval() {
get warningLevel(): "high" | "medium" | "light" | "" { if (!this.interval) {
const { updateReleaseDate, dependencies } = this; this.interval = setInterval(() => {
const { releaseDate } = dependencies; this.setWarningLevel();
}, 1000 * 60); // Once a day
}
}
if (!updateReleaseDate || !releaseDate) { private stopRefreshLevelInterval() {
return ""; if (this.interval) {
clearInterval(this.interval);
}
}
private get daysAfterUpdateAvailable() {
if (!this.updateAvailableDate) {
return 0;
} }
const update = moment(updateReleaseDate); const today = new Date();
const release = moment(releaseDate); const elapsedTime = today.getTime() - this.updateAvailableDate.getTime();
const elapsedDays = elapsedTime / (1000 * 60 * 60 * 24);
const duration = moment.duration(update.diff(release)); return elapsedDays;
const days = duration.asDays(); }
if (days >= 27) { get minutesAfterUpdateAvailable() {
return "high"; if (!this.updateAvailableDate) {
return 0;
} }
if (days >= 25 && days < 27) { const today = new Date();
return "medium"; const elapsedTime = today.getTime() - this.updateAvailableDate.getTime();
} const elapsedMinutes = Math.floor(elapsedTime / (1000 * 60));
if (days >= 20 && days < 25) { return elapsedMinutes;
return "light"; }
}
return ""; // private setHighWarningLevel(elapsedDays: number) {
// if (elapsedDays >= 25) {
// this.warningLevel = "high";
// }
// }
// private setMediumWarningLevel(elapsedDays: number) {
// if (elapsedDays >= 20 && elapsedDays < 25) {
// this.warningLevel = "medium";
// }
// }
// private setLightWarningLevel(elapsedDays: number) {
// if (elapsedDays < 20) {
// this.warningLevel = "light";
// }
// }
private setHighWarningLevel(elapsedDays: number) {
if (elapsedDays >= 6) {
this.warningLevel = "high";
}
}
private setMediumWarningLevel(elapsedDays: number) {
if (elapsedDays >= 2 && elapsedDays < 4) {
this.warningLevel = "medium";
}
}
private setLightWarningLevel(elapsedDays: number) {
if (elapsedDays < 2) {
this.warningLevel = "light";
}
}
private setWarningLevel() {
const days = this.minutesAfterUpdateAvailable;
this.setHighWarningLevel(days);
this.setMediumWarningLevel(days);
this.setLightWarningLevel(days);
}
reset() {
this.warningLevel = "";
this.updateAvailableDate = null;
this.stopRefreshLevelInterval();
} }
} }