All files / src/apps/user/pages/knowledge/source ImportFromUrlForm.tsx

76.66% Statements 23/30
61.11% Branches 11/18
71.42% Functions 5/7
82.14% Lines 23/28

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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109        2x       2x             52x 52x 52x 52x 52x   52x   105x     52x 1x 1x       1x 1x 1x 1x         1x 1x 1x                   1x       52x                     50x                                                       1x                          
import { useApiHandler } from "@/services/useApiHandler";
import { Loader2 } from "lucide-react";
import React, { useState } from "react";
 
const MAX_IMPORT_LINKS = 10;
 
/** Links to import into a file source: one per line, downloaded and
 *  indexed in the background (svc-core `knowledge/imports.py`). */
const ImportFromUrlForm: React.FC<{
  knowledgeId: string;
  folderPath: string;
  /** `done`: nothing was skipped, so the form has nothing left to say. */
  onImported: (done: boolean) => void;
  onCancel: () => void;
}> = ({ knowledgeId, folderPath, onImported, onCancel }) => {
  const { getApiHandler } = useApiHandler();
  const [text, setText] = useState("");
  const [busy, setBusy] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [skipped, setSkipped] = useState<string[]>([]);
 
  const urls = text
    .split("\n")
    .map((line) => line.trim())
    .filter(Boolean);
 
  const submit = async () => {
    Iif (urls.length === 0) return;
    Iif (urls.length > MAX_IMPORT_LINKS) {
      setError(`Up to ${MAX_IMPORT_LINKS} links at a time.`);
      return;
    }
    setBusy(true);
    setError(null);
    try {
      const result = await getApiHandler().knowledge_router.importDocuments(
        knowledgeId,
        urls,
        folderPath || undefined,
      );
      setSkipped(result.skipped.map((s) => `${s.url}: ${s.reason}`));
      setText("");
      onImported(result.skipped.length === 0);
    } catch (e) {
      const detail = (e as { response?: { data?: { detail?: unknown } } })
        ?.response?.data?.detail;
      setError(
        typeof detail === "string"
          ? detail
          : "Could not import those links. Please try again.",
      );
    } finally {
      setBusy(false);
    }
  };
 
  return (
    <div className="mb-3 space-y-2 px-3 py-3 rounded-lg bg-gray-50 dark:bg-gray-700/50 border border-gray-200 dark:border-gray-600">
      <label
        htmlFor={`import-links-${knowledgeId}`}
        className="block text-xs font-medium text-gray-600 dark:text-gray-300"
      >
        Document links, one per line
      </label>
      <textarea
        id={`import-links-${knowledgeId}`}
        value={text}
        onChange={(e) => setText(e.target.value)}
        rows={3}
        placeholder="https://example.gov.uk/policy.pdf"
        className="w-full rounded-lg border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-800 px-3 py-2 text-xs text-gray-800 dark:text-gray-200 placeholder-gray-400 dark:placeholder-gray-500 outline-none"
      />
      <p className="text-[11px] text-gray-400 dark:text-gray-500">
        PDFs, Word, Excel, PowerPoint, CSV and text files, fetched from their
        publishers under their terms. Each shows as importing until it is
        indexed.
      </p>
      {error && (
        <p className="text-xs text-red-600 dark:text-red-400">{error}</p>
      )}
      {skipped.length > 0 && (
        <ul className="text-xs text-amber-700 dark:text-amber-300 space-y-0.5">
          {skipped.map((line) => (
            <li key={line}>Skipped {line}</li>
          ))}
        </ul>
      )}
      <div className="flex justify-end gap-2">
        <button
          onClick={onCancel}
          className="px-2.5 py-1.5 text-xs text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100"
        >
          {skipped.length > 0 ? "Close" : "Cancel"}
        </button>
        <button
          onClick={() => void submit()}
          disabled={busy || urls.length === 0}
          className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-lg text-xs font-medium bg-gray-900 dark:bg-white text-white dark:text-gray-900 hover:opacity-80 disabled:opacity-50"
        >
          {busy && <Loader2 className="size-3 animate-spin" />}
          Import
        </button>
      </div>
    </div>
  );
};
 
export default ImportFromUrlForm;