Make update warning level completely reactive and responsibility of a renderer

Co-authored-by: Alex Andreev <alex.andreev.email@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne SavolainenandAlex Andreev committed 2022-06-28 12:11:13 +03:00
1 parent 5a8ffdf191
commit 2c39b9019f
9 files changed
+93 -46

No files matched your search

@@ -3,7 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import restartAndInstallUpdateChannel from "../../../common/application-update/update-warning-level/restart-and-install-update-channel.injectable";
import restartAndInstallUpdateChannel from "../../../common/application-update/restart-and-install-update-channel/restart-and-install-update-channel.injectable";
import messageToChannelInjectable from "../../utils/channel/message-to-channel.injectable";
const restartAndInstallUpdateInjectable = getInjectable({
@@ -13,13 +13,11 @@ import type { IconProps } from "../icon";
import { Icon } from "../icon";
import { withInjectables } from "@ogre-tools/injectable-react";
import { observer } from "mobx-react";
import updateWarningLevelInjectable from "../../../common/application-update/update-warning-level/update-warning-level.injectable";
import type { IComputedValue } from "mobx";
import { computed } from "mobx";
import restartAndInstallUpdateInjectable from "./restart-and-install-update.injectable";
import updateWarningLevelInjectable from "./update-warning-level.injectable";
interface UpdateButtonProps extends HTMLAttributes<HTMLButtonElement> {
}
interface UpdateButtonProps extends HTMLAttributes<HTMLButtonElement> {}
interface Dependencies {
warningLevel: IComputedValue<"light" | "medium" | "high" | "">;
@@ -72,14 +70,13 @@ export const NonInjectedUpdateButton = observer(({ warningLevel, update, id }: U
);
});
export const UpdateButton = withInjectables<Dependencies, UpdateButtonProps>(NonInjectedUpdateButton, {
getProps: (di, props) => {
const warnignLevel = di.inject(updateWarningLevelInjectable);
return {
export const UpdateButton = withInjectables<Dependencies, UpdateButtonProps>(
NonInjectedUpdateButton,
{
getProps: (di, props) => ({
...props,
warningLevel: computed(() => warnignLevel.value.get()),
warningLevel: di.inject(updateWarningLevelInjectable),
update: di.inject(restartAndInstallUpdateInjectable),
};
}),
},
});
);
@@ -0,0 +1,45 @@
/**
* 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";
import { computed } from "mobx";
import { now as reactiveDateNow } from "mobx-utils";
import updateDownloadedDateTimeInjectable from "../../../common/application-update/update-downloaded-date-time/update-downloaded-date-time.injectable";
const updateWarningLevelInjectable = getInjectable({
id: "update-warning-level",
instantiate: (di) => {
const updateDownloadedDateTime = di.inject(updateDownloadedDateTimeInjectable);
return computed(() => {
const downloadedAt = updateDownloadedDateTime.value.get();
if (!downloadedAt) {
return "";
}
const ONE_DAY = 1000 * 60 * 60 * 24;
const downloadedAtTimestamp = new Date(downloadedAt).getTime();
const currentDateTimeTimestamp = reactiveDateNow(ONE_DAY);
const elapsedTime = currentDateTimeTimestamp - downloadedAtTimestamp;
const elapsedDays = elapsedTime / ONE_DAY;
if (elapsedDays < 20) {
return "light";
}
if (elapsedDays >= 20 && elapsedDays < 25) {
return "medium";
}
return "high";
});
},
});
export default updateWarningLevelInjectable;