refactor: github version checking

This commit is contained in:
Miodec 2024-06-12 20:04:12 +02:00
parent 26785515e9
commit 7deb58cf88
5 changed files with 78 additions and 39 deletions

View file

@ -0,0 +1,27 @@
import { isDevEnvironment } from "../utils/misc";
import * as Version from "../states/version";
export function setText(text: string): void {
$("footer .currentVersion .text").text(text);
}
export function setIndicatorVisible(state: boolean): void {
if (state) {
$("#newVersionIndicator").removeClass("hidden");
} else {
$("#newVersionIndicator").addClass("hidden");
}
}
async function update(): Promise<void> {
if (isDevEnvironment()) {
setText("localhost");
return;
}
const { version, isNew } = await Version.get();
setText(version);
setIndicatorVisible(isNew);
}
void update();

View file

@ -1,21 +0,0 @@
function setMemory(v: string): void {
window.localStorage.setItem("lastSeenVersion", v);
}
function getMemory(): string {
return window.localStorage.getItem("lastSeenVersion") ?? "";
}
export async function show(version: string): Promise<void> {
const memory = await getMemory();
if (memory === "") {
setMemory(version);
return;
}
if (memory === version) return;
void caches.keys().then(function (names) {
for (const name of names) void caches.delete(name);
});
$("#newVersionIndicator").removeClass("hidden");
setMemory(version);
}

View file

@ -40,6 +40,7 @@ import "./test/tts";
import "./elements/fps-counter";
import "./controllers/profile-search-controller";
import { isDevEnvironment } from "./utils/misc";
import "./elements/version-button";
function addToGlobal(items: Record<string, unknown>): void {
for (const [name, item] of Object.entries(items)) {

View file

@ -1,8 +1,6 @@
import Config, * as UpdateConfig from "./config";
import * as Misc from "./utils/misc";
import * as JSONData from "./utils/json-data";
import * as MonkeyPower from "./elements/monkey-power";
import * as NewVersionNotification from "./elements/version-check";
import * as Notifications from "./elements/notifications";
import * as CookiesModal from "./modals/cookies";
import * as PSA from "./elements/psa";
@ -13,22 +11,6 @@ import Konami from "konami";
import { envConfig } from "./constants/env-config";
import * as ServerConfiguration from "./ape/server-configuration";
if (Misc.isDevEnvironment()) {
$("footer .currentVersion .text").text("localhost");
$("body").prepend(
`<a class='button configureAPI' href='${envConfig.backendUrl}/configure/' target='_blank' aria-label="Configure API" data-balloon-pos="right"><i class="fas fa-fw fa-server"></i></a>`
);
} else {
JSONData.getLatestReleaseFromGitHub()
.then((v) => {
$("footer .currentVersion .text").text(v);
void NewVersionNotification.show(v);
})
.catch((e) => {
$("footer .currentVersion .text").text("unknown");
});
}
void UpdateConfig.loadFromLocalStorage();
$(document).ready(() => {
@ -89,5 +71,8 @@ $(document).ready(() => {
void registration.unregister();
}
});
$("body").prepend(
`<a class='button configureAPI' href='${envConfig.backendUrl}/configure/' target='_blank' aria-label="Configure API" data-balloon-pos="right"><i class="fas fa-fw fa-server"></i></a>`
);
}
});

View file

@ -0,0 +1,47 @@
import { getLatestReleaseFromGitHub } from "../utils/json-data";
const LOCALSTORAGE_KEY = "lastSeenVersion";
let version: null | string = null;
let isVersionNew: null | boolean = null;
function setMemory(v: string): void {
window.localStorage.setItem(LOCALSTORAGE_KEY, v);
}
function getMemory(): string {
return window.localStorage.getItem(LOCALSTORAGE_KEY) ?? "";
}
async function check(): Promise<void> {
const currentVersion = await getLatestReleaseFromGitHub();
const memoryVersion = getMemory();
version = currentVersion;
isVersionNew =
memoryVersion === "" ? false : memoryVersion !== currentVersion;
if (isVersionNew || memoryVersion === "") {
setMemory(currentVersion);
purgeCaches();
}
}
function purgeCaches(): void {
void caches.keys().then(function (names) {
for (const name of names) void caches.delete(name);
});
}
export async function get(): Promise<{
version: string;
isNew: boolean;
}> {
if (version === null || isVersionNew === null) {
await check();
}
return {
version: version as string,
isNew: isVersionNew as boolean,
};
}