Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

5.8 KB
Raw Blame
/**
 * Version identity + update check, shared by every surface that shows either.
 *
 * Both requests fire once per browser session, from the first hook activated with
 * `active` — the app layout, which activates on mount so the update badge can appear on a
 * fresh load without the user opening anything. The results are cached at module level for
 * the rest of the session: a locale switch remounts the whole tree, and component state
 * would refetch on every remount. Passive consumers (`active: false`) never fetch and read
 * that cache. Failures resolve to null and clear the shared promise so a later activation
 * retries; nothing is surfaced as an error (the footer simply shows nothing, and "no update
 * known" hides the reminder and every badge). The server caches its own lookup for an hour
 * and shares one in-flight call, so an app load costs no outbound request of its own, and
 * `ADELIE_UPDATE_CHECK=off` still answers without dialing out at all. forceUpdateCheck is
 * the user asking directly: it refetches past the server's TTL and broadcasts the result to
 * every mounted hook.
 */
import { useEffect, useState } from "react";
import type { UpdateCheckResponse, VersionResponse } from "@adelie/server/api";
import * as api from "../api/endpoints";

let versionCache: VersionResponse | null = null;
let versionPromise: Promise<VersionResponse> | null = null;
let updateCache: UpdateCheckResponse | null = null;
let updatePromise: Promise<UpdateCheckResponse> | null = null;

/**
 * Mounted hooks subscribe here so any refresh of the module cache reaches every
 * consumer at once — the footer, the update dots, the reminder rows, and the draft
 * page's version line all react without a remount. Two paths push: forceUpdateCheck
 * (the sidebar's manual "check for updates" action) and the shared fetch resolving.
 * Active hooks await the shared promise themselves, but passive ones (active=false,
 * e.g. the collapsed rail's avatar dot) only ever read the cache — without this push
 * they would miss a result that lands while they are mounted.
 */
const listeners = new Set<() => void>();

/** Pushes the current module cache to every mounted hook (see the listeners comment). */
function notifyAll(): void {
  for (const notify of listeners) notify();
}

/** How one manual update check ended, for user feedback — exactly one notice per outcome. */
export type UpdateCheckOutcome =
  | { kind: "disabled" }
  | { kind: "failed" }
  | { kind: "up-to-date" }
  | { kind: "found"; latestVersion: string };

/**
 * Classifies a manual check result. Order matters: `disabled` means no lookup ran, `error`
 * means the lookup ran and failed (the response is fail-soft, not an exception), and only a
 * result that names the newer release counts as `found` — updateAvailable without a version
 * would leave the row and the toast with nothing to show.
 */
export function updateCheckOutcome(res: UpdateCheckResponse): UpdateCheckOutcome {
  if (res.disabled === true) return { kind: "disabled" };
  if (res.error !== undefined) return { kind: "failed" };
  if (res.updateAvailable && res.latestVersion !== null) {
    return { kind: "found", latestVersion: res.latestVersion };
  }
  return { kind: "up-to-date" };
}

export interface VersionInfo {
  version: VersionResponse | null;
  update: UpdateCheckResponse | null;
}

/** The module cache as it stands, for callers outside React (the update flow's actions). */
export function getVersionInfo(): VersionInfo {
  return { version: versionCache, update: updateCache };
}

export function useVersionInfo(active: boolean): VersionInfo {
  // Initial state comes from the module cache, so a remounted sidebar (locale switch)
  // shows the version footer and the update dot immediately, with no second request.
  const [version, setVersion] = useState<VersionResponse | null>(versionCache);
  const [update, setUpdate] = useState<UpdateCheckResponse | null>(updateCache);

  // Re-sync from the module cache whenever forceUpdateCheck pushes a fresh result.
  useEffect(() => {
    const sync = () => {
      setVersion(versionCache);
      setUpdate(updateCache);
    };
    listeners.add(sync);
    return () => {
      listeners.delete(sync);
    };
  }, []);

  useEffect(() => {
    if (!active) return;
    let cancelled = false;

    versionPromise ??= api.getVersion().then((res) => {
      versionCache = res;
      notifyAll();
      return res;
    });
    versionPromise
      .then((res) => {
        if (!cancelled) setVersion(res);
      })
      .catch(() => {
        versionPromise = null;
      });

    updatePromise ??= api.checkUpdate().then((res) => {
      updateCache = res;
      notifyAll();
      return res;
    });
    updatePromise
      .then((res) => {
        if (!cancelled) setUpdate(res);
      })
      .catch(() => {
        updatePromise = null;
      });

    return () => {
      cancelled = true;
    };
  }, [active]);

  return { version, update };
}

/**
 * Forced re-check for the sidebar's manual "check for updates" action: asks the server
 * to bypass its TTL cache (?force=1), replaces the module cache, and pushes the result
 * to every mounted consumer. The shared promise is swapped in up front so consumers
 * activating mid-flight await the fresh lookup instead of resurrecting a stale one.
 * The update check itself stays fail-soft (a lookup failure resolves normally with
 * `error` set); this rejects only when the request to our own server fails — then the
 * shared promise is cleared so the passive path can retry, and the caller toasts.
 */
export async function forceUpdateCheck(): Promise<UpdateCheckResponse> {
  const promise = api.checkUpdate(true).then((res) => {
    updateCache = res;
    return res;
  });
  updatePromise = promise;
  try {
    return await promise;
  } catch (e) {
    if (updatePromise === promise) updatePromise = null;
    throw e;
  } finally {
    notifyAll();
  }
}