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

Set proper timeframes

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2022-05-30 17:05:27 +03:00
parent dc7901ef8b
commit 194370e020
3 changed files with 26 additions and 39 deletions

View File

@ -4,6 +4,7 @@
*/ */
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 sessionStorageInjectable from "../utils/session-storage.injectable";
import { AppUpdateWarning } from "./app-update-warning"; import { AppUpdateWarning } from "./app-update-warning";
const appUpdateWarningInjectable = getInjectable({ const appUpdateWarningInjectable = getInjectable({
@ -12,6 +13,7 @@ const appUpdateWarningInjectable = getInjectable({
instantiate: (di) => { instantiate: (di) => {
return AppUpdateWarning.createInstance({ return AppUpdateWarning.createInstance({
ipcRenderer: di.inject(ipcRendererInjectable), ipcRenderer: di.inject(ipcRendererInjectable),
sessionStorage: di.inject(sessionStorageInjectable),
}); });
}, },
}); });

View File

@ -9,14 +9,17 @@ import type { IpcRenderer } from "electron";
interface Dependencies { interface Dependencies {
readonly ipcRenderer: IpcRenderer; readonly ipcRenderer: IpcRenderer;
readonly sessionStorage: Storage;
} }
const onceADay = 1000 * 60 * 60 * 24;
export class AppUpdateWarning extends Singleton { export class AppUpdateWarning extends Singleton {
@observable warningLevel: "high" | "medium" | "light" | "" = ""; @observable warningLevel: "high" | "medium" | "light" | "" = "";
@observable private updateAvailableDate: Date | null = this.getDateFromSessionStorage(); @observable private updateAvailableDate: Date | null = this.getDateFromSessionStorage();
private interval: NodeJS.Timeout | null = null; private interval: NodeJS.Timeout | null = null;
constructor(dependencies: Dependencies) { constructor(private dependencies: Dependencies) {
super(); super();
makeObservable(this); makeObservable(this);
@ -29,12 +32,12 @@ export class AppUpdateWarning extends Singleton {
saveDateToSessionStorage() { saveDateToSessionStorage() {
if (this.updateAvailableDate) { if (this.updateAvailableDate) {
window.sessionStorage.setItem("when-update-available", this.updateAvailableDate.toISOString()); this.dependencies.sessionStorage.setItem("when-update-available", this.updateAvailableDate.toISOString());
} }
} }
getDateFromSessionStorage() { getDateFromSessionStorage() {
const value = window.sessionStorage.getItem("when-update-available"); const value = this.dependencies.sessionStorage.getItem("when-update-available");
if (!value) { if (!value) {
return null; return null;
@ -56,7 +59,7 @@ export class AppUpdateWarning extends Singleton {
if (!this.interval) { if (!this.interval) {
this.interval = setInterval(() => { this.interval = setInterval(() => {
this.setWarningLevel(); this.setWarningLevel();
}, 1000 * 60); // Once a day }, onceADay);
} }
} }
@ -73,61 +76,31 @@ export class AppUpdateWarning extends Singleton {
const today = new Date(); const today = new Date();
const elapsedTime = today.getTime() - this.updateAvailableDate.getTime(); const elapsedTime = today.getTime() - this.updateAvailableDate.getTime();
const elapsedDays = elapsedTime / (1000 * 60 * 60 * 24); const elapsedDays = elapsedTime / (onceADay);
return elapsedDays; return elapsedDays;
} }
get minutesAfterUpdateAvailable() {
if (!this.updateAvailableDate) {
return 0;
}
const today = new Date();
const elapsedTime = today.getTime() - this.updateAvailableDate.getTime();
const elapsedMinutes = Math.floor(elapsedTime / (1000 * 60));
return elapsedMinutes;
}
// 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) { private setHighWarningLevel(elapsedDays: number) {
if (elapsedDays >= 6) { if (elapsedDays >= 25) {
this.warningLevel = "high"; this.warningLevel = "high";
} }
} }
private setMediumWarningLevel(elapsedDays: number) { private setMediumWarningLevel(elapsedDays: number) {
if (elapsedDays >= 2 && elapsedDays < 4) { if (elapsedDays >= 20 && elapsedDays < 25) {
this.warningLevel = "medium"; this.warningLevel = "medium";
} }
} }
private setLightWarningLevel(elapsedDays: number) { private setLightWarningLevel(elapsedDays: number) {
if (elapsedDays < 2) { if (elapsedDays < 20) {
this.warningLevel = "light"; this.warningLevel = "light";
} }
} }
private setWarningLevel() { private setWarningLevel() {
const days = this.minutesAfterUpdateAvailable; const days = this.daysAfterUpdateAvailable;
this.setHighWarningLevel(days); this.setHighWarningLevel(days);
this.setMediumWarningLevel(days); this.setMediumWarningLevel(days);

View File

@ -0,0 +1,12 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
const sessionStorageInjectable = getInjectable({
id: "session-storage",
instantiate: () => window.sessionStorage,
});
export default sessionStorageInjectable;