Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions packages/app/src/components/updater-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@ 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", () => {
expect(updaterAction({ status: "idle" })).toEqual({
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",
})
Expand All @@ -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",
})
})
})
41 changes: 24 additions & 17 deletions packages/app/src/components/updater-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<UpdaterState["status"], UpdaterAction> = {
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() {
Expand Down Expand Up @@ -48,4 +55,4 @@ export function useUpdaterAction() {
}
},
}
}
}
Loading