From a8afe88c83ec4e4be0237750ea7e333de77354dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Tue, 24 Nov 2026 13:07:57 +0500 Subject: [PATCH] Show the server version and update button in Settings after an update check The Server card in Settings > Updates copied the shared update offer into local state once and an effect wiped that copy whenever a finished update attempt or disk-state refresh had bumped the shared revision, so after a check the card kept showing only "..." with no update button while the main screen (which reads the shared state) still offered the update. The card now derives the offer from the shared state; a manual check on the card is kept only until the next finished attempt. Conflict choices default to "keep" without a copy as well. Co-Authored-By: Claude Sonnet 5.5 --- ...61124-settings-server-card-shows-update.md | 7 ++ src/lib/serverUpdate.ts | 5 + src/screens/ServerUpdateCard.tsx | 21 ++-- tools/server-update-card.test.mjs | 96 +++++++++++++++++++ 4 files changed, 121 insertions(+), 8 deletions(-) create mode 100644 changelog.d/20261124-settings-server-card-shows-update.md create mode 100644 tools/server-update-card.test.mjs diff --git a/changelog.d/20261124-settings-server-card-shows-update.md b/changelog.d/20261124-settings-server-card-shows-update.md new file mode 100644 index 0000000..6ac3f06 --- /dev/null +++ b/changelog.d/20261124-settings-server-card-shows-update.md @@ -0,0 +1,7 @@ +--- +area: manager +type: fixed +audience: admins +title: Settings now shows the server version and update button after you have checked for updates +--- +The Server card in Settings > Updates could stay empty, with only "…" and no update button, while the main screen already offered an update. It now shows the same version and update as the main screen. diff --git a/src/lib/serverUpdate.ts b/src/lib/serverUpdate.ts index 3bacd07..114b279 100644 --- a/src/lib/serverUpdate.ts +++ b/src/lib/serverUpdate.ts @@ -63,6 +63,11 @@ export function startServerUpdatePolling(id: string): () => void { return () => clearInterval(t); } +/** How many update attempts and disk-state refreshes have finished for this server. */ +export function getServerUpdateRevision(id: string): number { + return get(id).stateRevision; +} + export function clearServerUpdateResult(id: string) { set(id, { result: null, uiError: null }); } diff --git a/src/screens/ServerUpdateCard.tsx b/src/screens/ServerUpdateCard.tsx index 651fa8c..fd99eff 100644 --- a/src/screens/ServerUpdateCard.tsx +++ b/src/screens/ServerUpdateCard.tsx @@ -1,7 +1,7 @@ import { useEffect, useState } from "react"; import { open } from "@tauri-apps/plugin-dialog"; import { ChevronDown, ChevronRight, Loader2, RefreshCw, Server } from "lucide-react"; -import { applyServerUpdate, clearServerUpdateResult, refreshServerUpdateState, useServerUpdate } from "@/lib/serverUpdate"; +import { applyServerUpdate, clearServerUpdateResult, getServerUpdateRevision, refreshServerUpdateState, useServerUpdate } from "@/lib/serverUpdate"; import { isUpdateCurrent } from "@/lib/serverUpdateStatus"; import { api, asUiError, type UiError, type UpdatePreview, type UpdateTxn } from "@/lib/api"; import { Button } from "@/components/ui/button"; @@ -17,7 +17,12 @@ export function ServerUpdateCard({ serverId, onHistory, onAttention }: { serverI const { t } = useI18n(); const upd = useServerUpdate(serverId); const human = useHuman(); - const [preview, setPreview] = useState(upd.preview); + // The offer shown here is the shared one the main screen uses (it is checked at start and every five minutes), unless + // the owner pressed the check button on this card. A manual check counts only until the next finished update attempt, + // which is why it carries the revision it was made at. Nothing is copied from the shared state when this card opens, + // so an offer that arrives later, or one found before the card was opened, still shows. + const [manual, setManual] = useState<{ revision: number; preview: UpdatePreview | null } | null>(null); + const preview = manual && manual.revision === upd.stateRevision ? manual.preview : upd.preview; const [pending, setPending] = useState(null); const [pendingChecked, setPendingChecked] = useState(false); const [pendingStateError, setPendingStateError] = useState(null); @@ -29,7 +34,8 @@ export function ServerUpdateCard({ serverId, onHistory, onAttention }: { serverI const error = pendingStateError ?? localError ?? upd.uiError; const [localDone, setDone] = useState(null); const done = upd.result && "committed" in upd.result ? t("upd.updatedTo", { v: upd.result.committed }) : localDone; - const [choices, setChoices] = useState>(() => Object.fromEntries((upd.preview?.conflicts ?? []).map(c => [c, "keep" as const]))); + const [picked, setChoices] = useState>({}); + const choices: Record = Object.fromEntries((preview?.conflicts ?? []).map(c => [c, picked[c] ?? "keep"])); const [advanced, setAdvanced] = useState(false); const [source, setSource] = useState(""); const [startAfter, setStartAfter] = useStartAfterUpdate("server", serverId); @@ -48,7 +54,6 @@ export function ServerUpdateCard({ serverId, onHistory, onAttention }: { serverI // Every finished attempt refreshes the disk state, including failures in the background. useEffect(() => { if (!upd.stateRevision) return; - setPreview(null); setPendingChecked(!upd.stateError); setPendingStateError(upd.stateError); if (!upd.stateError) setPending(upd.result && "pending" in upd.result ? upd.result.pending : null); @@ -72,13 +77,13 @@ export function ServerUpdateCard({ serverId, onHistory, onAttention }: { serverI setError(null); setDone(null); clearServerUpdateResult(serverId); - setPreview(null); + setManual(null); try { const txn = await refreshPending(); if (txn) return; const p = await api.checkUpdate(serverId, source.trim() || undefined); - setPreview(p); - setChoices(Object.fromEntries(p.conflicts.map((c) => [c, "keep" as const]))); + setManual({ revision: getServerUpdateRevision(serverId), preview: p }); + setChoices({}); } catch (e) { setError(asUiError(e)); } finally { @@ -116,7 +121,7 @@ export function ServerUpdateCard({ serverId, onHistory, onAttention }: { serverI const result = await api.retryUpdateValidation(serverId, txn.id); if (result.state === "committed") { setPending(null); - setPreview(null); + setManual(null); setDone(t("upd.updatedTo", { v: result.to_version })); clearServerUpdateResult(serverId); } else setPending(result); diff --git a/tools/server-update-card.test.mjs b/tools/server-update-card.test.mjs new file mode 100644 index 0000000..957fd10 --- /dev/null +++ b/tools/server-update-card.test.mjs @@ -0,0 +1,96 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { createRequire } from "node:module"; +import { fileURLToPath } from "node:url"; +import { rolldown } from "rolldown"; +import { createElement } from "react"; +import { renderToStaticMarkup } from "react-dom/server"; + +// The Server card in Settings -> Updates, rendered with a tiny hook runtime so that its effects really run (a plain +// server render skips them, which is where the offer used to be wiped when the tab was opened after an update attempt +// had already finished). The card must show what the shared update state, which the main screen also uses, holds. +const stubs = { + react: `export const useState=(...a)=>globalThis.__hooks.useState(...a); export const useEffect=(...a)=>globalThis.__hooks.useEffect(...a);`, + "@/lib/api": `export const api={pendingUpdate:async()=>null}; export const asUiError=e=>e;`, + "@tauri-apps/plugin-dialog": `export const open=async()=>null;`, + "@/i18n": `export const useI18n=()=>({t:(k,v)=>v?k+JSON.stringify(v):k}); export const useHuman=()=>h=>h; export const hasKey=()=>false;`, + "@/lib/serverUpdate": `export const applyServerUpdate=async()=>false; export const clearServerUpdateResult=()=>{}; export const refreshServerUpdateState=async()=>null; export const getServerUpdateRevision=()=>0; export const useServerUpdate=()=>globalThis.__cardUpdate;`, + "@/lib/updatePrefs": `export const useStartAfterUpdate=()=>[false,()=>{}];`, + "@/components/ui/button": `import {createElement} from 'react'; export const Button=({children,disabled,onClick,className,...props})=>createElement('button',{disabled,onClick,className,...props},children);`, + "@/components/ui/card": `import {createElement} from 'react'; export const Card=({children,...props})=>createElement('div',props,children);`, +}; +const card = fileURLToPath(new URL("../src/screens/ServerUpdateCard.tsx", import.meta.url)); +const bundle = await rolldown({ + input: card, + transform: { jsx: "react-jsx" }, + external: (id, importer) => id === "react/jsx-runtime" || id === "lucide-react" || (id === "react" && !importer?.endsWith("ServerUpdateCard.tsx")), + plugins: [{ name: "card-test", + resolveId(id, importer) { + if (id === "@/lib/serverUpdateStatus") return fileURLToPath(new URL("../src/lib/serverUpdateStatus.ts", import.meta.url)); + if (id.startsWith("@/") || id.startsWith("@tauri-apps/") || (id === "react" && importer?.endsWith("ServerUpdateCard.tsx"))) return "\0" + id; + }, + load(id) { + if (!id.startsWith("\0")) return; + return stubs[id.slice(1)] ?? `export const ${id.split("/").at(-1)}=()=>null;`; + }, + }], +}); +const result = await bundle.generate({ format: "cjs" }); +await bundle.close(); +const module = { exports: {} }; +new Function("require", "module", "exports", result.output[0].code)(createRequire(import.meta.url), module, module.exports); + +const offer = { from_version: "0.261116.11201", to_version: "0.261119.11401", items: [{ path: "bin/worldserver.exe", action: "replace" }], conflicts: [], migrations: 0, download_bytes: 5 << 20 }; +async function render(update) { + globalThis.__cardUpdate = { preview: null, available: false, checking: false, applying: false, step: null, percent: 0, error: null, uiError: null, stateError: null, stateRevision: 0, result: null, ...update }; + const slots = [], deps = []; + let index = 0, effects = [], dirty = false; + globalThis.__hooks = { + useState(initial) { + const i = index++; + if (!(i in slots)) slots[i] = typeof initial === "function" ? initial() : initial; + return [slots[i], (next) => { const value = typeof next === "function" ? next(slots[i]) : next; if (!Object.is(value, slots[i])) { slots[i] = value; dirty = true; } }]; + }, + useEffect(fn, list) { + const i = index++; + const before = deps[i]; + if (!before || !list || list.some((d, k) => !Object.is(d, before[k]))) effects.push(fn); + deps[i] = list; + }, + }; + let html = ""; + for (let pass = 0; pass < 10; pass++) { + index = 0; effects = []; dirty = false; + html = renderToStaticMarkup(createElement(module.exports.ServerUpdateCard, { serverId: "fixture", onHistory() {}, onAttention() {} })); + effects.forEach((fn) => fn()); + await new Promise((resolve) => setTimeout(resolve, 0)); + if (!dirty) break; + } + return html.replaceAll(""", '"'); +} + +test("Settings card shows the installed version and the update button from the shared offer", async () => { + const html = await render({ preview: offer, available: true }); + assert.match(html, /settings\.version\{"v":"0\.261116\.11201"\}/); + assert.match(html, /upd\.availableFrom/); + assert.match(html, /upd\.now/); +}); + +test("Settings card keeps the offer after an update attempt already finished (it is not wiped on open)", async () => { + const html = await render({ preview: offer, available: true, stateRevision: 3 }); + assert.match(html, /settings\.version\{"v":"0\.261116\.11201"\}/); + assert.match(html, /upd\.now/); +}); + +test("Settings card shows an up-to-date server as current, without an update button", async () => { + const same = { ...offer, to_version: offer.from_version, items: [{ path: "bin/worldserver.exe", action: "skip" }], pending_migrations: 0 }; + const html = await render({ preview: same }); + assert.match(html, /upd\.current/); + assert.doesNotMatch(html, /upd\.now/); +}); + +test("Settings card shows a placeholder while no check has finished", async () => { + const html = await render({ checking: true }); + assert.doesNotMatch(html, /settings\.version/); + assert.doesNotMatch(html, /upd\.now/); +});