"use client"; import { useEffect, useState } from "react"; import type { VersionInfo } from "@/lib/versionInfo"; const EMPTY: VersionInfo = { version: null, tag: null, gitDescribe: null, commitHash: null, commitDate: null, commitSubject: null, lastUpdated: null, commitMessage: null, buildTime: null, }; function resolveVersionLabel(info: VersionInfo): string { // Coolify strips .git from the build context, so only commitHash (via the // SOURCE_COMMIT build arg) resolves there — compose it into the label so // every deploy shows a distinct version without a manual semver bump. if (info.gitDescribe) return info.gitDescribe; if (info.tag) return info.tag; if (info.version && info.commitHash) return `${info.version}+${info.commitHash}`; return info.version ?? "unknown version"; } function formatCommitDate(iso: string | null): string | null { if (!iso) return null; const date = new Date(iso); if (Number.isNaN(date.getTime())) return null; return date.toLocaleString("en-US", { year: "numeric", month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", }); } const VersionOverlay = ({ canSeeCommit }: { canSeeCommit: boolean }) => { const [info, setInfo] = useState(null); useEffect(() => { let cancelled = false; void fetch("/api/version", { cache: "no-store" }) .then((res) => res.json() as Promise) .then((data) => { if (!cancelled) setInfo(data); }) .catch(() => { if (!cancelled) setInfo(EMPTY); }); return () => { cancelled = true; }; }, []); const resolved = info ?? EMPTY; const versionLabel = resolveVersionLabel(resolved); const lastUpdated = formatCommitDate( resolved.lastUpdated ?? resolved.commitDate ?? resolved.buildTime, ); const commitMessage = resolved.commitMessage ?? resolved.commitSubject; const commitInfo = resolved.commitHash && commitMessage ? `${resolved.commitHash} · ${commitMessage}` : (resolved.commitHash ?? commitMessage ?? null); const showSecondary = lastUpdated !== null || commitInfo !== null; return (
version {versionLabel} {showSecondary && ( {lastUpdated && <>last updated {lastUpdated}} {lastUpdated && commitInfo &&
} {canSeeCommit && commitInfo && {commitInfo}}
)}
); }; export default VersionOverlay;