/** * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ import { withInjectables } from "@ogre-tools/injectable-react"; import { observer } from "mobx-react"; import assert from "assert"; import React, { useState } from "react"; import { Spinner } from "../spinner"; import type { ProgressOfUpdateDownload } from "../../../common/application-update/progress-of-update-download/progress-of-update-download.injectable"; import progressOfUpdateDownloadInjectable from "../../../common/application-update/progress-of-update-download/progress-of-update-download.injectable"; import type { DiscoveredUpdateVersion } from "../../../common/application-update/discovered-update-version/discovered-update-version.injectable"; import discoveredUpdateVersionInjectable from "../../../common/application-update/discovered-update-version/discovered-update-version.injectable"; import type { UpdateIsBeingDownloaded } from "../../../common/application-update/update-is-being-downloaded/update-is-being-downloaded.injectable"; import updateIsBeingDownloadedInjectable from "../../../common/application-update/update-is-being-downloaded/update-is-being-downloaded.injectable"; import type { UpdatesAreBeingDiscovered } from "../../../common/application-update/updates-are-being-discovered/updates-are-being-discovered.injectable"; import updatesAreBeingDiscoveredInjectable from "../../../common/application-update/updates-are-being-discovered/updates-are-being-discovered.injectable"; import { reactiveNow } from "../../../common/utils/reactive-now/reactive-now"; interface Dependencies { progressOfUpdateDownload: ProgressOfUpdateDownload; discoveredVersionState: DiscoveredUpdateVersion; downloadingUpdateState: UpdateIsBeingDownloaded; checkingForUpdatesState: UpdatesAreBeingDiscovered; } interface EndNoteProps { version?: string; note: (version: string) => JSX.Element; } const EndNote = observer(({ version, note }: EndNoteProps) => { const [start] = useState(Date.now()); if (start + 5000 <= reactiveNow()) { return idle(); } return note(version ?? ""); }); const checking = () => ( <>
Checking for updates...
); const available = (version: string) =>
{`${version ?? "Update"} is available`}
; const notAvailable = () =>
No new updates available
; const downloading = (version: string) => { return ( <>
{`Downloading version ${version}...`}
); }; const downloadFailed = (errMsg: string) =>
{errMsg}
; const idle = () =>
; export const NonInjectedAutoUpdateComponent = observer(({ progressOfUpdateDownload, discoveredVersionState, downloadingUpdateState, checkingForUpdatesState, }: Dependencies) => { const discoveredVersion = discoveredVersionState.value.get(); const { failed } = progressOfUpdateDownload.value.get(); if (downloadingUpdateState.value.get()) { assert(discoveredVersion); return downloading(discoveredVersion.version); } if (checkingForUpdatesState.value.get()) { return checking(); } if ( discoveredVersion) { return ; } if ( failed ) { return ; } return ; }); export const AutoUpdateComponent = withInjectables(NonInjectedAutoUpdateComponent, { getProps: (di, props) => ({ progressOfUpdateDownload: di.inject(progressOfUpdateDownloadInjectable), discoveredVersionState: di.inject(discoveredUpdateVersionInjectable), downloadingUpdateState: di.inject(updateIsBeingDownloadedInjectable), checkingForUpdatesState: di.inject(updatesAreBeingDiscoveredInjectable), ...props, }), });