All files / src/apps/user/pages/knowledge/source documents.ts

69.23% Statements 18/26
46.15% Branches 6/13
88.88% Functions 8/9
72.72% Lines 16/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61                48x             16x 16x 16x 16x 16x           16x           16x       32x               2x 56x       2x 32x   2x 1x 1x              
import { type KnowledgeDocument } from "@/objects/Knowledge";
 
// ─── Folder helpers ───────────────────────────────────────────────────────────
 
export function docFolderPath(doc: {
  folder_path?: string;
  document_metadata?: { folder_path?: string };
}): string {
  return doc.folder_path ?? doc.document_metadata?.folder_path ?? "";
}
 
export function getSubfolders(
  docs: { document_metadata?: { folder_path?: string } }[],
  currentPath: string,
): string[] {
  const prefix = currentPath ? `${currentPath}/` : "";
  const seen = new Set<string>();
  for (const doc of docs) {
    const fp = docFolderPath(doc);
    Iif (fp.startsWith(prefix) && fp !== currentPath) {
      const rest = fp.slice(prefix.length);
      const segment = rest.split("/")[0];
      if (segment) seen.add(segment);
    }
  }
  return [...seen].sort();
}
 
export function getFilesAtPath<
  T extends { document_metadata?: { folder_path?: string } },
>(docs: T[], currentPath: string): T[] {
  return docs.filter((doc) => docFolderPath(doc) === currentPath);
}
 
export function buildBreadcrumbs(path: string): { label: string; path: string }[] {
  Eif (!path) return [];
  const parts = path.split("/");
  return parts.map((part, i) => ({
    label: part,
    path: parts.slice(0, i + 1).join("/"),
  }));
}
 
export const isSearchable = (doc: KnowledgeDocument) =>
  doc.document_metadata?.index_status === "indexed";
 
/** Still downloading from its link: its import indexes it, and a reindex
 * would download it twice (svc-core refuses one). */
export const isImporting = (doc: KnowledgeDocument) =>
  doc.document_metadata?.index_status === "importing";
 
export const hostOf = (url: string): string => {
  try {
    return new URL(url).hostname.replace(/^www\./, "");
  } catch {
    return url;
  }
};
 
/** svc-core's `MAX_IMPORTS_PER_SOURCE`: links one request may import. */