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

import { useEffect, useState } from "react";
import { listLinkedFolders } from "../api/rag-api";
import type { RagDocument } from "../types/rag";

export function useLinkedFolderNames(
  projectId: string | null,
  folderIds: string,
): ReadonlyMap<string, string> {
  const [names, setNames] = useState<ReadonlyMap<string, string>>(new Map());
  useEffect(() => {
    if (!projectId || !folderIds) return;
    let cancelled = false;
    listLinkedFolders({ type: "project", id: projectId })
      .then((folders) => {
        if (cancelled) return;
        setNames(new Map(folders.map((f) => [f.id, f.displayName])));
      })
      .catch(() => {
      });
    return () => {
      cancelled = true;
    };
  }, [projectId, folderIds]);
  return names;
}

/** A linked folder can hold thousands of files, so it is drawn as one item. */
export function groupByLinkedFolder<T extends RagDocument>(
  docs: T[],
): {
  loose: T[];
  folders: [string, T[]][];
} {
  const loose: T[] = [];
  const folders = new Map<string, T[]>();
  for (const doc of docs) {
    const folderId = doc.linkedFolderId;
    if (!folderId) {
      loose.push(doc);
      continue;
    }
    const group = folders.get(folderId);
    if (group) group.push(doc);
    else folders.set(folderId, [doc]);
  }
  return { loose, folders: [...folders] };
}