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.

4.9 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 {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import {
  Dialog,
  DialogContent,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { useT } from "@/i18n";
import { useEffect, useRef, useState } from "react";

interface NameDialogProps {
  open: boolean;
  title: string;
  submitLabel: string;
  initialValue: string;
  onSubmit: (name: string) => Promise<void>;
  onOpenChange: (open: boolean) => void;
}

export function NameDialog({ open, title, onOpenChange, ...form }: NameDialogProps) {
  const [busy, setBusy] = useState(false);
  return (
    <Dialog open={open} onOpenChange={(next) => !busy && onOpenChange(next)}>
      <DialogContent className="sm:max-w-md">
        <DialogHeader>
          <DialogTitle>{title}</DialogTitle>
        </DialogHeader>
        <NameForm {...form} busy={busy} setBusy={setBusy} onClose={() => onOpenChange(false)} />
      </DialogContent>
    </Dialog>
  );
}

function NameForm({
  submitLabel,
  initialValue,
  onSubmit,
  busy,
  setBusy,
  onClose,
}: Pick<NameDialogProps, "submitLabel" | "initialValue" | "onSubmit"> & {
  busy: boolean;
  setBusy: (busy: boolean) => void;
  onClose: () => void;
}) {
  const t = useT();
  const [value, setValue] = useState(initialValue);
  const input = useRef<HTMLInputElement>(null);

  useEffect(() => {
    const dot = initialValue.lastIndexOf(".");
    const frame = requestAnimationFrame(() =>
      input.current?.setSelectionRange(0, dot > 0 ? dot : initialValue.length),
    );
    return () => cancelAnimationFrame(frame);
  }, [initialValue]);

  const trimmed = value.trim();
  async function submit() {
    if (!trimmed || busy) return;
    setBusy(true);
    try {
      await onSubmit(trimmed);
      onClose();
    } catch {
      // The caller toasted; keep the dialog open so the name can be fixed.
    } finally {
      setBusy(false);
    }
  }

  return (
    <form
      onSubmit={(event) => {
        event.preventDefault();
        void submit();
      }}
    >
      <Input
        ref={input}
        autoFocus
        value={value}
        maxLength={255}
        onChange={(event) => setValue(event.target.value)}
        aria-label={t("library.list.name")}
      />
      <DialogFooter className="mt-5">
        <Button type="button" variant="ghost" onClick={onClose}>
          {t("common.cancel")}
        </Button>
        <Button type="submit" variant="dark" disabled={!trimmed || busy}>
          {submitLabel}
        </Button>
      </DialogFooter>
    </form>
  );
}

export function ConfirmDeleteDialog({
  open,
  title,
  description,
  confirmLabel,
  onConfirm,
  onOpenChange,
}: {
  open: boolean;
  title: string;
  description: string;
  confirmLabel: string;
  onConfirm: () => void;
  onOpenChange: (open: boolean) => void;
}) {
  const t = useT();
  return (
    <AlertDialog open={open} onOpenChange={onOpenChange}>
      <AlertDialogContent onOverlayClick={() => onOpenChange(false)}>
        <AlertDialogHeader>
          <AlertDialogTitle>{title}</AlertDialogTitle>
          <AlertDialogDescription>{description}</AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>{t("common.cancel")}</AlertDialogCancel>
          <AlertDialogAction variant="destructive" onClick={onConfirm}>
            {confirmLabel}
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}

export function UnsavedChangesDialog({
  error,
  onRetry,
  onDiscard,
  onKeepEditing,
}: {
  error: string | null;
  onRetry: () => void;
  onDiscard: () => void;
  onKeepEditing: () => void;
}) {
  const t = useT();
  const reason = error && !/[.!?。!?؟।]$/.test(error) ? `${error}.` : error;
  return (
    <AlertDialog open={error !== null} onOpenChange={(open) => !open && onKeepEditing()}>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>{t("library.dialog.unsavedTitle")}</AlertDialogTitle>
          <AlertDialogDescription>
            {t("library.dialog.unsavedDescription", { reason })}
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>{t("library.dialog.keepEditing")}</AlertDialogCancel>
          <AlertDialogAction variant="destructive" onClick={onDiscard}>
            {t("library.dialog.discardChanges")}
          </AlertDialogAction>
          <AlertDialogAction onClick={onRetry}>{t("library.empty.tryAgain")}</AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}