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.4 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 { authFetch } from "@/features/auth";
import { type RefObject, useEffect, useRef, useState } from "react";

export type SandboxImageState =
  | { status: "idle" }
  | { status: "loaded"; url: string; blob: Blob }
  | { status: "failed" };

const IDLE: SandboxImageState = { status: "idle" };

/**
 * Fetches one auth-protected sandbox file into an object URL.
 *
 * The route answers on the Authorization header (`_authenticate_header_or_query`), so a bare
 * `<img src>` hitting it straight gets a 401 and the renderer's "Image not available" placeholder.
 * Pass the URL from `sandboxFilePath()`/`markdownSandboxImageSrc()` and render what comes back.
 *
 * Any element can hold the ref (a `<video>` for a generated clip); it only marks where the file shows.
 *
 * Keyed by url: an element that moves to another file reads idle rather than showing the previous
 * file's blob, and a stale response can never write state for a url it was not fetched for.
 */
export function useSandboxImage<T extends Element = HTMLImageElement>(
  url: string | null,
): {
  ref: RefObject<T | null>;
  state: SandboxImageState;
} {
  const ref = useRef<T>(null);
  const [state, setState] = useState<{
    url: string | null;
    load: SandboxImageState;
    signal?: AbortSignal;
  }>({
    url,
    load: IDLE,
  });
  const [nearViewport, setNearViewport] = useState(
    () => typeof IntersectionObserver === "undefined",
  );

  useEffect(() => {
    if (nearViewport || !url) return;
    const element = ref.current;
    if (!element) return;
    const observer = new IntersectionObserver(
      (entries) => {
        if (entries.some((entry) => entry.isIntersecting)) {
          setNearViewport(true);
          observer.disconnect();
        }
      },
      { rootMargin: "200px" },
    );
    observer.observe(element);
    return () => observer.disconnect();
  }, [nearViewport, url]);

  useEffect(() => {
    if (!url || !nearViewport) return;
    const controller = new AbortController();
    let objectUrl: string | null = null;

    authFetch(url, { signal: controller.signal })
      .then(async (response) => {
        // Guarded on both arms: a late write for the url this element used to hold must not land.
        if (!response.ok) {
          if (!controller.signal.aborted)
            setState({
              url,
              load: { status: "failed" },
              signal: controller.signal,
            });
          return;
        }
        const blob = await response.blob();
        if (controller.signal.aborted) return;
        objectUrl = URL.createObjectURL(blob);
        setState({
          url,
          load: { status: "loaded", url: objectUrl, blob },
          signal: controller.signal,
        });
      })
      .catch(() => {
        if (!controller.signal.aborted)
          setState({
            url,
            load: { status: "failed" },
            signal: controller.signal,
          });
      });

    return () => {
      controller.abort();
      // Revoke even if aborted, or the bytes stay pinned for the session.
      if (objectUrl) URL.revokeObjectURL(objectUrl);
    };
  }, [url, nearViewport]);

  // Returning to the same URL must not reuse a result whose fetch was cleaned up.
  return {
    ref,
    state: state.url === url && !state.signal?.aborted ? state.load : IDLE,
  };
}