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.0 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

"use client";

import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover";
import { copyToClipboard } from "@/lib/copy-to-clipboard";
import { Tick02Icon } from "@/lib/tick-icon";
import { cn } from "@/lib/utils";
import { Copy01Icon } from "@/lib/copy-icon";
import { HugeiconsIcon } from "@hugeicons/react";
import { useEffect, useRef, useState } from "react";

interface CopyableErrorChipProps {
  message: string;
  className?: string;
}

export function CopyableErrorChip({
  message,
  className,
}: CopyableErrorChipProps) {
  const [copied, setCopied] = useState(false);
  const resetTimer = useRef<ReturnType<typeof setTimeout> | null>(null);

  useEffect(() => () => {
    if (resetTimer.current) clearTimeout(resetTimer.current);
  }, []);

  const handleCopy = async () => {
    if (await copyToClipboard(message)) {
      setCopied(true);
      if (resetTimer.current) clearTimeout(resetTimer.current);
      resetTimer.current = setTimeout(() => {
        setCopied(false);
        resetTimer.current = null;
      }, 1800);
    }
  };

  return (
    <Popover>
      <PopoverTrigger asChild={true}>
        {/* No aria-label: the visible text names the button, so readers get the untruncated error. */}
        <button
          type="button"
          className={cn(
            "flex max-w-[calc(28rem*var(--ui-space-scale,1))] min-w-0 cursor-pointer items-center rounded-full text-left text-xs text-destructive transition-colors hover:bg-destructive/10 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring",
            className,
          )}
        >
          <span className="min-w-0 flex-1 truncate">{message}</span>
        </button>
      </PopoverTrigger>
      <PopoverContent
        align="start"
        side="bottom"
        className="w-[min(36rem,calc(100vw-1rem))] gap-2"
      >
        <div className="flex items-start justify-between gap-2">
          <span className="text-xs font-medium text-destructive">Error</span>
          <button
            type="button"
            onClick={handleCopy}
            aria-label={copied ? "Copied" : "Copy error message"}
            className={cn(
              "btn-fill inline-flex items-center gap-1 rounded-full px-2 py-1 text-ui-11 text-muted-foreground transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring",
              copied && "text-emerald-600 dark:text-emerald-500",
            )}
          >
            <HugeiconsIcon
              icon={copied ? Tick02Icon : Copy01Icon}
              className="size-3.5"
            />
            {copied ? "Copied" : "Copy"}
          </button>
        </div>
        <p className="max-h-64 overflow-y-auto select-text whitespace-pre-wrap break-words text-xs text-destructive">
          {message}
        </p>
      </PopoverContent>
    </Popover>
  );
}