Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

♻️ Convert control panel version tile to a server component (#2866)

Luke Vella1b240f81bf24
diff --git a/apps/web/biome.json b/apps/web/biome.json
index 0e878ca55..d6dfe4f96 100644
--- a/apps/web/biome.json
+++ b/apps/web/biome.json
@@ -340,8 +340,6 @@
"src/trpc/routers/event-types.ts",
"src/trpc/routers/events.ts",
"src/trpc/routers/spaces.ts",
- "src/trpc/routers/system.ts",
- "src/trpc/routers/user.ts",
"src/trpc/routers/polls/comments.ts",
"src/trpc/routers/polls/participants.ts",
"src/features/licensing/actions.ts",
diff --git a/apps/web/public/locales/en/app.json b/apps/web/public/locales/en/app.json
index 23eda2788..686ccc0f4 100644
--- a/apps/web/public/locales/en/app.json
+++ b/apps/web/public/locales/en/app.json
@@ -773,6 +773,7 @@
"upgradePromptTitle": "Upgrade to Pro",
"upgradeToPro": "Upgrade to Pro",
"uploadImage": "Upload",
+ "upToDate": "Up to date",
"url": "URL",
"urlInvalid": "Must be a valid URL",
"user": "User",
diff --git a/apps/web/src/app/[locale]/control-panel/version-tile.tsx b/apps/web/src/app/[locale]/control-panel/version-tile.tsx
index d9be02b08..a18364ae0 100644
--- a/apps/web/src/app/[locale]/control-panel/version-tile.tsx
+++ b/apps/web/src/app/[locale]/control-panel/version-tile.tsx
@@ -1,29 +1,43 @@
-"use client";
-
+import { Skeleton } from "@rallly/ui/skeleton";
import { Tile, TileDescription, TileTitle } from "@rallly/ui/tile";
import { DownloadIcon } from "lucide-react";
+import { Suspense } from "react";
import { PageIcon } from "@/components/page-icons";
-import { Spinner } from "@/components/spinner";
-import { Trans } from "@/i18n/client";
+import { loadUpdateStatus } from "@/features/instance-settings/loaders";
+import { getTranslation } from "@/i18n/server";
import { appVersion } from "@/lib/constants";
-import { trpc } from "@/trpc/client";
const RELEASES_URL = "https://github.com/lukevella/rallly/releases";
-export function VersionTile() {
- const { data, isFetching } = trpc.system.getUpdateStatus.useQuery(undefined, {
- staleTime: 60 * 60 * 1000,
- refetchOnWindowFocus: false,
- });
+async function UpdateStatus() {
+ const update = await loadUpdateStatus();
+
+ if (!update) {
+ return null;
+ }
+
+ const { t } = await getTranslation();
+
+ if (update.status === "up-to-date") {
+ return (
+ <span className="text-green-600 text-sm">
+ {t("upToDate", { defaultValue: "Up to date" })}
+ </span>
+ );
+ }
+ return (
+ <span className="text-primary text-sm">
+ {t("updateAvailable", { defaultValue: "Update available" })}
+ </span>
+ );
+}
+
+export function VersionTile() {
return (
<Tile
render={
- <a
- href={data?.url ?? RELEASES_URL}
- target="_blank"
- rel="noreferrer noopener"
- />
+ <a href={RELEASES_URL} target="_blank" rel="noreferrer noopener" />
}
>
<PageIcon>
@@ -31,13 +45,9 @@ export function VersionTile() {
</PageIcon>
<TileTitle>{`v${appVersion ?? "unknown"}`}</TileTitle>
<TileDescription>
- {isFetching ? (
- <Spinner />
- ) : data ? (
- <span className="text-primary text-sm">
- <Trans i18nKey="updateAvailable" defaults="Update available" />
- </span>
- ) : null}
+ <Suspense fallback={<Skeleton className="h-4 w-24" />}>
+ <UpdateStatus />
+ </Suspense>
</TileDescription>
</Tile>
);
diff --git a/apps/web/src/features/instance-settings/data.ts b/apps/web/src/features/instance-settings/data.ts
index 08dc383d5..b54dd3d32 100644
--- a/apps/web/src/features/instance-settings/data.ts
+++ b/apps/web/src/features/instance-settings/data.ts
@@ -12,11 +12,13 @@ export const getInstanceSettings = unstable_cache(
id: 1,
},
select: {
+ instanceId: true,
disableUserRegistration: true,
},
});
return {
+ instanceId: instanceSettings?.instanceId ?? null,
disableUserRegistration:
instanceSettings?.disableUserRegistration ?? false,
};
diff --git a/apps/web/src/features/instance-settings/loaders.ts b/apps/web/src/features/instance-settings/loaders.ts
new file mode 100644
index 000000000..58204742f
--- /dev/null
+++ b/apps/web/src/features/instance-settings/loaders.ts
@@ -0,0 +1,15 @@
+import "server-only";
+
+import { cache } from "react";
+import { getInstanceSettings } from "./data";
+import { getUpdateStatus } from "./service";
+
+export const loadUpdateStatus = cache(async () => {
+ const { instanceId } = await getInstanceSettings();
+
+ if (!instanceId) {
+ return null;
+ }
+
+ return getUpdateStatus({ instanceId });
+});
diff --git a/apps/web/src/features/instance-settings/service.ts b/apps/web/src/features/instance-settings/service.ts
new file mode 100644
index 000000000..768b591b4
--- /dev/null
+++ b/apps/web/src/features/instance-settings/service.ts
@@ -0,0 +1,76 @@
+import "server-only";
+
+import { createLogger } from "@rallly/logger";
+import * as z from "zod";
+import { env } from "@/env";
+import { appVersion } from "@/lib/constants";
+
+const logger = createLogger("update-check");
+
+const updateStatusSchema = z.object({
+ latest: z.string(),
+ url: z.string(),
+ publishedAt: z.string(),
+});
+
+function normalizeVersion(version: string) {
+ return version.replace(/^v/, "").split(/[-+]/)[0];
+}
+
+function isOutdated(current: string, latest: string) {
+ const a = normalizeVersion(current)
+ .split(".")
+ .map((n) => Number(n) || 0);
+ const b = normalizeVersion(latest)
+ .split(".")
+ .map((n) => Number(n) || 0);
+ const len = Math.max(a.length, b.length);
+ for (let i = 0; i < len; i++) {
+ const ai = a[i] ?? 0;
+ const bi = b[i] ?? 0;
+ if (ai !== bi) return ai < bi;
+ }
+ return false;
+}
+
+export async function getUpdateStatus({ instanceId }: { instanceId: string }) {
+ if (!appVersion || !env.API_BASE_URL) return null;
+
+ const url = new URL("/api/updates", env.API_BASE_URL);
+ url.searchParams.set("version", appVersion);
+ url.searchParams.set("instanceId", instanceId);
+
+ try {
+ const res = await fetch(url, {
+ // Node fetch has no default timeout — without this a hung upstream
+ // holds the streamed slot open indefinitely on a cold cache
+ signal: AbortSignal.timeout(3000),
+ next: { revalidate: 3600 },
+ });
+ if (!res.ok) {
+ logger.warn(
+ { instanceId, version: appVersion, status: res.status },
+ "Update check returned a non-success status",
+ );
+ return null;
+ }
+ const parsed = updateStatusSchema.safeParse(await res.json());
+ if (!parsed.success) {
+ logger.warn(
+ { instanceId, version: appVersion, issues: parsed.error.issues },
+ "Update check returned an invalid response",
+ );
+ return null;
+ }
+ if (!isOutdated(appVersion, parsed.data.latest)) {
+ return { status: "up-to-date" as const };
+ }
+ return { status: "update-available" as const, ...parsed.data };
+ } catch (error) {
+ logger.warn(
+ { error, instanceId, version: appVersion },
+ "Failed to check for updates",
+ );
+ return null;
+ }
+}
diff --git a/apps/web/src/trpc/routers/index.ts b/apps/web/src/trpc/routers/index.ts
index 91dd7fe9e..c7e5ba8fe 100644
--- a/apps/web/src/trpc/routers/index.ts
+++ b/apps/web/src/trpc/routers/index.ts
@@ -9,7 +9,6 @@ import { eventTypes } from "./event-types";
import { events } from "./events";
import { polls } from "./polls";
import { spaces } from "./spaces";
-import { system } from "./system";
export const appRouter = mergeRouters(
router({
@@ -20,7 +19,6 @@ export const appRouter = mergeRouters(
events,
polls,
spaces,
- system,
calendars,
}),
);
diff --git a/apps/web/src/trpc/routers/system.ts b/apps/web/src/trpc/routers/system.ts
deleted file mode 100644
index c53f8e87f..000000000
--- a/apps/web/src/trpc/routers/system.ts
+++ /dev/null
@@ -1,80 +0,0 @@
-import { prisma } from "@rallly/database";
-import * as z from "zod";
-import { env } from "@/env";
-import { createCache } from "@/lib/cache";
-import { appVersion } from "@/lib/constants";
-import { adminProcedure, router } from "../trpc";
-
-const upstreamSchema = z.object({
- latest: z.string(),
- url: z.string(),
- publishedAt: z.string(),
-});
-
-type UpstreamPayload = z.infer<typeof upstreamSchema>;
-
-const updatesCache = createCache<UpstreamPayload>({
- namespace: "system:updates",
- ttl: "1 h",
-});
-
-function normalizeVersion(version: string) {
- return version.replace(/^v/, "").split(/[-+]/)[0];
-}
-
-function isOutdated(current: string, latest: string) {
- const a = normalizeVersion(current)
- .split(".")
- .map((n) => Number(n) || 0);
- const b = normalizeVersion(latest)
- .split(".")
- .map((n) => Number(n) || 0);
- const len = Math.max(a.length, b.length);
- for (let i = 0; i < len; i++) {
- const ai = a[i] ?? 0;
- const bi = b[i] ?? 0;
- if (ai !== bi) return ai < bi;
- }
- return false;
-}
-
-async function fetchLatestRelease(
- instanceId: string,
-): Promise<UpstreamPayload | null> {
- const cached = await updatesCache.get("latest");
- if (cached) return cached;
-
- if (!appVersion || !env.API_BASE_URL) return null;
-
- const url = new URL("/api/updates", env.API_BASE_URL);
- url.searchParams.set("version", appVersion);
- url.searchParams.set("instanceId", instanceId);
-
- try {
- const res = await fetch(url, { signal: AbortSignal.timeout(5000) });
- if (!res.ok) return null;
- const parsed = upstreamSchema.safeParse(await res.json());
- if (!parsed.success) return null;
- await updatesCache.set("latest", parsed.data);
- return parsed.data;
- } catch {
- return null;
- }
-}
-
-export const system = router({
- getUpdateStatus: adminProcedure.query(async () => {
- if (!appVersion || !env.API_BASE_URL) return null;
-
- const settings = await prisma.instanceSettings.findUnique({
- where: { id: 1 },
- select: { instanceId: true },
- });
- if (!settings) return null;
-
- const upstream = await fetchLatestRelease(settings.instanceId);
- if (!upstream) return null;
- if (!isOutdated(appVersion, upstream.latest)) return null;
- return upstream;
- }),
-});