Files
ThreadNet-Web/src/components/views/settings/UpdateCheckButton.tsx
T
David LangleyandGitHub 69ee8fd96a Change License: AGPL + Element Commercial (#28856)
* Add commercial licence and update config files

* Update license in headers

* Revert "Update license in headers"

This reverts commit 7ed7949485e88889a9ffc8075a9df1f8e936777e.

* Update only spdx id

* Remove LicenseRef- from package.json

LicenseRef- no longer allowed in npm v3 package.json
This fixes the warning in the logs and failing build check.
2025-01-06 11:18:54 +00:00

89 lines
2.9 KiB
TypeScript

/*
Copyright 2024 New Vector Ltd.
Copyright 2020 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
import React, { ReactNode, useState } from "react";
import { UpdateCheckStatus } from "../../../BasePlatform";
import PlatformPeg from "../../../PlatformPeg";
import { useDispatcher } from "../../../hooks/useDispatcher";
import dis from "../../../dispatcher/dispatcher";
import { Action } from "../../../dispatcher/actions";
import { _t } from "../../../languageHandler";
import InlineSpinner from "../../../components/views/elements/InlineSpinner";
import AccessibleButton from "../../../components/views/elements/AccessibleButton";
import { CheckUpdatesPayload } from "../../../dispatcher/payloads/CheckUpdatesPayload";
function installUpdate(): void {
PlatformPeg.get()?.installUpdate();
}
function getStatusText(status: UpdateCheckStatus, errorDetail?: string): ReactNode {
switch (status) {
case UpdateCheckStatus.Error:
return _t("update|error_encountered", { errorDetail });
case UpdateCheckStatus.Checking:
return _t("update|checking");
case UpdateCheckStatus.NotAvailable:
return _t("update|no_update");
case UpdateCheckStatus.Downloading:
return _t("update|downloading");
case UpdateCheckStatus.Ready:
return _t(
"update|new_version_available",
{},
{
a: (sub) => (
<AccessibleButton kind="link_inline" onClick={installUpdate}>
{sub}
</AccessibleButton>
),
},
);
}
}
const doneStatuses = [UpdateCheckStatus.Ready, UpdateCheckStatus.Error, UpdateCheckStatus.NotAvailable];
const UpdateCheckButton: React.FC = () => {
const [state, setState] = useState<CheckUpdatesPayload | null>(null);
const onCheckForUpdateClick = (): void => {
setState(null);
PlatformPeg.get()?.startUpdateCheck();
};
useDispatcher(dis, ({ action, ...params }) => {
if (action === Action.CheckUpdates) {
setState(params as CheckUpdatesPayload);
}
});
const busy = !!state && !doneStatuses.includes(state.status);
let suffix: JSX.Element | undefined;
if (state) {
suffix = (
<span className="mx_UpdateCheckButton_summary">
{getStatusText(state.status, state.detail)}
{busy && <InlineSpinner />}
</span>
);
}
return (
<React.Fragment>
<AccessibleButton onClick={onCheckForUpdateClick} kind="primary_outline" disabled={busy}>
{_t("update|check_action")}
</AccessibleButton>
{suffix}
</React.Fragment>
);
};
export default UpdateCheckButton;