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.6 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 type { TranslationKey } from "@/i18n";
import { useEffect, useMemo, useState } from "react";
import { type LibraryDisk, type LibraryItem, type LibraryLocation, getLibrary } from "./api";
import { fileKind } from "./file-kind";
import type { LibrarySearch } from "./search";
import { includedBySettings, useLibrarySettingsStore } from "./settings-store";

type StorageCategory = "files" | "images" | "videos" | "audio" | "fineTunes";

export const STORAGE_LABELS: Record<StorageCategory | LibraryLocation["key"], TranslationKey> = {
  files: "settings.data.filesSection",
  uploads: "settings.library.locationUploads",
  images: "settings.library.categoryImages",
  videos: "settings.library.categoryVideos",
  audio: "library.tabs.audio",
  fineTunes: "settings.library.categoryFineTunes",
  exports: "settings.library.locationExports",
};

interface StorageUsage {
  category: StorageCategory;
  link: LibrarySearch;
  bytes: number;
  count: number;
}

interface LibraryStorage {
  status: "loading" | "ready" | "error";
  totalBytes: number;
  hiddenBytes: number;
  diskBytes: number;
  categories: StorageUsage[];
  disk: LibraryDisk | null;
}

const CATEGORY_LINKS: [StorageCategory, LibrarySearch][] = [
  ["files", { show: "all", filter: "files", sort: "size" }],
  ["images", { show: "images", sort: "size" }],
  ["videos", { show: "videos", sort: "size" }],
  ["audio", { show: "audio", sort: "size" }],
  ["fineTunes", { show: "models", sort: "size" }],
];

const KIND_CATEGORIES: Partial<Record<string, StorageCategory>> = {
  image: "images",
  video: "videos",
  audio: "audio",
  model: "fineTunes",
};

function diskSource(id: string): string {
  const [source, origin] = id.split(":", 2);
  return source === "model" ? `${source}:${origin}` : source!;
}

const STALE_EVENT = "unsloth:library-storage-stale";

export function refreshLibraryStorage(): void {
  window.dispatchEvent(new Event(STALE_EVENT));
}

export function useLibraryStorage(): LibraryStorage {
  const settings = useLibrarySettingsStore();
  const [snapshot, setSnapshot] = useState<{
    status: LibraryStorage["status"];
    items: LibraryItem[];
    disk: LibraryDisk | null;
    unlisted: Record<string, number>;
  }>({ status: "loading", items: [], disk: null, unlisted: {} });
  const [version, setVersion] = useState(0);
  useEffect(() => {
    const stale = () => setVersion((current) => current + 1);
    window.addEventListener(STALE_EVENT, stale);
    return () => window.removeEventListener(STALE_EVENT, stale);
  }, []);
  useEffect(() => {
    let cancelled = false;
    getLibrary().then(
      ({ items, disk, unlistedBytes }) =>
        !cancelled &&
        setSnapshot({ status: "ready", items, disk: disk ?? null, unlisted: unlistedBytes ?? {} }),
      () => !cancelled && setSnapshot((current) => ({ ...current, status: "error" })),
    );
    return () => {
      cancelled = true;
    };
  }, [version]);
  return useMemo(() => {
    const totals = new Map<StorageCategory, { bytes: number; count: number }>();
    const onDisk = snapshot.disk?.sources ? new Set(snapshot.disk.sources) : null;
    let diskBytes = 0;
    let hiddenBytes = 0;
    for (const item of snapshot.items) {
      const bytes = item.storageBytes ?? item.sizeBytes ?? 0;
      if (!onDisk || onDisk.has(diskSource(item.id))) {
        diskBytes += bytes;
      }
      if (!includedBySettings(item.id, settings)) {
        hiddenBytes += bytes;
        continue;
      }
      const category = KIND_CATEGORIES[fileKind(item)] ?? "files";
      const total = totals.get(category) ?? { bytes: 0, count: 0 };
      total.bytes += bytes;
      total.count += 1;
      totals.set(category, total);
    }
    for (const [source, bytes] of Object.entries(snapshot.unlisted)) {
      if (!onDisk || onDisk.has(source)) diskBytes += bytes;
      if (!includedBySettings(`${source}:`, settings)) {
        hiddenBytes += bytes;
        continue;
      }
      const category = KIND_CATEGORIES[source] ?? "files";
      const total = totals.get(category) ?? { bytes: 0, count: 0 };
      total.bytes += bytes;
      totals.set(category, total);
    }
    const categories = CATEGORY_LINKS.flatMap(([category, link]) => {
      const total = totals.get(category);
      return total ? [{ category, link, ...total }] : [];
    }).sort((a, b) => b.bytes - a.bytes);
    return {
      status: snapshot.status,
      totalBytes: categories.reduce((sum, entry) => sum + entry.bytes, hiddenBytes),
      hiddenBytes,
      diskBytes,
      categories,
      disk: snapshot.disk,
    };
  }, [snapshot, settings]);
}