Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

3.4 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

import type { InterpolationValues, Locale, TranslationKey } from "@/i18n";
import { formatRelativeTime } from "../../i18n/relative-time.ts";
import { formatDate, formatNumber } from "../../lib/intl-cache.ts";
import { withTimeFormat } from "../../lib/time-format.ts";

type Translate = (key: TranslationKey, values?: InterpolationValues) => string;

const yesterdayFormatters = new Map<Locale, Intl.RelativeTimeFormat>();

function yesterday(locale: Locale): string {
  let formatter = yesterdayFormatters.get(locale);
  if (!formatter) {
    formatter = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
    yesterdayFormatters.set(locale, formatter);
  }
  return formatter.format(-1, "day");
}

const DAY_MS = 86_400_000;

function startOfDay(date: Date): number {
  return new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
}

export function formatCardTime(ts: number, locale: Locale, now: number = Date.now()): string {
  if (!Number.isFinite(ts)) return "";
  const then = new Date(ts);
  const today = new Date(now);
  // Rounded: DST days are 23 or 25 hours long.
  const days = Math.round((startOfDay(today) - startOfDay(then)) / DAY_MS);
  if (days === 0) {
    return formatDate(then, locale, withTimeFormat({ hour: "numeric", minute: "2-digit" }, locale));
  }
  if (days === 1) {
    const word = yesterday(locale);
    return word.charAt(0).toLocaleUpperCase(locale) + word.slice(1);
  }
  if (days > 1 && days < 7) return formatDate(then, locale, { weekday: "long" });
  return formatDate(then, locale, {
    month: "short",
    day: "numeric",
    year: then.getFullYear() === today.getFullYear() ? undefined : "numeric",
  });
}

export function formatActivityTime(ts: number, locale: Locale, t: Translate): string {
  const elapsed = Date.now() - ts;
  if (elapsed < -60_000) return formatCardTime(ts, locale);
  const minutes = Math.floor(elapsed / 60_000);
  if (minutes < 1) return t("library.list.justNow");
  if (minutes < 60) return formatRelativeTime(locale, -minutes, "minute");
  const hours = Math.floor(minutes / 60);
  if (hours < 24) return formatRelativeTime(locale, -hours, "hour");
  const days = Math.round((startOfDay(new Date()) - startOfDay(new Date(ts))) / DAY_MS);
  if (days <= 1) return yesterday(locale);
  if (days < 7) return formatRelativeTime(locale, -days, "day");
  return formatCardTime(ts, locale);
}

const SIZE_UNITS = [
  "library.size.kilobytes",
  "library.size.megabytes",
  "library.size.gigabytes",
  "library.size.terabytes",
] as const satisfies readonly TranslationKey[];

export function formatSize(bytes: number | null, locale: Locale, t: Translate): string | null {
  if (bytes === null) return null;
  if (bytes < 1024) {
    return t("library.size.bytes", { value: formatNumber(bytes, locale, { useGrouping: false }) });
  }
  let value = bytes / 1024;
  let unit: (typeof SIZE_UNITS)[number] = SIZE_UNITS[0];
  for (const next of SIZE_UNITS.slice(1)) {
    if (value < 1024) break;
    value /= 1024;
    unit = next;
  }
  const digits = value >= 10 ? 0 : 1;
  const formatted = formatNumber(value, locale, {
    minimumFractionDigits: digits,
    maximumFractionDigits: digits,
  });
  return t(unit, { value: formatted });
}

export function formatItemCount(count: number, t: Translate): string {
  return t(count === 1 ? "library.itemCountOne" : "library.itemCount", { count });
}