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.

12.1 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 { Button } from "@/components/ui/button";
import { ReleaseNotesPanel } from "@/components/update/release-notes-panel";
import { useNotificationGate } from "@/hooks/use-notification-frequency";
import type {
  DesktopUpdatePolicyMode,
  RetainedUpdateFailure,
  UpdateInfo,
  UpdateStatus,
} from "@/hooks/use-tauri-update";
import type { CopySupportDiagnosticsResult } from "@/lib/tauri-diagnostics";
import { cn } from "@/lib/utils";
import { useT } from "@/i18n";
import { Alert02Icon, Download01Icon } from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
import { AnimatePresence, motion } from "motion/react";
import { useState } from "react";

interface UpdateBannerProps {
  status: UpdateStatus;
  info: UpdateInfo | null;
  dismissed: boolean;
  lastFailure: RetainedUpdateFailure | null;
  // Set while attached to a server this app did not start, which blocks the update.
  externalServerPort?: number | null;
  updatePolicyMode: DesktopUpdatePolicyMode;
  manualReleaseUrl: string | null;
  // Release page for this version, preferred over the generic changelog.
  releasePageUrl?: string | null;
  // false fills a shared overlay stack; true self-anchors.
  positioned?: boolean;
  onInstall: () => void;
  onDismiss: () => void;
  onCopyDiagnostics: () => Promise<CopySupportDiagnosticsResult>;
}

const EASE_OUT_QUART: [number, number, number, number] = [0.165, 0.84, 0.44, 1];
const LEADING_V = /^v/;

function formatVersion(version: string | null | undefined): string {
  if (!version) return "";
  return version.startsWith("v") ? version : `v${version}`;
}

