From 3e8e270f4aad1662d82698fe65e03f091c58df35 Mon Sep 17 00:00:00 2001 From: Marnilla-Metwaly <180761643+Marnilla-Metwaly@users.noreply.github.com> Date: Sat, 5 Sep 2026 16:06:44 +0000 Subject: [PATCH] refactor(app): simplify updater action selection --- .../app/src/components/updater-action.test.ts | 26 ++++++++++-- packages/app/src/components/updater-action.ts | 41 +++++++++++-------- 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/packages/app/src/components/updater-action.test.ts b/packages/app/src/components/updater-action.test.ts index 46e7c732..7b789908 100644 --- a/packages/app/src/components/updater-action.test.ts +++ b/packages/app/src/components/updater-action.test.ts @@ -2,8 +2,13 @@ import { describe, expect, test } from "bun:test" import { updaterAction } from "./updater-action" describe("updaterAction", () => { - test("disables update actions when the platform has no updater", () => { - expect(updaterAction(undefined)).toEqual({ label: "settings.updates.action.checkNow" }) + test("disables update actions when the updater is unavailable or disabled", () => { + expect(updaterAction(undefined)).toEqual({ + label: "settings.updates.action.checkNow", + }) + expect(updaterAction({ status: "disabled" })).toEqual({ + label: "settings.updates.action.checkNow", + }) }) test("projects updater transitions into one settings action", () => { @@ -11,7 +16,9 @@ describe("updaterAction", () => { label: "settings.updates.action.checkNow", run: "check", }) - expect(updaterAction({ status: "checking" })).toEqual({ label: "settings.updates.action.checking" }) + expect(updaterAction({ status: "checking" })).toEqual({ + label: "settings.updates.action.checking", + }) expect(updaterAction({ status: "downloading", version: "2.0.0" })).toEqual({ label: "settings.updates.action.downloading", }) @@ -23,4 +30,15 @@ describe("updaterAction", () => { label: "settings.updates.action.installing", }) }) -}) + + test("allows another check after completion or an error", () => { + expect(updaterAction({ status: "up-to-date" })).toEqual({ + label: "settings.updates.action.checkNow", + run: "check", + }) + expect(updaterAction({ status: "error", message: "Update failed" })).toEqual({ + label: "settings.updates.action.checkNow", + run: "check", + }) + }) +}) \ No newline at end of file diff --git a/packages/app/src/components/updater-action.ts b/packages/app/src/components/updater-action.ts index 9c13c542..4f5ff704 100644 --- a/packages/app/src/components/updater-action.ts +++ b/packages/app/src/components/updater-action.ts @@ -4,22 +4,29 @@ import { usePlatform } from "@/context/platform" import { useLanguage } from "@/context/language" import { showToast } from "@/utils/toast" -export function updaterAction(state: UpdaterState | undefined) { - if (!state) return { label: "settings.updates.action.checkNow" as const } - switch (state.status) { - case "checking": - return { label: "settings.updates.action.checking" as const } - case "downloading": - return { label: "settings.updates.action.downloading" as const } - case "ready": - return { label: "toast.update.action.installRestart" as const, run: "install" as const } - case "installing": - return { label: "settings.updates.action.installing" as const } - case "disabled": - return { label: "settings.updates.action.checkNow" as const } - default: - return { label: "settings.updates.action.checkNow" as const, run: "check" as const } - } +type UpdaterAction = { + label: + | "settings.updates.action.checkNow" + | "settings.updates.action.checking" + | "settings.updates.action.downloading" + | "toast.update.action.installRestart" + | "settings.updates.action.installing" + run?: "check" | "install" +} + +const updaterActions: Record = { + disabled: { label: "settings.updates.action.checkNow" }, + idle: { label: "settings.updates.action.checkNow", run: "check" }, + checking: { label: "settings.updates.action.checking" }, + downloading: { label: "settings.updates.action.downloading" }, + ready: { label: "toast.update.action.installRestart", run: "install" }, + "up-to-date": { label: "settings.updates.action.checkNow", run: "check" }, + installing: { label: "settings.updates.action.installing" }, + error: { label: "settings.updates.action.checkNow", run: "check" }, +} + +export function updaterAction(state: UpdaterState | undefined): UpdaterAction { + return { ...updaterActions[state?.status ?? "disabled"] } } export function useUpdaterAction() { @@ -48,4 +55,4 @@ export function useUpdaterAction() { } }, } -} +} \ No newline at end of file