Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

7.6 KB
Raw Blame
import type { UsageProviderKind, UsageProviderPlanLimits } from "@akeru/contracts";
import { CheckIcon, XIcon } from "lucide-react";

import { cn } from "../../lib/utils";
import type { EnvironmentUsageStatus } from "@akeru/client-runtime/usage";
import { Skeleton } from "../ui/skeleton";
import { UsagePlanMeters } from "./UsageCharts";
import { PROVIDER_ORDER, PROVIDER_PRESENTATION } from "./usageProviders";

export function UsageLimitsOverview({
  planLimits,
  environments,
  isPending,
}: {
  readonly planLimits: readonly UsageProviderPlanLimits[];
  readonly environments: readonly EnvironmentUsageStatus[];
  readonly isPending: boolean;
}) {
  if (isPending && planLimits.length === 0) {
    return <UsageSkeleton />;
  }

  return (
    <div className="flex flex-col gap-9">
      {environments.length > 1 ? <UsageDeviceStrip environments={environments} /> : null}
      {planLimits.length > 0 ? (
        planLimits.map((limits) => <UsagePlanMeters key={limits.provider} limits={limits} />)
      ) : environments.length === 0 ? (
        <p className="text-sm text-muted-foreground">Connect an environment to see usage.</p>
      ) : (
        <div className="rounded-xl border border-border/70 px-5 py-6">
          <h2 className="text-sm font-medium text-foreground">No subscription limits yet</h2>
          <p className="mt-1 text-sm text-muted-foreground">
            Connect a provider subscription in Settings, then refresh this page.
          </p>
        </div>
      )}
    </div>
  );
}

/** Brand mark for the harness a row belongs to. */
export function ProviderMark({
  provider,
  className,
}: {
  readonly provider: UsageProviderKind;
  readonly className: string;
}) {
  const Mark = PROVIDER_PRESENTATION[provider].mark;

  return <Mark className={cn("shrink-0", className)} aria-hidden />;
}

export function Metric({ label, value }: { readonly label: string; readonly value: string }) {
  return (
    <div className="flex min-w-0 flex-col gap-0.5">
      <span className="text-xs text-muted-foreground">{label}</span>
      <span className="text-base font-medium text-foreground tabular-nums">{value}</span>
    </div>
  );
}

/**
 * Says plainly when the totals are incomplete: an environment that failed, or
 * one whose transcripts another environment already reported. Environments
 * that are still answering never reach this notice; the page shows the
 * loading skeleton until every one is terminal.
 */
export function UsageCoverageNotice({
  environments,
  duplicateSources,
  staleEnvironments,
}: {
  readonly environments: readonly EnvironmentUsageStatus[];
  readonly duplicateSources: readonly string[];
  readonly staleEnvironments: readonly string[];
}) {
  const failed = environments.filter((environment) => environment.error !== null);

  const stale = environments.filter((environment) =>
    staleEnvironments.includes(environment.environmentId),
  );

  if (failed.length === 0 && stale.length === 0 && duplicateSources.length === 0) {
    return null;
  }

  return (
    <div className="flex flex-col gap-1 border border-border px-3 py-2 text-xs text-muted-foreground">
      {failed.map((environment) => (
        <span key={environment.label}>{environment.label} could not report usage.</span>
      ))}
      {stale.map((environment) => (
        <span key={environment.label}>
          {environment.label} runs an older server version and is excluded from totals.
        </span>
      ))}
      {duplicateSources.length > 0 ? (
        <span>
          Counted once across clients sharing an environment usage store:{" "}
          {duplicateSources.join(", ")}
        </span>
      ) : null}
    </div>
  );
}

/**
 * Per-device progress while the page waits for every environment to answer.
 * Only rendered with two or more devices; a lone device has nothing to
 * enumerate.
 */
export function UsageDeviceStrip({
  environments,
}: {
  readonly environments: readonly EnvironmentUsageStatus[];
}) {
  const scanning = environments.filter(
    (environment) => environment.summary === null && environment.error === null,
  );

  return (
    <div className="flex flex-wrap items-baseline gap-x-4 gap-y-1 border border-border px-3 py-2 text-xs">
      {environments.map((environment) => {
        if (environment.summary !== null) {
          return (
            <span
              key={environment.environmentId}
              className="flex items-center gap-1 text-foreground"
            >
              <CheckIcon className="size-3 text-success-foreground" aria-hidden />
              {environment.label}
            </span>
          );
        }

        if (environment.error !== null) {
          return (
            <span
              key={environment.environmentId}
              className="flex items-center gap-1 text-destructive"
            >
              <XIcon className="size-3" aria-hidden />
              {environment.label}
            </span>
          );
        }

        return (
          <span
            key={environment.environmentId}
            className="animate-status-pulse text-muted-foreground"
          >
            {environment.label}…
          </span>
        );
      })}
      <span className="ms-auto text-muted-foreground">
        {scanning.length === 1
          ? "1 device still scanning"
          : `${scanning.length} devices still scanning`}
      </span>
    </div>
  );
}

/**
 * Stand-in with the loaded page's shape, using the shared `Skeleton` bars so it
 * remains static like other long-lived loading states.
 * Blocks fill in exactly once when the last device answers.
 */
export function UsageSkeleton() {
  return (
    <>
      <section className="grid gap-6 lg:grid-cols-18rem-1fr">
        <div className="flex flex-col gap-5">
          <div className="flex flex-col gap-1">
            <Skeleton className="h-10 w-36" />
            <Skeleton className="h-4 w-32" />
          </div>
          {PROVIDER_ORDER.map((provider) => (
            <div key={provider} className="flex flex-col gap-1">
              <div className="flex min-h-5 items-center justify-between gap-4">
                <span className="flex items-center gap-2">
                  <Skeleton className="size-2 shrink-0 rounded-full" />
                  <Skeleton className="size-4 shrink-0 rounded-full" />
                  <Skeleton className="h-3.5 w-20" />
                </span>
                <Skeleton className="h-3.5 w-14" />
              </div>
              <Skeleton className="h-4 w-36" />
            </div>
          ))}
        </div>

        <div className="flex flex-col gap-3">
          <Skeleton className="h-5 w-24" />
          <div className="flex flex-col gap-1">
            <Skeleton tone="faint" className="ml-16 h-56" />
            <Skeleton tone="faint" className="ml-16 h-4" />
          </div>
        </div>
      </section>

      <section className="flex flex-col gap-2">
        <h2 className="text-sm font-medium text-foreground">Totals</h2>
        <div className="grid grid-cols-2 gap-x-6 gap-y-4 py-1 md:grid-cols-5">
          {["Processed tokens", "Cached input", "Uncached input", "Output", "Cache savings"].map(
            (label) => (
              <div key={label} className="flex flex-col gap-0.5">
                <span className="text-xs text-muted-foreground">{label}</span>
                <Skeleton className="h-6 w-16" />
              </div>
            ),
          )}
        </div>
      </section>

      <section className="flex flex-col gap-3">
        <div className="flex items-center justify-between gap-3">
          <h2 className="text-sm font-medium text-foreground">Breakdown</h2>
          <Skeleton className="h-7 w-28 rounded-lg" />
        </div>
        <Skeleton tone="faint" className="h-44" />
      </section>
    </>
  );
}