export function UpdateBanner({
  status,
  info,
  dismissed,
  lastFailure,
  externalServerPort = null,
  updatePolicyMode,
  manualReleaseUrl,
  releasePageUrl = null,
  positioned = true,
  onInstall,
  onDismiss,
  onCopyDiagnostics,
}: UpdateBannerProps) {
  const t = useT();
  const [copying, setCopying] = useState(false);
  const [manualReport, setManualReport] = useState<string | null>(null);
  const [manualMessage, setManualMessage] = useState<string | null>(null);
  // Version whose notes are expanded; a new offer collapses the panel.
  const [notesVersion, setNotesVersion] = useState<string | null>(null);
  const showFailure = Boolean(lastFailure) && !dismissed;
  const showAvailable = status === "available" && !dismissed && !showFailure;
  // Failures always show; only the offer follows the notification frequency.
  const offerOpen = useNotificationGate(
    "unsloth",
    showAvailable && Boolean(info),
  );
  const show = showFailure || offerOpen;
  const isManualLinuxPackage = updatePolicyMode === "manual_linux_package";
  const isExternalServer = externalServerPort !== null;
  const installDisabled = isManualLinuxPackage
    ? manualReleaseUrl === null
    : isExternalServer;
  const currentVersion = formatVersion(info?.currentVersion);
  const latestVersion = formatVersion(info?.version);
  const icon = showFailure ? Alert02Icon : Download01Icon;
  const notesTargetVersion = info?.version?.replace(LEADING_V, "") ?? null;
  const notesOpen =
    notesTargetVersion !== null && notesVersion === notesTargetVersion;

  async function handleCopyDiagnostics() {
    setCopying(true);
    try {
      const result = await onCopyDiagnostics();
      if (result.ok) {
        setManualReport(null);
        setManualMessage(null);
      } else {
        setManualReport(result.report);
        setManualMessage(
          result.error ?? t("desktopStartup.diagnostics.clipboardFailed"),
        );
      }
    } catch (error) {
      setManualReport(null);
      setManualMessage(`Diagnostics copy failed: ${String(error)}`);
    } finally {
      setCopying(false);
    }
  }

  return (
    <AnimatePresence>
      {show && (
        <motion.div
          initial={{ opacity: 0, y: 12, scale: 0.96 }}
          animate={{ opacity: 1, y: 0, scale: 1 }}
          exit={{ opacity: 0, y: 8, scale: 0.97 }}
          transition={{ duration: 0.35, ease: EASE_OUT_QUART }}
          className={cn(
            positioned
              ? "fixed bottom-4 right-4 z-[9999] w-[calc(100vw-2rem)] max-w-[calc(448px*var(--ui-space-scale,1))]"
              : cn(
                  "pointer-events-auto flex w-[calc(100vw-2rem)] max-w-[calc(448px*var(--ui-space-scale,1))] shrink-0 flex-col",
                  // Only rendered notes may shrink in the capped rail.
                  "has-[[data-slot=update-release-notes]]:min-h-[calc(117px+93px*var(--ui-font-scale,1))] has-[[data-slot=update-release-notes]]:shrink max-[383px]:has-[[data-slot=update-release-notes]]:min-h-[calc(24px+224px*var(--ui-font-scale,1))]",
                ),
          )}
          data-testid="tauri-update-banner"
        >
          {/* Paint the full floor even when the notes are short. */}
          <div className="toast-surface relative flex max-h-[calc(100dvh_-_2rem_-_var(--studio-window-chrome-top,0px))] min-h-0 grow flex-col overflow-hidden rounded-2xl bg-white px-5 pb-4 pt-5 shadow-[0_2px_8px_-2px_rgba(0,0,0,0.16)] dark:bg-card dark:shadow-[0_8px_28px_-6px_var(--background)]">
            <button
              type="button"
              onClick={onDismiss}
              className="absolute top-2.5 right-3 flex size-6 items-center justify-center rounded-full text-muted-foreground/60 transition-colors hover:bg-muted hover:text-foreground"
              aria-label={t("desktopUpdate.banner.dismiss")}
            >
              <svg
                aria-hidden="true"
                width="12"
                height="12"
                viewBox="0 0 14 14"
                fill="none"
                xmlns="http://www.w3.org/2000/svg"
              >
                <path
                  d="M11 3L3 11M3 3l8 8"
                  stroke="currentColor"
                  strokeWidth="1.5"
                  strokeLinecap="round"
                />
              </svg>
            </button>

            <div className="flex min-w-0 shrink-0 items-start gap-4 pr-6">
              <HugeiconsIcon
                icon={icon}
                aria-hidden="true"
                className="mt-1 size-5 shrink-0 text-foreground"
                strokeWidth={1.75}
              />
              <div className="min-w-0">
                <p className="font-heading text-base font-medium text-foreground">
                  {showFailure ? t("desktopUpdate.banner.failedTitle") : t("desktopUpdate.banner.newVersion")}
                </p>
                {showFailure ? null : (
                  <p className="mt-0.5 text-xs text-muted-foreground">
                    {currentVersion} &rarr;{" "}
                    <span className="font-medium text-foreground">
                      {latestVersion}
                    </span>
                  </p>
                )}
                <p className="mt-1 text-ui-11 text-muted-foreground/70">
                  {showFailure
                    ? t("desktopUpdate.banner.backendRecovered")
                    : isManualLinuxPackage
                      ? t("desktopUpdate.banner.linuxManual")
                      : isExternalServer
                        ? t("settings.about.update.desktopExternalServer", {
                            port: externalServerPort,
                          })
                        : t("desktopUpdate.banner.available")}
                </p>
              </div>
            </div>

            {showFailure && lastFailure && (
              <p className="mt-3 line-clamp-2 shrink-0 text-xs text-destructive">
                {lastFailure.error}
              </p>
            )}

            {!showFailure && notesTargetVersion ? (
              <ReleaseNotesPanel
                version={notesTargetVersion}
                open={notesOpen}
                releaseNotesUrl={releasePageUrl ?? manualReleaseUrl}
              />
            ) : null}

            <div
              className={cn(
                "mt-4 flex shrink-0 flex-wrap items-center gap-x-1 gap-y-2",
                !showFailure && notesTargetVersion
                  ? "justify-between"
                  : "justify-end",
              )}
            >
              {!showFailure && notesTargetVersion ? (
                <Button
                  size="sm"
                  variant="ghost"
                  className="-ml-2 h-auto whitespace-nowrap rounded-full px-2.5 py-2 text-ui-13 font-medium text-foreground"
                  onClick={() =>
                    setNotesVersion(notesOpen ? null : notesTargetVersion)
                  }
                  aria-expanded={notesOpen}
                  data-testid="tauri-update-release-notes-toggle"
                >
                  {notesOpen ? t("desktopUpdate.banner.hideNotes") : t("desktopUpdate.banner.showNotes")}
                </Button>
              ) : null}
              {showFailure ? (
                <>
                  <Button
                    size="sm"
                    variant="ghost"
                    className="h-auto rounded-full px-3 py-2 text-ui-13 font-medium text-foreground"
                    onClick={() => {
                      handleCopyDiagnostics().catch(console.error);
                    }}
                  >
                    {copying ? t("desktopStartup.diagnostics.copying") : t("desktopUpdate.banner.copyDiagnostics")}
                  </Button>
                  <Button
                    size="sm"
                    variant="ghost"
                    className="h-auto rounded-full px-3 py-2 text-ui-13 font-medium text-foreground"
                    onClick={onDismiss}
                  >
                    {t("desktopUpdate.banner.later")}
                  </Button>
                  <Button
                    size="sm"
                    variant="primary"
                    className="-mr-1 rounded-full text-ui-13"
                    onClick={onInstall}
                    disabled={installDisabled}
                  >
                    {isManualLinuxPackage
                      ? t("desktopUpdate.banner.openReleasePage")
                      : t("desktopUpdate.banner.retry")}
                  </Button>
                </>
              ) : (
                <div className="flex flex-wrap items-center justify-end gap-x-1 gap-y-2">
                  <Button
                    size="sm"
                    variant="ghost"
                    className="h-auto whitespace-nowrap rounded-full px-2.5 py-2 text-ui-13 font-medium text-foreground"
                    onClick={onDismiss}
                  >
                    {t("desktopUpdate.banner.remindLater")}
                  </Button>
                  <Button
                    size="sm"
                    variant="primary"
                    className="-mr-1 h-auto whitespace-nowrap rounded-full px-3 py-2 text-ui-13"
                    onClick={onInstall}
                    disabled={installDisabled}
                    data-testid="tauri-update-install"
                  >
                    {isManualLinuxPackage ? t("desktopUpdate.banner.openReleasePage") : t("desktopUpdate.banner.update")}
                  </Button>
                </div>
              )}
            </div>
            {(manualMessage || manualReport) && (
              // The one region that may give up height: the card is capped and clips, and the rail cannot scroll to it.
              <div
                className="hover-scrollbar min-h-0 flex-1 overflow-y-auto overscroll-contain"
                data-testid="tauri-update-manual-report"
              >
                {manualMessage && (
                  <p className="mt-3 text-xs text-destructive">
                    {manualMessage}
                  </p>
                )}
                {manualReport && (
                  <textarea
                    readOnly={true}
                    value={manualReport}
                    onFocus={(event) => event.currentTarget.select()}
                    className="mt-2 h-28 w-full resize-none rounded-xl border border-border/50 bg-muted/30 p-2 font-mono text-ui-10 text-muted-foreground"
                  />
                )}
              </div>
            )}
          </div>
        </motion.div>
      )}
    </AnimatePresence>
  );
}