Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
3.6 KB
Raw Blame
import { api } from "../api";
import { useAuthStore } from "../auth";
import type { User } from "../types";

export interface ReleaseStamp {
  /** MAJOR.MINOR.PATCH, matching the changelog and the server's semver check. */
  version: string;
  /** Release day, YYYY-MM-DD. */
  date: string;
}

type Triple = [number, number, number];

/**
 * Parses MAJOR.MINOR.PATCH, tolerating a leading "v" and any prerelease or
 * build suffix (server builds report e.g. "v0.7.2-12-gabc"). Returns null for
 * anything else, including the empty string dev builds report.
 */
export function parseVersion(raw: string | null | undefined): Triple | null {
  const match = /^v?(\d+)\.(\d+)\.(\d+)/.exec(raw?.trim() ?? "");
  if (!match) return null;
  return [Number(match[1]), Number(match[2]), Number(match[3])];
}

export function compareVersions(a: Triple, b: Triple): number {
  return a[0] - b[0] || a[1] - b[1] || a[2] - b[2];
}

/**
 * Releases the client can announce: those not newer than the connected
 * server, since a self-hosted server can lag the client bundle and must not
 * advertise features it doesn't run. An unknown server version (managed
 * cloud, dev builds) applies no cap. Result is newest first.
 */
export function visibleReleases<R extends ReleaseStamp>(
  releases: readonly R[],
  serverVersion: string,
): R[] {
  const cap = parseVersion(serverVersion);
  return releases
    .filter((r) => {
      const v = parseVersion(r.version);
      return v !== null && (cap === null || compareVersions(v, cap) <= 0);
    })
    .sort((a, b) => compareVersions(parseVersion(b.version)!, parseVersion(a.version)!));
}

/**
 * Releases the user hasn't seen. Before the first dismissal, only releases
 * after the account's creation day count, so a new user isn't greeted with
 * the whole backlog. Returns [] when the server can't persist the marker
 * (`whats_new_seen_version` undefined): a release that can never be marked
 * seen would stay new forever.
 */
export function unreadReleases<R extends ReleaseStamp>(
  visible: readonly R[],
  user: Pick<User, "whats_new_seen_version" | "created_at"> | null,
): R[] {
  if (!user || user.whats_new_seen_version === undefined) return [];
  const seen = parseVersion(user.whats_new_seen_version);
  if (seen) {
    return visible.filter((r) => compareVersions(parseVersion(r.version)!, seen) > 0);
  }
  const joined = user.created_at.slice(0, 10);
  if (!joined) return [];
  return visible.filter((r) => r.date > joined);
}

/**
 * Persists the dismissal. Applied optimistically so the release stops
 * counting as new at once, and rolled back on failure so the next open
 * retries. The server only advances the marker, so the response may carry a
 * newer version than `version`.
 */
export async function markWhatsNewSeen(version: string): Promise<void> {
  const requester = useAuthStore.getState().user;
  if (!requester) return;
  const previous = requester.whats_new_seen_version;

  // Writes only the marker, and only onto the account that made the request
  // while it is still signed in: the response can land after a logout or an
  // account switch, and must not resurrect or overwrite that session.
  const applyMarker = (value: string | null | undefined) => {
    const current = useAuthStore.getState().user;
    if (current?.id !== requester.id) return;
    useAuthStore.getState().setUser({ ...current, whats_new_seen_version: value });
  };

  applyMarker(version);
  try {
    const user = await api.markWhatsNewSeen(version);
    if (user.id === requester.id) applyMarker(user.whats_new_seen_version);
  } catch (err) {
    if (useAuthStore.getState().user?.whats_new_seen_version === version) applyMarker(previous);
    throw err;
  }
}