mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-19 11:25:56 +00:00
Self-update in the background (install on close), module configs created from .dist, quiet-logging + table fixups in clean-base, backlog notes
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2d0bfd0a38
commit
a6ae4e78a3
15 files changed
+358
-55
No files matched your search
+24
-1
@@ -4,10 +4,12 @@ import { Welcome } from "@/screens/Welcome";
|
||||
import { ImportServer } from "@/screens/ImportServer";
|
||||
import { Shell } from "@/screens/Shell";
|
||||
import { InstallServer } from "@/screens/InstallServer";
|
||||
import { UpdateBanner } from "@/components/UpdateBanner";
|
||||
import { autoUpdateEnabled, installOnClose, lookForUpdate } from "@/lib/selfUpdate";
|
||||
|
||||
type View = "welcome" | "add" | "import" | "install";
|
||||
|
||||
export default function App() {
|
||||
function Main() {
|
||||
const [servers, setServers] = useState<ServerSummary[] | null>(null);
|
||||
const [view, setView] = useState<View>("welcome");
|
||||
const [activeId, setActiveId] = useState<string | null>(null);
|
||||
@@ -75,3 +77,24 @@ export default function App() {
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export default function App() {
|
||||
// Quietly fetch a new Manager version a few seconds after start and install it when the window is closed.
|
||||
useEffect(() => {
|
||||
const stop = installOnClose();
|
||||
const timer = setTimeout(() => {
|
||||
if (autoUpdateEnabled()) void lookForUpdate({ download: true });
|
||||
}, 5000);
|
||||
return () => {
|
||||
clearTimeout(timer);
|
||||
stop();
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Main />
|
||||
<UpdateBanner />
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { Download } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { useT } from "@/i18n";
|
||||
import { installNow, useSelfUpdate } from "@/lib/selfUpdate";
|
||||
|
||||
/** Small notice shown once a new Manager version is downloaded and waiting to be installed. */
|
||||
export function UpdateBanner() {
|
||||
const t = useT();
|
||||
const s = useSelfUpdate();
|
||||
if (s.phase !== "ready" && s.phase !== "installing") return null;
|
||||
return (
|
||||
<div className="fixed bottom-5 right-5 z-50 flex max-w-sm items-start gap-3 rounded-card border border-gold/50 bg-card p-4 shadow-[0_8px_30px_rgb(0_0_0/0.5)]" role="status">
|
||||
<Download className="mt-0.5 h-5 w-5 shrink-0 text-gold" aria-hidden />
|
||||
<div className="text-sm">
|
||||
<p className="font-medium">{t("selfupd.ready", { v: s.version ?? "" })}</p>
|
||||
<p className="mt-0.5 text-muted">{s.phase === "installing" ? t("selfupd.installing") : t("selfupd.onClose")}</p>
|
||||
{s.phase === "ready" && (
|
||||
<Button className="mt-2" size="sm" variant="secondary" onClick={() => void installNow()}>
|
||||
{t("selfupd.now")}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -422,6 +422,14 @@ const de: Partial<Record<keyof typeof en, string>> = {
|
||||
"err.package_not_published.message": "Am Download-Ort wurde noch nichts veröffentlicht. Versuche es später erneut oder installiere aus einem lokalen Paketordner (Erweitert).",
|
||||
"err.network_unreachable.title": "Der Download-Server war nicht erreichbar",
|
||||
"err.network_unreachable.message": "Prüfe deine Internetverbindung und versuche es erneut.",
|
||||
|
||||
// Manager self-update
|
||||
"selfupd.ready": "Manager {v} ist installationsbereit",
|
||||
"selfupd.onClose": "Es wird installiert, wenn du den Manager schließt. Deine Server laufen weiter.",
|
||||
"selfupd.installing": "Installation läuft … der Manager wird geschlossen.",
|
||||
"selfupd.now": "Jetzt installieren",
|
||||
"about.auto": "Updates im Hintergrund laden und beim Schließen des Managers installieren",
|
||||
"about.autoHint": "Ein Manager-Update stoppt oder verändert deine Server nie.",
|
||||
};
|
||||
|
||||
export default de;
|
||||
@@ -422,6 +422,14 @@ const en = {
|
||||
"err.package_not_published.message": "Nothing has been published at the download location. Try again later, or install from a local package folder (Advanced).",
|
||||
"err.network_unreachable.title": "Could not reach the download server",
|
||||
"err.network_unreachable.message": "Check your internet connection and try again.",
|
||||
|
||||
// Manager self-update
|
||||
"selfupd.ready": "Manager {v} is ready to install",
|
||||
"selfupd.onClose": "It will be installed when you close the Manager. Your servers keep running.",
|
||||
"selfupd.installing": "Installing… the Manager will close.",
|
||||
"selfupd.now": "Install now",
|
||||
"about.auto": "Download updates in the background and install them when I close the Manager",
|
||||
"about.autoHint": "Your servers are never stopped or changed by a Manager update.",
|
||||
} as const;
|
||||
|
||||
export default en;
|
||||
@@ -422,6 +422,14 @@ const es: Partial<Record<keyof typeof en, string>> = {
|
||||
"err.package_not_published.message": "Aún no se ha publicado nada en la ubicación de descarga. Inténtalo más tarde o instala desde una carpeta de paquete local (Avanzado).",
|
||||
"err.network_unreachable.title": "No se pudo contactar con el servidor de descargas",
|
||||
"err.network_unreachable.message": "Comprueba tu conexión a internet e inténtalo de nuevo.",
|
||||
|
||||
// Manager self-update
|
||||
"selfupd.ready": "Manager {v} está listo para instalarse",
|
||||
"selfupd.onClose": "Se instalará cuando cierres el Manager. Tus servidores siguen funcionando.",
|
||||
"selfupd.installing": "Instalando… el Manager se cerrará.",
|
||||
"selfupd.now": "Instalar ahora",
|
||||
"about.auto": "Descargar las actualizaciones en segundo plano e instalarlas al cerrar el Manager",
|
||||
"about.autoHint": "Una actualización del Manager nunca detiene ni cambia tus servidores.",
|
||||
};
|
||||
|
||||
export default es;
|
||||
@@ -422,6 +422,14 @@ const fr: Partial<Record<keyof typeof en, string>> = {
|
||||
"err.package_not_published.message": "Rien n’a encore été publié à l’emplacement de téléchargement. Réessayez plus tard ou installez depuis un dossier de paquet local (Avancé).",
|
||||
"err.network_unreachable.title": "Impossible de joindre le serveur de téléchargement",
|
||||
"err.network_unreachable.message": "Vérifiez votre connexion internet et réessayez.",
|
||||
|
||||
// Manager self-update
|
||||
"selfupd.ready": "Manager {v} est prêt à être installé",
|
||||
"selfupd.onClose": "Il sera installé à la fermeture du Manager. Vos serveurs continuent de fonctionner.",
|
||||
"selfupd.installing": "Installation… le Manager va se fermer.",
|
||||
"selfupd.now": "Installer maintenant",
|
||||
"about.auto": "Télécharger les mises à jour en arrière-plan et les installer à la fermeture du Manager",
|
||||
"about.autoHint": "Une mise à jour du Manager n’arrête ni ne modifie jamais vos serveurs.",
|
||||
};
|
||||
|
||||
export default fr;
|
||||
@@ -422,6 +422,14 @@ const ru: Partial<Record<keyof typeof en, string>> = {
|
||||
"err.package_not_published.message": "В месте загрузки пока ничего не опубликовано. Повторите позже или установите из локальной папки с пакетом (Дополнительно).",
|
||||
"err.network_unreachable.title": "Не удалось связаться с сервером загрузки",
|
||||
"err.network_unreachable.message": "Проверьте подключение к интернету и повторите попытку.",
|
||||
|
||||
// Manager self-update
|
||||
"selfupd.ready": "Менеджер {v} готов к установке",
|
||||
"selfupd.onClose": "Он установится, когда вы закроете менеджер. Ваши серверы продолжат работать.",
|
||||
"selfupd.installing": "Установка… менеджер закроется.",
|
||||
"selfupd.now": "Установить сейчас",
|
||||
"about.auto": "Скачивать обновления в фоне и устанавливать их, когда я закрою менеджер",
|
||||
"about.autoHint": "Обновление менеджера никогда не останавливает и не меняет ваши серверы.",
|
||||
};
|
||||
|
||||
export default ru;
|
||||
@@ -0,0 +1,133 @@
|
||||
import { useSyncExternalStore } from "react";
|
||||
import { getCurrentWindow } from "@tauri-apps/api/window";
|
||||
import { check, type Update } from "@tauri-apps/plugin-updater";
|
||||
import { relaunch } from "@tauri-apps/plugin-process";
|
||||
|
||||
/**
|
||||
* Manager self-update: look for a new version in the background, download it quietly, and install it when the window is
|
||||
* closed (or right away when asked). Servers are separate processes, so updating the Manager never touches them.
|
||||
*/
|
||||
export type Phase = "idle" | "checking" | "downloading" | "ready" | "installing" | "error" | "current";
|
||||
export interface SelfUpdateState {
|
||||
phase: Phase;
|
||||
version?: string;
|
||||
notes?: string;
|
||||
percent: number | null;
|
||||
/** Raw error text, kept for the details line (the UI shows a friendly message). */
|
||||
error?: string;
|
||||
}
|
||||
|
||||
const AUTO_KEY = "coa-auto-update";
|
||||
let state: SelfUpdateState = { phase: "idle", percent: null };
|
||||
let update: Update | null = null;
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
function set(next: Partial<SelfUpdateState>) {
|
||||
state = { ...state, ...next };
|
||||
listeners.forEach((l) => l());
|
||||
}
|
||||
|
||||
export function autoUpdateEnabled(): boolean {
|
||||
try {
|
||||
return localStorage.getItem(AUTO_KEY) !== "off";
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
export function setAutoUpdate(on: boolean) {
|
||||
try {
|
||||
localStorage.setItem(AUTO_KEY, on ? "on" : "off");
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
listeners.forEach((l) => l());
|
||||
}
|
||||
|
||||
/** Look for a new version. With `download` it also fetches the installer so it is ready to install on exit. */
|
||||
export async function lookForUpdate(opts: { download: boolean }): Promise<void> {
|
||||
if (state.phase === "checking" || state.phase === "downloading" || state.phase === "installing") return;
|
||||
set({ phase: "checking", error: undefined, percent: null });
|
||||
try {
|
||||
const u = await check();
|
||||
if (!u) {
|
||||
update = null;
|
||||
set({ phase: "current", version: undefined });
|
||||
return;
|
||||
}
|
||||
update = u;
|
||||
set({ version: u.version, notes: u.body ?? undefined });
|
||||
if (!opts.download) {
|
||||
set({ phase: "idle" });
|
||||
return;
|
||||
}
|
||||
await downloadPending();
|
||||
} catch (e) {
|
||||
set({ phase: "error", error: String(e) });
|
||||
}
|
||||
}
|
||||
|
||||
async function downloadPending(): Promise<void> {
|
||||
if (!update) return;
|
||||
set({ phase: "downloading", percent: 0 });
|
||||
let total = 0;
|
||||
let done = 0;
|
||||
try {
|
||||
await update.download((e) => {
|
||||
if (e.event === "Started") total = e.data.contentLength ?? 0;
|
||||
if (e.event === "Progress") {
|
||||
done += e.data.chunkLength;
|
||||
if (total) set({ percent: Math.round((done / total) * 100) });
|
||||
}
|
||||
});
|
||||
set({ phase: "ready", percent: 100 });
|
||||
} catch (e) {
|
||||
set({ phase: "error", error: String(e), percent: null });
|
||||
}
|
||||
}
|
||||
|
||||
/** Download (if needed) and install now. The installer ends this process; `relaunch` is the fallback. */
|
||||
export async function installNow(): Promise<void> {
|
||||
if (!update) return;
|
||||
try {
|
||||
if (state.phase !== "ready") await downloadPending();
|
||||
if (state.phase !== "ready") return;
|
||||
set({ phase: "installing" });
|
||||
await update.install();
|
||||
await relaunch();
|
||||
} catch (e) {
|
||||
set({ phase: "error", error: String(e) });
|
||||
}
|
||||
}
|
||||
|
||||
/** Install a downloaded update when the user closes the window. Call once at start-up. */
|
||||
export function installOnClose(): () => void {
|
||||
let stop: (() => void) | undefined;
|
||||
void getCurrentWindow()
|
||||
.onCloseRequested(async (event) => {
|
||||
if (state.phase !== "ready" || !update || !autoUpdateEnabled()) return;
|
||||
event.preventDefault();
|
||||
try {
|
||||
set({ phase: "installing" });
|
||||
await update.install();
|
||||
} catch (e) {
|
||||
set({ phase: "error", error: String(e) });
|
||||
await getCurrentWindow().destroy();
|
||||
}
|
||||
})
|
||||
.then((un) => {
|
||||
stop = un;
|
||||
})
|
||||
.catch(() => {});
|
||||
return () => stop?.();
|
||||
}
|
||||
|
||||
export function useSelfUpdate(): SelfUpdateState {
|
||||
return useSyncExternalStore(
|
||||
(l) => {
|
||||
listeners.add(l);
|
||||
return () => listeners.delete(l);
|
||||
},
|
||||
() => state,
|
||||
);
|
||||
}
|
||||
+50
-53
@@ -1,81 +1,78 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { getVersion } from "@tauri-apps/api/app";
|
||||
import { check, type Update } from "@tauri-apps/plugin-updater";
|
||||
import { relaunch } from "@tauri-apps/plugin-process";
|
||||
import { Loader2 } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card } from "@/components/ui/card";
|
||||
import { useT } from "@/i18n";
|
||||
import { autoUpdateEnabled, installNow, lookForUpdate, setAutoUpdate, useSelfUpdate } from "@/lib/selfUpdate";
|
||||
|
||||
export function AboutCard() {
|
||||
const t = useT();
|
||||
const s = useSelfUpdate();
|
||||
const [version, setVersion] = useState("");
|
||||
const [update, setUpdate] = useState<Update | null>(null);
|
||||
const [busy, setBusy] = useState<"check" | "install" | null>(null);
|
||||
const [percent, setPercent] = useState<number | null>(null);
|
||||
const [note, setNote] = useState<string | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [auto, setAuto] = useState(autoUpdateEnabled());
|
||||
const [asked, setAsked] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
void getVersion().then(setVersion).catch(() => {});
|
||||
}, []);
|
||||
|
||||
async function look() {
|
||||
setBusy("check");
|
||||
setError(null);
|
||||
setNote(null);
|
||||
try {
|
||||
const u = await check();
|
||||
setUpdate(u);
|
||||
if (!u) setNote(t("about.upToDate"));
|
||||
} catch {
|
||||
setError(t("about.checkFailed"));
|
||||
} finally {
|
||||
setBusy(null);
|
||||
}
|
||||
}
|
||||
|
||||
async function install() {
|
||||
if (!update) return;
|
||||
setBusy("install");
|
||||
setError(null);
|
||||
let total = 0;
|
||||
let done = 0;
|
||||
try {
|
||||
await update.downloadAndInstall((e) => {
|
||||
if (e.event === "Started") total = e.data.contentLength ?? 0;
|
||||
if (e.event === "Progress") {
|
||||
done += e.data.chunkLength;
|
||||
if (total) setPercent(Math.round((done / total) * 100));
|
||||
}
|
||||
});
|
||||
await relaunch();
|
||||
} catch {
|
||||
setError(t("about.installFailed"));
|
||||
setBusy(null);
|
||||
}
|
||||
}
|
||||
const busy = s.phase === "checking" || s.phase === "downloading" || s.phase === "installing";
|
||||
|
||||
return (
|
||||
<Card className="mt-6 p-6">
|
||||
<h2 className="font-semibold">{t("app.name")}</h2>
|
||||
<p className="mt-1 text-sm text-muted">{t("about.version", { v: version || "…" })}</p>
|
||||
<div className="mt-4 flex items-center gap-3">
|
||||
<Button size="sm" disabled={!!busy} onClick={() => void look()}>
|
||||
{busy === "check" && <Loader2 className="h-4 w-4 animate-spin" aria-hidden />}
|
||||
|
||||
<label className="mt-3 flex cursor-pointer items-start gap-2 text-sm">
|
||||
<input
|
||||
type="checkbox"
|
||||
className="mt-1"
|
||||
checked={auto}
|
||||
onChange={(e) => {
|
||||
setAuto(e.target.checked);
|
||||
setAutoUpdate(e.target.checked);
|
||||
}}
|
||||
/>
|
||||
<span>
|
||||
{t("about.auto")}
|
||||
<span className="block text-xs text-muted">{t("about.autoHint")}</span>
|
||||
</span>
|
||||
</label>
|
||||
|
||||
<div className="mt-4 flex flex-wrap items-center gap-3">
|
||||
<Button
|
||||
size="sm"
|
||||
disabled={busy}
|
||||
onClick={() => {
|
||||
setAsked(true);
|
||||
void lookForUpdate({ download: false });
|
||||
}}
|
||||
>
|
||||
{s.phase === "checking" && <Loader2 className="h-4 w-4 animate-spin" aria-hidden />}
|
||||
{t("about.check")}
|
||||
</Button>
|
||||
{update && (
|
||||
<Button size="sm" variant="primary" disabled={!!busy} onClick={() => void install()}>
|
||||
{busy === "install" && <Loader2 className="h-4 w-4 animate-spin" aria-hidden />}
|
||||
{t("about.install", { v: update.version })}
|
||||
{s.version && s.phase !== "current" && (
|
||||
<Button size="sm" variant="primary" disabled={busy} onClick={() => void installNow()}>
|
||||
{(s.phase === "downloading" || s.phase === "installing") && <Loader2 className="h-4 w-4 animate-spin" aria-hidden />}
|
||||
{t("about.install", { v: s.version })}
|
||||
</Button>
|
||||
)}
|
||||
{percent !== null && busy === "install" && <span className="text-sm text-muted" role="status">{percent}%</span>}
|
||||
{s.percent !== null && s.phase === "downloading" && (
|
||||
<span className="text-sm text-muted" role="status">
|
||||
{s.percent}%
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
{update?.body && <p className="mt-3 whitespace-pre-line text-sm text-muted">{update.body}</p>}
|
||||
{note && <p className="mt-3 text-sm text-ok" role="status">{note}</p>}
|
||||
{error && <p className="mt-3 text-sm text-bad" role="alert">{error}</p>}
|
||||
{s.version && s.notes && s.phase !== "current" && <p className="mt-3 whitespace-pre-line text-sm text-muted">{s.notes}</p>}
|
||||
{s.phase === "ready" && <p className="mt-3 text-sm text-ok" role="status">{t("selfupd.onClose")}</p>}
|
||||
{asked && s.phase === "current" && <p className="mt-3 text-sm text-ok" role="status">{t("about.upToDate")}</p>}
|
||||
{s.phase === "error" && (
|
||||
<div className="mt-3 text-sm text-bad" role="alert">
|
||||
<p>{t("about.checkFailed")}</p>
|
||||
{s.error && <p className="selectable mt-1 break-all text-xs text-muted">{s.error}</p>}
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user