Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

3.2 KB
Raw Blame
import { format, isToday, parseISO } from "date-fns";
import type { BriefingResponse } from "@/app/api/__generated__/models/briefingResponse";
import type { BriefingRunItem } from "@/app/api/__generated__/models/briefingRunItem";

// A briefing can be all decisions and no terminal runs — a run paused on an
// approval never completes — which leaves the card with nothing but a date.
// Callers check this before mounting the card, so a briefing that renders to
// nothing shows their own fallback instead of a blank surface.
export function hasRecapContent(
  briefing: BriefingResponse | null | undefined,
): briefing is BriefingResponse {
  return Boolean(briefing && briefing.content.run_items.length > 0);
}

export const COLLAPSED_ROWS = 3;

// Roughly six rows: enough that "show all" feels like it opened something,
// short enough that the card still sits under the composer.
const MAX_EXPANDED_HEIGHT = 416;

// Sub-pixel scroll offsets are routine (zoom, fractional row heights), so an
// exact comparison would leave both arrows on forever.
const EDGE_TOLERANCE = 2;

// Null when there is nothing to measure yet, so callers can leave the last
// good height in place rather than collapsing the card to zero.
export function measureListHeight(
  list: HTMLElement,
  isShowingAll: boolean,
): number | null {
  const rows = Array.from(list.children) as HTMLElement[];
  if (rows.length === 0) return null;
  if (isShowingAll) return Math.min(list.scrollHeight, MAX_EXPANDED_HEIGHT);

  const lastVisible = rows[Math.min(COLLAPSED_ROWS, rows.length) - 1];
  return lastVisible.offsetTop + lastVisible.offsetHeight - rows[0].offsetTop;
}

export function getScrollEdges(list: HTMLElement) {
  const { scrollTop, scrollHeight, clientHeight } = list;
  return {
    canScrollUp: scrollTop > EDGE_TOLERANCE,
    canScrollDown: scrollTop + clientHeight < scrollHeight - EDGE_TOLERANCE,
  };
}

export function formatBriefingDate(date: Date | string): string {
  // The generated type says Date, but date-only strings ("2026-08-07") skip
  // the client's date transformer (its regex requires a time part), so the
  // runtime value is still a string. parseISO reads it as LOCAL midnight —
  // `new Date("2026-08-07")` would read UTC midnight and shift the label a
  // day back for viewers west of UTC.
  const parsed = typeof date === "string" ? parseISO(date) : date;
  if (isToday(parsed)) return "This morning";
  return format(parsed, "MMMM d");
}

export function isInternalLink(link: string): boolean {
  return link.startsWith("/") && !link.startsWith("//");
}

// Relative paths only: the backend composes these, but nothing else stops a
// future regression from delivering an absolute or `javascript:` URL to a
// Next.js <Link>.
export function getSafeLink(link: string | null | undefined): string | null {
  return link && isInternalLink(link) ? link : null;
}

// Attribution only earns its space when it sits in front of something else:
// with a summary the row reads "Ana · Found 3 leads", and without one the
// expert's name is the whole subtitle rather than a prefix to itself.
export function getSubtitleParts(item: BriefingRunItem) {
  if (!item.summary) return { attribution: null, text: item.expert_name };
  return { attribution: item.expert_name, text: item.summary };
}