Skip to content

armgit / clipy

Public Imported from GitHub
0

The AI-native screen recorder. Record your screen and talk; the people you share it with and your AI agents both get it.

5.9 KB
Raw Blame
import { useEffect, useLayoutEffect, useState } from "react";
import { useTranslations } from "use-intl";
import {
  declineModelAutoDownload,
  dismissModelAutoDownloadNotice,
  getModelAutoDownload,
  onModelAutoDownload,
  onTranscriptionModelProgress,
  type ModelAutoDownloadDto,
} from "../lib/bridge";
import { isWindows } from "../lib/platform";

function megabytes(bytes: number): string {
  return `${Math.round(bytes / 1_000_000)} MB`;
}

/** Which notice the status calls for, if any. */
export function modelNoticeKind(status: ModelAutoDownloadDto | null): "progress" | "finished" | null {
  if (!status?.supported || !status.model) return null;
  if (status.state === "finished") return "finished";
  if (status.hidden) return null;
  if (status.state === "downloading") return "progress";
  // A wait is shown only as a pause of a download that already started:
  // someone who never saw it start is not told about a download that has
  // not begun.
  if (status.state === "waiting" && status.started) return "progress";
  return null;
}

/**
 * The recorder's small notice for the default transcription model's
 * automatic download: progress with Cancel while it runs
 * or is paused, then once "Transcription now uses … on this Mac" (or PC). It sits
 * above the controls like the What's new card and moves nothing. Cancel is
 * remembered for good (Settings still offers Download); ✕ hides the progress
 * for this launch, or retires the "now uses" note. The parent renders it only
 * while idle, on macOS and Windows in the official app.
 */
export function ModelDownloadNotice({
  onError,
  onLayoutChange,
}: {
  onError: (message: string) => void;
  onLayoutChange?: () => void;
}) {
  const t = useTranslations("desktop.modelNotice");
  const [status, setStatus] = useState<ModelAutoDownloadDto | null>(null);
  const [fraction, setFraction] = useState<number | null>(null);

  useEffect(() => {
    let disposed = false;
    const offs: Array<() => void> = [];
    getModelAutoDownload()
      .then((s) => !disposed && setStatus(s))
      .catch(() => {});
    void onModelAutoDownload((s) => {
      if (disposed) return;
      setStatus(s);
      if (s.state !== "downloading") setFraction(null);
    })
      .then((off) => (disposed ? off() : offs.push(off)))
      .catch(() => {});
    void onTranscriptionModelProgress((event) => {
      if (!disposed && event.state === "downloading") setFraction(event.fraction);
    })
      .then((off) => (disposed ? off() : offs.push(off)))
      .catch(() => {});
    return () => {
      disposed = true;
      offs.forEach((off) => off());
    };
  }, []);

  const kind = modelNoticeKind(status);
  useLayoutEffect(() => {
    onLayoutChange?.();
  }, [kind, onLayoutChange]);

  if (!status?.model || !kind) return null;
  const model = status.model;

  const dismiss = () =>
    void dismissModelAutoDownloadNotice()
      .then(setStatus)
      .catch((err) => onError(String(err)));
  const cancel = () =>
    void declineModelAutoDownload(false)
      .then(setStatus)
      .catch((err) => onError(String(err)));

  const percent = Math.round((fraction ?? status.fraction) * 100);
  const paused =
    status.state === "waiting"
      ? status.reason === "busy"
        ? t("pausedBusy")
        : status.reason === "network"
          ? t("pausedNetwork")
          : status.reason === "disk"
            ? t("pausedDisk", { size: megabytes(model.sizeBytes * 3) })
            : t("retrying")
      : null;

  return (
    <section
      aria-label={t("eyebrow")}
      role="status"
      className="clipy-fade-in relative mx-3.5 mb-2.5 mt-1 rounded-2xl bg-row px-3.5 pb-3 pt-3 shadow-[inset_0_0_0_1px_rgba(10,13,22,0.08)]"
    >
      <button
        type="button"
        onClick={dismiss}
        aria-label={kind === "finished" ? t("dismiss") : t("hide")}
        title={kind === "finished" ? t("dismiss") : t("hide")}
        className="absolute right-2 top-2 flex h-6 w-6 items-center justify-center rounded-md text-ink-muted hover:bg-surface/70 hover:text-ink-text"
      >
        <svg width="12" height="12" viewBox="0 0 12 12" aria-hidden>
          <path d="M3 3l6 6M9 3L3 9" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" />
        </svg>
      </button>
      <p className="text-[10.5px] font-bold uppercase tracking-[0.12em] text-flash-600">{t("eyebrow")}</p>
      {kind === "finished" ? (
        <p className="mt-1 pr-6 text-[12.5px] leading-snug text-ink-text">{t(isWindows ? "finishedWindows" : "finished", { model: model.label })}</p>
      ) : (
        <>
          <p className="mt-0.5 pr-6 text-[13.5px] font-semibold tracking-[-0.01em] text-ink-text">
            {t("downloadingTitle", { model: model.label })}
          </p>
          <p className="mt-0.5 text-[12px] leading-snug text-ink-secondary">
            {paused ?? t(isWindows ? "downloadingBodyWindows" : "downloadingBody", { size: megabytes(model.sizeBytes) })}
          </p>
          <div className="mt-2 flex items-center gap-2.5">
            <div
              className="h-1.5 min-w-0 flex-1 overflow-hidden rounded-full bg-surface"
              role="progressbar"
              aria-valuemin={0}
              aria-valuemax={100}
              aria-valuenow={percent}
            >
              <div
                className={`h-full rounded-full ${paused ? "bg-ink-muted" : "bg-flash-500"} transition-[width] duration-300`}
                style={{ width: `${Math.max(2, percent)}%` }}
              />
            </div>
            <span className="w-9 shrink-0 text-right text-[11.5px] tabular-nums text-ink-secondary">
              {t("percent", { percent })}
            </span>
            <button
              type="button"
              onClick={cancel}
              className="shrink-0 rounded-md px-1.5 py-0.5 text-[12.5px] font-medium text-ink-secondary hover:text-ink-text"
            >
              {t("cancel")}
            </button>
          </div>
        </>
      )}
    </section>
  );
}