mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-21 11:25:56 +00:00
Settings: a running server update survives leaving the tab
The update's busy flag, progress and result were local to the Settings card, so switching tabs and coming back showed 'Update now' again while the update was still running (and the server rightly answered 'another action is still in progress'). They now live in the shared server-update store: the card re-attaches to the running update with its progress, and shows how it ended. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
645ffd8999
commit
2c0bb2bac3
3 files changed
+46
-32
No files matched your search
+24
-8
@@ -1,6 +1,6 @@
|
||||
import { useSyncExternalStore } from "react";
|
||||
import { listen } from "@tauri-apps/api/event";
|
||||
import { api, asUiError, type UpdatePreview } from "@/lib/api";
|
||||
import { api, asUiError, type UiError, type UpdatePreview, type UpdateTxn } from "@/lib/api";
|
||||
|
||||
/**
|
||||
* Server update status for the main screen and the navigation: checked when the app starts and every five minutes,
|
||||
@@ -14,9 +14,13 @@ export interface ServerUpdateState {
|
||||
step: string | null;
|
||||
percent: number;
|
||||
error: string | null;
|
||||
/** The full error of the last update attempt, for the Settings card. */
|
||||
uiError: UiError | null;
|
||||
/** How the last update ended; kept here so the Settings card can show it after the user left and came back. */
|
||||
result: { committed: string } | { pending: UpdateTxn } | null;
|
||||
}
|
||||
|
||||
const EMPTY: ServerUpdateState = { preview: null, available: false, checking: false, applying: false, step: null, percent: 0, error: null };
|
||||
const EMPTY: ServerUpdateState = { preview: null, available: false, checking: false, applying: false, step: null, percent: 0, error: null, uiError: null, result: null };
|
||||
const states = new Map<string, ServerUpdateState>();
|
||||
const listeners = new Set<() => void>();
|
||||
export const CHECK_EVERY_MS = 5 * 60 * 1000;
|
||||
@@ -49,11 +53,21 @@ export function startServerUpdatePolling(id: string): () => void {
|
||||
return () => clearInterval(t);
|
||||
}
|
||||
|
||||
/** Apply the pending update when no file needs a decision. Resolves true on success. */
|
||||
export async function applyServerUpdate(id: string): Promise<boolean> {
|
||||
export function clearServerUpdateResult(id: string) {
|
||||
set(id, { result: null, uiError: null });
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply an update. Without options this is the main-screen button: the pending update, only when no file needs a
|
||||
* decision. With options (the Settings card) the owner has already chosen what to do with each changed file and may
|
||||
* point at another package source. The state lives here, not in a screen, so leaving the Settings tab and coming back
|
||||
* shows the update still running (and its progress) instead of an "Update now" button the server would refuse.
|
||||
*/
|
||||
export async function applyServerUpdate(id: string, opts?: { choices: Record<string, "keep" | "replace">; source?: string }): Promise<boolean> {
|
||||
const p = get(id).preview;
|
||||
if (!p || p.conflicts.length > 0 || get(id).applying) return false;
|
||||
set(id, { applying: true, step: null, percent: 0, error: null });
|
||||
if (get(id).applying) return false;
|
||||
if (!opts && (!p || p.conflicts.length > 0)) return false;
|
||||
set(id, { applying: true, step: null, percent: 0, error: null, uiError: null, result: null });
|
||||
let un: (() => void) | undefined;
|
||||
try {
|
||||
un = await listen<{ step: string; percent: number }>("update-progress", (e) => set(id, { step: e.payload.step, percent: e.payload.percent }));
|
||||
@@ -61,10 +75,12 @@ export async function applyServerUpdate(id: string): Promise<boolean> {
|
||||
/* progress is optional */
|
||||
}
|
||||
try {
|
||||
await api.applyUpdate(id, {});
|
||||
const out = await api.applyUpdate(id, opts?.choices ?? {}, opts?.source);
|
||||
set(id, { result: out.txn.state === "committed" ? { committed: out.txn.to_version ?? "" } : { pending: out.txn } });
|
||||
return true;
|
||||
} catch (e) {
|
||||
set(id, { error: asUiError(e).human.code });
|
||||
const ui = asUiError(e);
|
||||
set(id, { error: ui.human.code, uiError: ui });
|
||||
return false;
|
||||
} finally {
|
||||
un?.();
|
||||
|
||||
Reference in new issue
Block a user