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.

1.7 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 { Component, type ReactNode, useEffect, useRef } from "react";

interface LazyImportBoundaryProps {
  fallback: ReactNode;
  children: ReactNode;
}

interface LazyImportBoundaryState {
  failed: boolean;
}

export class LazyImportBoundary extends Component<
  LazyImportBoundaryProps,
  LazyImportBoundaryState
> {
  state: LazyImportBoundaryState = { failed: false };

  static getDerivedStateFromError(): LazyImportBoundaryState {
    return { failed: true };
  }

  render() {
    return this.state.failed ? this.props.fallback : this.props.children;
  }
}

export function LazyImportFailure({
  message,
  reloadLabel,
  testId,
  className,
  dismissLabel,
  onDismiss,
}: {
  message: string;
  reloadLabel: string;
  testId: string;
  className: string;
  dismissLabel?: string;
  onDismiss?: () => void;
}) {
  const reloadRef = useRef<HTMLButtonElement>(null);
  useEffect(() => reloadRef.current?.focus(), []);
  return (
    <div role="alert" data-testid={testId} className={className}>
      <p className="text-sm">{message}</p>
      <div className="mt-2 flex gap-2">
        <button
          ref={reloadRef}
          type="button"
          onClick={() => window.location.reload()}
          className="btn-fill rounded-full px-3 py-2 font-medium text-xs "
        >
          {reloadLabel}
        </button>
        {dismissLabel && onDismiss ? (
          <button
            type="button"
            onClick={onDismiss}
            className="btn-fill rounded-full px-3 py-2 font-medium text-xs "
          >
            {dismissLabel}
          </button>
        ) : null}
      </div>
    </div>
  );
}