Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python
43.0 KB
Raw Blame
"use client";

import { useQueryClient } from "@tanstack/react-query";
import {
  type CheckboxSelectionCallbackParams,
  type ColDef,
  type ColumnState,
  type ValueFormatterParams,
  type ValueGetterParams,
} from "ag-grid-community";
import { AgGridReact, type CustomCellRendererProps } from "ag-grid-react";
import {
  AlertTriangle,
  ChevronRight,
  Cloud,
  FileIcon,
  Globe,
  RefreshCw,
} from "lucide-react";
import { useRouter, useSearchParams } from "next/navigation";
import {
  Suspense,
  useCallback,
  useEffect,
  useMemo,
  useRef,
  useState,
} from "react";
import {
  BOLD_MATCH_CLASS,
  HighlightedText,
  PREVIEW_TEXT_CLASS,
} from "@/components/highlighted-text";
import { KnowledgeDataTable } from "@/components/knowledge-data-table";
import { KnowledgeDropdown } from "@/components/knowledge-dropdown";
import { KnowledgeUrlIcon } from "@/components/knowledge-url-icon";
import { ProtectedRoute } from "@/components/protected-route";
import { Banner, BannerIcon, BannerTitle } from "@/components/ui/banner";
import { useOpenTaskMenu } from "@/contexts/console-status-context";
import { useKnowledgeFilter } from "@/contexts/knowledge-filter-context";
import { useTask } from "@/contexts/task-context";
import { trackButton } from "@/lib/analytics";
import { buildChunksUrl } from "@/lib/chunks-url";
import { isFileCancelled } from "@/lib/task-error-display";
import {
  EMPTY_SEARCH_RESULT,
  type File,
  type SearchResult,
  useGetSearchQuery,
} from "../api/queries/useGetSearchQuery";
import { useListFiles } from "../api/queries/useListFiles";
import "@/components/AgGrid/registerAgGridModules";
import "@/components/AgGrid/agGridStyles.css";
import { toast } from "sonner";
import { CancelIngestionButton } from "@/components/cancel-ingestion-button";
import { KnowledgeActionsDropdown } from "@/components/knowledge-actions-dropdown";
import { KnowledgeBatchActionsBar } from "@/components/knowledge-batch-actions-bar";
import { KnowledgePaginationFooter } from "@/components/knowledge-pagination-footer";
import { KnowledgeSearchBar } from "@/components/knowledge-search-bar";
import { KnowledgeSearchInput } from "@/components/knowledge-search-input";
import { StatusBadge } from "@/components/ui/status-badge";
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from "@/components/ui/tooltip";
import { WebsitePagesView } from "@/components/website-pages-view";
import { useIsCloudBrand } from "@/contexts/brand-context";
import { getConnectorDescriptor } from "@/lib/connectors/registry";
import { formatFileSize } from "@/lib/file-format";
import { buildSearchPayloadFilters } from "@/lib/filter-normalization";
import {
  buildKnowledgeTableRows,
  getKnowledgeFileIdentity,
} from "@/lib/knowledge-table-state";
import {
  computeTopPreviews,
  type SearchPreview,
  stripMarks,
} from "@/lib/search-preview";
import { parseTimestampMs } from "@/lib/time-utils";
import { cn } from "@/lib/utils";
import {
  DeleteConfirmationDialog,
  formatFilesToDelete,
} from "../../components/delete-confirmation-dialog";
import { useDeleteDocument } from "../api/mutations/useDeleteDocument";
import { useDeleteWebsiteSourcePageMutation } from "../api/mutations/useWebsiteSourceMutation";

function sameFileSelection(a: File[], b: File[]): boolean {
  if (a.length !== b.length) {
    return false;
  }
  const identities = new Set(b.map((row) => getKnowledgeFileIdentity(row)));
  return a.every((row) => identities.has(getKnowledgeFileIdentity(row)));
}

/** Failed overlays can stay selected after they lose their checkbox (processing → failed). */
function syncGridSelectionToDeletableRows(
  api: NonNullable<AgGridReact<File>["api"]>,
  isDeletable: (file?: File) => boolean,
): File[] {
  api.forEachNode((node) => {
    if (node.isSelected() && !isDeletable(node.data)) {
      node.setSelected(false);
    }
  });
  return api.getSelectedRows().filter(isDeletable);
}

/** Deselect non-deletable rows in the grid only; returns whether anything changed. */
function pruneNonDeletableGridSelection(
  api: NonNullable<AgGridReact<File>["api"]>,
  isDeletable: (file?: File) => boolean,
): boolean {
  let pruned = false;
  api.forEachNode((node) => {
    if (node.isSelected() && !isDeletable(node.data)) {
      node.setSelected(false);
      pruned = true;
    }
  });
  return pruned;
}

// Function to get the appropriate icon for a connector type
export function getSourceIcon(connectorType?: string) {
  if (connectorType === "openrag_docs" || connectorType === "url") {
    return <KnowledgeUrlIcon className="h-4 w-4 text-muted-foreground" />;
  }
  if (connectorType) {
    const Icon = getConnectorDescriptor(connectorType)?.Icon;
    if (Icon) return <Icon className="h-4 w-4 text-foreground shrink-0" />;
  }
  switch (connectorType) {
    case "s3":
      return <Cloud className="h-4 w-4 text-foreground shrink-0" />;
    default:
      return <FileIcon className="h-4 w-4 text-muted-foreground shrink-0" />;
  }
}

const AG_FIELD_TO_SORT_BY: Record<string, string> = {
  filename: "filename",
  size: "file_size",
  mimetype: "mimetype",
  owner: "owner",
  chunkCount: "chunk_count",
  indexed_time: "indexed_time",
  status: "status",
};

function listFilesFilterValues(values?: string[]) {
  const filtered = values?.filter((value) => value !== "*");
  return filtered && filtered.length > 0 ? filtered : undefined;
}

function buildFilterPageResetKey(
  parsedFilterData: ReturnType<typeof useKnowledgeFilter>["parsedFilterData"],
) {
  if (!parsedFilterData) {
    return "";
  }

  return JSON.stringify({
    query: parsedFilterData.query,
    connector_types: parsedFilterData.filters.connector_types,
    document_types: parsedFilterData.filters.document_types,
    owners: parsedFilterData.filters.owners,
    data_sources: parsedFilterData.filters.data_sources,
  });
}

export function getStatusSortRank(status?: File["status"]): number {
  switch (status) {
    case "active":
      return 0;
    case "processing":
      return 1;
    case "sync":
      return 2;
    case "failed":
      return 3;
    case "skipped":
      return 4;
    case "cancelled":
      return 5;
    case "unavailable":
      return 6;
    case "hidden":
      return 7;
    default:
      return 0;
  }
}

export function getSkippedWarningText(warning?: string): string {
  return warning ?? "Duplicate content — already exists in the knowledge base.";
}

/** Returns true only for skipped+duplicate_content rows; other skip reasons use the normal StatusBadge. */
export function isSkippedStatus(
  rawStatus?: string,
  skipReason?: string,
): boolean {
  return rawStatus === "skipped" && skipReason === "duplicate_content";
}

export function SkippedStatusCell({ warning }: { warning?: string }) {
  const warningText = getSkippedWarningText(warning);
  return (
    <Tooltip>
      <TooltipTrigger asChild>
        <span className="inline-flex items-center gap-1 text-sm text-amber-600 dark:text-amber-400 cursor-default">
          Duplicate
        </span>
      </TooltipTrigger>
      <TooltipContent
        side="top"
        align="end"
        className="max-w-80 whitespace-pre-wrap wrap-break-word"
      >
        {warningText}
      </TooltipContent>
    </Tooltip>
  );
}

export function getOwnerLabel(file?: File): string {
  return file?.owner_name?.trim() || file?.owner_email?.trim() || "—";
}

export function formatAvgScoreLabel(value: unknown): string {
  return typeof value === "number" ? value.toFixed(2) : "-";
}

export function formatChunkCountLabel(chunkCount?: number): string {
  return chunkCount?.toString() ?? "-";
}

/** Promotes "failed" to "cancelled" when the file's task was cancelled. */
export function resolveDisplayStatus(
  rawStatus: string,
  isCancelled: boolean,
): string {
  return rawStatus === "failed" && isCancelled ? "cancelled" : rawStatus;
}

export function formatSizeLabel(value: unknown): string {
  return value ? formatFileSize(value as number) : "-";
}

/** Always returns 0 — passed to ag-grid columns whose ordering is server-side. */
export function serverSideComparator(): number {
  return 0;
}

export function compareStatusRank(
  valueA?: File["status"],
  valueB?: File["status"],
): number {
  return getStatusSortRank(valueA) - getStatusSortRank(valueB);
}

export function compareAvgScore(
  valueA: number | undefined,
  valueB: number | undefined,
): number {
  return (valueA || 0) - (valueB || 0);
}

export function getFileStatus(status?: File["status"]): string {
  return status || "active";
}

// Lives in lib/ so components can build the link without importing this page.
export { buildChunksUrl };

export function buildWebsitePagesUrl(
  sourceId: string,
  effectiveSearchText: string,
): string {
  const params = new URLSearchParams({ website: sourceId });
  const trimmed = effectiveSearchText.trim();
  if (trimmed && trimmed !== "*") {
    params.set("q", trimmed);
  }
  return `/knowledge?${params.toString()}`;
}

const AVG_SCORE_TOOLTIP =
  "Average relevance score across the matched chunks of this file. Higher means a stronger match — sort by this column to rank results by relevance.";

export function AvgScoreCellContent({ value }: { value: unknown }) {
  const label = formatAvgScoreLabel(value);
  return (
    <Tooltip>
      <TooltipTrigger asChild>
        <span className="text-xs text-accent-emerald-foreground bg-accent-emerald px-2 py-1 rounded cursor-default">
          {label}
        </span>
      </TooltipTrigger>
      <TooltipContent side="top" className="max-w-56 text-center">
        {AVG_SCORE_TOOLTIP}
      </TooltipContent>
    </Tooltip>
  );
}

export function StatusCellContent({
  data,
  isCloudBrand,
  isOpenragDocsRow,
  hasOpenragRefreshCue,
  selectTask,
  getTaskIdForRow,
  openTaskMenu,
  setRecentTasksExpanded,
}: {
  data?: File;
  isCloudBrand: boolean;
  isOpenragDocsRow: (file?: File) => boolean;
  hasOpenragRefreshCue: boolean;
  selectTask: (taskId: string | null) => void;
  getTaskIdForRow: (data?: File) => string | null;
  openTaskMenu: () => void;
  setRecentTasksExpanded: (v: boolean) => void;
}) {
  const rawStatus = data?.status || "active";
  const status = resolveDisplayStatus(
    rawStatus,
    data != null && isFileCancelled(data),
  );
  const showOpenragRefreshCue = isOpenragDocsRow(data) && hasOpenragRefreshCue;

  if (showOpenragRefreshCue) {
    if (isCloudBrand) {
      return (
        <div className="inline-flex items-center gap-2 text-primary">
          <RefreshCw className="h-4 w-4 animate-spin" />
          <span className="text-sm font-medium">Refreshing</span>
        </div>
      );
    }
    return (
      <div className="inline-flex items-center justify-center h-5 w-5">
        <RefreshCw
          className="h-4 w-4 text-primary animate-spin"
          aria-label="OpenRAG doc is refreshing"
        />
      </div>
    );
  }

  if (status === "failed") {
    const button = (
      <button
        type="button"
        className={cn(
          "inline-flex items-center h-full transition",
          isCloudBrand
            ? "text-destructive hover:opacity-80"
            : "w-full text-red-500 hover:text-red-400",
        )}
        aria-label={
          data?.error
            ? `View ingestion error: ${data.error}`
            : "View ingestion error"
        }
        data-testid="failed-status-cell-trigger"
        onClick={() => {
          selectTask(getTaskIdForRow(data));
          openTaskMenu();
          setRecentTasksExpanded(true);
        }}
      >
        <StatusBadge status={status} className="pointer-events-none" />
      </button>
    );

    if (!data?.error) {
      return button;
    }

    return (
      <Tooltip>
        <TooltipTrigger asChild>{button}</TooltipTrigger>
        <TooltipContent
          side="top"
          align="end"
          className="max-w-80 whitespace-pre-wrap wrap-break-word"
        >
          {data.error}
        </TooltipContent>
      </Tooltip>
    );
  }

  if (status === "cancelled") {
    return <StatusBadge status="cancelled" />;
  }

  if (isSkippedStatus(rawStatus, data?.skip_reason)) {
    return <SkippedStatusCell warning={data?.warning} />;
  }

  return <StatusBadge status={status} />;
}

export function resolveActionsVariant(
  status: string,
  taskId: string | null | undefined,
  filePath: string,
): "cancel" | "dropdown" | null {
  if (status === "processing") {
    if (!taskId || !filePath) return null;
    return "cancel";
  }
  if (status !== "active") return null;
  return "dropdown";
}

function SearchPage() {
  const isCloudBrand = useIsCloudBrand();
  const [isNarrow, setIsNarrow] = useState(false);
  useEffect(() => {
    const mq = window.matchMedia("(max-width: 640px)");
    setIsNarrow(mq.matches);
    const handler = (e: MediaQueryListEvent) => setIsNarrow(e.matches);
    mq.addEventListener("change", handler);
    return () => mq.removeEventListener("change", handler);
  }, []);
  const queryClient = useQueryClient();
  const router = useRouter();
  const {
    files: taskFiles,
    tasks,
    refreshTasks,
    setRecentTasksExpanded,
    selectTask,
    cancelFile,
  } = useTask();
  const openTaskMenu = useOpenTaskMenu();
  const {
    parsedFilterData,
    queryOverride,
    selectedFilter,
    setSelectedSources,
    dateRange,
    effectiveDateRange,
  } = useKnowledgeFilter();
  const [selectedRows, setSelectedRows] = useState<File[]>([]);

  const [sortBy, setSortBy] = useState<string>("filename");
  const [sortOrder, setSortOrder] = useState<"asc" | "desc">("asc");

  useEffect(() => {
    setSelectedSources(
      selectedRows.flatMap((row) => (row.filename ? [row.filename] : [])),
    );
    return () => setSelectedSources([]);
  }, [selectedRows, setSelectedSources]);
  const [showBulkDeleteDialog, setShowBulkDeleteDialog] = useState(false);
  const lastErrorRef = useRef<string | null>(null);
  const hasInitializedFailedFilesRef = useRef(false);
  const seenFailedFileKeysRef = useRef<Set<string>>(new Set());

  const deleteDocumentMutation = useDeleteDocument();
  const deleteWebsiteSourceMutation = useDeleteWebsiteSourcePageMutation();
  const [currentPage, setCurrentPage] = useState(1);
  const [currentPageSize, setCurrentPageSize] = useState(25);

  const cursorCacheRef = useRef<Map<number, Record<string, unknown>>>(
    null as any,
  );
  if (!cursorCacheRef.current) {
    cursorCacheRef.current = new Map();
  }

  useEffect(() => {
    refreshTasks();
  }, [refreshTasks]);

  const getFailedFileKey = useCallback(
    (file: (typeof taskFiles)[number]) =>
      `${file.task_id}:${file.source_url || file.filename}`,
    [],
  );

  const getTaskIdForRow = useCallback(
    (file?: File): string | null => {
      if (!file) return null;
      const sourceUrl = file.source_url || "";
      const filename = file.filename || "";

      // Prioritize exact source URL match to avoid confusion with duplicate filenames
      const matches = taskFiles.filter((taskFile) => {
        if (sourceUrl) {
          // If row has source URL, match only by source URL
          return taskFile.source_url === sourceUrl;
        }
        // Fall back to filename matching only when no source URL
        return taskFile.filename === filename;
      });

      if (matches.length === 0) return null;

      const failedMatches =
        file.status === "failed"
          ? matches.filter((taskFile) => taskFile.status === "failed")
          : matches;
      const candidates = failedMatches.length > 0 ? failedMatches : matches;

      const taskTimestampMsById = new Map(
        tasks.map((task) => [
          task.task_id,
          parseTimestampMs(task.updated_at) ??
            parseTimestampMs(task.created_at) ??
            0,
        ]),
      );

      const mostRecent = candidates.reduce(
        (best, cur) => {
          const curMs =
            taskTimestampMsById.get(cur.task_id) ??
            parseTimestampMs(cur.updated_at) ??
            parseTimestampMs(cur.created_at) ??
            0;
          if (!best) return cur;
          const bestMs =
            taskTimestampMsById.get(best.task_id) ??
            parseTimestampMs(best.updated_at) ??
            parseTimestampMs(best.created_at) ??
            0;
          return curMs > bestMs ? cur : best;
        },
        undefined as (typeof candidates)[0] | undefined,
      );

      return mostRecent?.task_id || null;
    },
    [taskFiles, tasks],
  );

  useEffect(() => {
    const failedFiles = taskFiles.filter((file) => file.status === "failed");
    const seenKeys = seenFailedFileKeysRef.current;

    if (!hasInitializedFailedFilesRef.current) {
      failedFiles.forEach((file) => {
        seenKeys.add(getFailedFileKey(file));
      });
      hasInitializedFailedFilesRef.current = true;
      return;
    }

    let firstNewFailureTaskId: string | null = null;
    const hasNewFailure = failedFiles.some((file) => {
      const key = getFailedFileKey(file);
      if (seenKeys.has(key)) {
        return false;
      }
      seenKeys.add(key);
      if (!firstNewFailureTaskId) {
        firstNewFailureTaskId = file.task_id;
      }
      return true;
    });

    if (hasNewFailure) {
      if (firstNewFailureTaskId) {
        selectTask(firstNewFailureTaskId);
      }
      openTaskMenu();
      setRecentTasksExpanded(true);
    }
  }, [
    taskFiles,
    openTaskMenu,
    setRecentTasksExpanded,
    selectTask,
    getFailedFileKey,
  ]);

  const effectiveSearchText =
    queryOverride.trim() || parsedFilterData?.query?.trim() || "";
  const hasActiveFilters = parsedFilterData?.filters
    ? buildSearchPayloadFilters(parsedFilterData.filters, {
        includeDates: false,
      }) !== undefined
    : false;
  const isWildcardQuery =
    (effectiveSearchText === "" || effectiveSearchText === "*") &&
    !hasActiveFilters;
  const filterPageResetKey = buildFilterPageResetKey(parsedFilterData);

  // Retrieval uses the effective UTC bounds as-is; `dateRange` is display-only.
  const createdAfter = effectiveDateRange.createdAfter;
  const createdBefore = effectiveDateRange.createdBefore;

  const {
    data: listFilesData,
    isLoading: isListFilesLoading,
    isFetching: isListFilesFetching,
    error: listFilesError,
    isError: isListFilesError,
  } = useListFiles(
    {
      page: currentPage,
      pageSize: currentPageSize,
      sortBy,
      sortOrder,
      afterKey: cursorCacheRef.current.get(currentPage) ?? null,
      connectorType: listFilesFilterValues(
        parsedFilterData?.filters?.connector_types,
      ),
      mimetype: listFilesFilterValues(
        parsedFilterData?.filters?.document_types,
      ),
      owner: listFilesFilterValues(parsedFilterData?.filters?.owners),
      dataSources: listFilesFilterValues(
        parsedFilterData?.filters?.data_sources,
      ),
      createdAfter,
      createdBefore,
    },
    {
      refetchInterval: 5000,
      enabled: isWildcardQuery,
    },
  );

  const {
    data: searchData = EMPTY_SEARCH_RESULT,
    isLoading: isSearchLoading,
    error: searchError,
    isError: isSearchError,
  } = useGetSearchQuery(
    queryOverride,
    parsedFilterData,
    { enabled: !isWildcardQuery },
    { resultMode: "knowledge_sources" },
    { createdAfter, createdBefore },
  );

  const { files: searchFiles, warnings: searchWarnings } =
    searchData as SearchResult;

  const isLoading = isWildcardQuery ? isListFilesLoading : isSearchLoading;

  const isFetching = isWildcardQuery ? isListFilesFetching : isSearchLoading;
  const error = isWildcardQuery ? listFilesError : searchError;
  const isError = isWildcardQuery ? isListFilesError : isSearchError;

  // One-line "why it matched" for the most relevant files across the whole
  // result set (not just the visible page).
  const topPreviews = useMemo(
    () =>
      isWildcardQuery
        ? new Map<string, SearchPreview>()
        : computeTopPreviews(searchFiles, effectiveSearchText),
    [isWildcardQuery, searchFiles, effectiveSearchText],
  );

  const effectiveData: File[] = isWildcardQuery
    ? (listFilesData?.files ?? [])
    : searchFiles.slice(
        (currentPage - 1) * currentPageSize,
        currentPage * currentPageSize,
      );

  const isOpenragDocsRow = useCallback((file?: File) => {
    return (
      file?.connector_type === "openrag_docs" ||
      file?.connector_type === "system_default"
    );
  }, []);

  const getFileIdentity = useCallback((file?: File) => {
    return getKnowledgeFileIdentity(file);
  }, []);

  const isDeletableKnowledgeRow = useCallback((file?: File) => {
    return (file?.status || "active") === "active";
  }, []);

  const resolveDeleteFilename = useCallback(
    (row: File) => {
      const identity = getKnowledgeFileIdentity(row);
      const indexed = effectiveData.find(
        (file) => getKnowledgeFileIdentity(file) === identity,
      );
      return indexed?.filename ?? row.filename;
    },
    [effectiveData],
  );

  const hasOpenragRefreshCueFromTasks = tasks.some((task) => {
    const isTaskActive =
      task.status === "pending" ||
      task.status === "running" ||
      task.status === "processing";
    if (!isTaskActive || !task.files) {
      return false;
    }

    return Object.entries(task.files).some(([fileKey, fileInfo]) => {
      const filename = (fileInfo as { filename?: string })?.filename ?? "";
      return (
        filename === "OpenRAG docs refresh" || fileKey.includes("openr.ag")
      );
    });
  });
  const hasOpenragRefreshCue = hasOpenragRefreshCueFromTasks;

  // Show toast notification for search errors
  useEffect(() => {
    if (isError && error) {
      const errorMessage =
        error instanceof Error ? error.message : "Search failed";
      // Avoid showing duplicate toasts for the same error
      if (lastErrorRef.current !== errorMessage) {
        lastErrorRef.current = errorMessage;
        toast.error("Search error", {
          description: errorMessage,
          duration: 5000,
        });
      }
    } else if (!isError) {
      // Reset when query succeeds
      lastErrorRef.current = null;
    }
  }, [isError, error]);
  const fileResults = buildKnowledgeTableRows(
    effectiveData,
    taskFiles,
    Boolean(selectedFilter),
    dateRange,
    { createdAfter, createdBefore },
  );

  const serverTotal = isWildcardQuery
    ? (listFilesData?.total ?? 0)
    : searchFiles.length;
  const gridRows: File[] = fileResults;
  const totalPages = Math.max(1, Math.ceil(serverTotal / currentPageSize));

  useEffect(() => {
    cursorCacheRef.current = new Map();
    setCurrentPage(1);
  }, [effectiveSearchText, filterPageResetKey, createdAfter, createdBefore]);

  // when the server responds with an after_key for page N, cache it as the cursor for page N+1
  useEffect(() => {
    if (listFilesData?.after_key && listFilesData.page) {
      const nextPage = listFilesData.page + 1;
      cursorCacheRef.current.set(nextPage, listFilesData.after_key);
    }
  }, [listFilesData]);
  const gridRef = useRef<AgGridReact>(null);
  const gridReadyRef = useRef(false);

  const handleGridReady = useCallback(() => {
    gridReadyRef.current = true;
  }, []);

  const handleGridPreDestroyed = useCallback(() => {
    gridReadyRef.current = false;
  }, []);

  const getGridApi = useCallback(() => {
    if (!gridReadyRef.current) return null;
    return gridRef.current?.api ?? null;
  }, []);

  const onSortChanged = useCallback(() => {
    const api = getGridApi();
    if (!api) return;

    const sortedCol: ColumnState | undefined = api
      .getColumnState()
      .find((col) => col.sort != null);

    const newSortBy = sortedCol
      ? (AG_FIELD_TO_SORT_BY[sortedCol.colId] ?? sortedCol.colId)
      : "filename";
    const newSortOrder: "asc" | "desc" =
      sortedCol?.sort === "desc" ? "desc" : "asc";

    // Changing sort invalidates all cursors; reset to page 1
    cursorCacheRef.current = new Map();
    setCurrentPage(1);
    setSortBy(newSortBy);
    setSortOrder(newSortOrder);
  }, [getGridApi]);

  const gridRowsSelectionKey = useMemo(
    () =>
      gridRows
        .map(
          (row) => `${getKnowledgeFileIdentity(row)}:${row.status ?? "active"}`,
        )
        .join("\0"),
    [gridRows],
  );

  useEffect(() => {
    const api = getGridApi();
    if (!api) {
      return;
    }
    pruneNonDeletableGridSelection(api, isDeletableKnowledgeRow);
    const nextSelected = api.getSelectedRows().filter(isDeletableKnowledgeRow);
    setSelectedRows((current) =>
      sameFileSelection(current, nextSelected) ? current : nextSelected,
    );
    api.refreshCells({ force: true });
  }, [gridRowsSelectionKey, isDeletableKnowledgeRow, getGridApi]);

  // ── Shared column fragments ──────────────────────────────────────────────
  const colSource: ColDef<File> = useMemo(
    () => ({
      field: "filename",
      headerName: "Source",
      sortable: true,
      comparator: serverSideComparator,
      valueGetter: (params: ValueGetterParams<File>) =>
        `${params.data?.filename ?? ""}::${params.data?.status ?? "active"}`,
      valueFormatter: (params: ValueFormatterParams<File>) =>
        params.data?.filename ??
        (typeof params.value === "string" ? params.value.split("::")[0] : "-"),
      checkboxSelection: (params: CheckboxSelectionCallbackParams<File>) =>
        isDeletableKnowledgeRow(params?.data),
      headerCheckboxSelection: true,
      ...(isCloudBrand
        ? { flex: 2.2, minWidth: 260 }
        : { initialFlex: 2, minWidth: 220 }),
      cellRenderer: ({ data, value }: CustomCellRendererProps<File>) => {
        const filename =
          data?.filename ??
          (typeof value === "string" ? value.split("::")[0] : "");
        const status = data?.status || "active";
        const isActive = status === "active";
        const isParentWebsiteSource =
          data?.connector_type === "url" &&
          Boolean(data.web_source_id) &&
          !data.web_page_id;
        const showOpenragSourceAnimation =
          isOpenragDocsRow(data) && hasOpenragRefreshCue;
        const preview = topPreviews.get(getKnowledgeFileIdentity(data));
        return (
          <div className="flex items-center overflow-hidden w-full min-w-0 h-full">
            <div
              className={`transition-opacity duration-200 ${
                isActive ? "w-0" : "w-7"
              }`}
            ></div>
            <button
              type="button"
              className={cn(
                "flex gap-2 text-left flex-1 overflow-hidden transition-colors",
                preview ? "items-start" : "items-center",
                isActive
                  ? isCloudBrand
                    ? "cursor-pointer hover:text-primary"
                    : "cursor-pointer hover:text-blue-600"
                  : "cursor-default",
              )}
              onClick={() => {
                if (!isActive) return;
                if (data?.connector_type === "url" && data.web_source_id) {
                  router.push(
                    buildWebsitePagesUrl(
                      data.web_source_id,
                      effectiveSearchText,
                    ),
                  );
                  return;
                }
                router.push(buildChunksUrl(filename, effectiveSearchText));
              }}
            >
              {getSourceIcon(data?.connector_type)}
              <span className="flex min-w-0 flex-1 flex-col">
                <Tooltip>
                  <TooltipTrigger asChild>
                    <span
                      className={cn(
                        "font-medium truncate min-w-0",
                        showOpenragSourceAnimation
                          ? "text-primary animate-pulse"
                          : "text-foreground",
                      )}
                    >
                      {filename}
                    </span>
                  </TooltipTrigger>
                  <TooltipContent side="top" align="start">
                    {filename}
                  </TooltipContent>
                </Tooltip>
                {preview && (
                  <span
                    className={cn(
                      "block truncate text-xs font-normal",
                      PREVIEW_TEXT_CLASS,
                    )}
                    title={stripMarks(preview.text)}
                  >
                    {preview.kind === "keyword" ? (
                      <HighlightedText
                        highlights={[preview.text]}
                        fallbackText=""
                        markClassName={BOLD_MATCH_CLASS}
                      />
                    ) : (
                      preview.text
                    )}
                  </span>
                )}
              </span>
              {isParentWebsiteSource && (
                <ChevronRight
                  aria-hidden="true"
                  className="ml-auto size-5 shrink-0 text-link-primary"
                />
              )}
            </button>
          </div>
        );
      },
    }),
    [
      isCloudBrand,
      isDeletableKnowledgeRow,
      isOpenragDocsRow,
      hasOpenragRefreshCue,
      effectiveSearchText,
      router,
      topPreviews,
    ],
  );

  // Rows that carry a preview line need extra height. The cloud grid already
  // uses a 64px row, which fits two lines.
  const getRowHeight = useCallback(
    (params: { data?: File }) =>
      topPreviews.has(getKnowledgeFileIdentity(params.data)) ? 56 : undefined,
    [topPreviews],
  );

  // biome-ignore lint/correctness/useExhaustiveDependencies: topPreviews changes which rows are tall, so heights must be recomputed.
  useEffect(() => {
    getGridApi()?.resetRowHeights();
  }, [topPreviews, getGridApi]);

  const colSize: ColDef<File> = {
    field: "size",
    headerName: "Size",
    ...(isCloudBrand ? { flex: 1, minWidth: 110 } : {}),
    sortable: true,
    comparator: serverSideComparator,
    valueFormatter: (params: ValueFormatterParams<File>) =>
      formatSizeLabel(params.value),
    cellClass: isCloudBrand ? "text-muted-foreground" : undefined,
  };

  const colType: ColDef<File> = {
    field: "mimetype",
    headerName: "Type",
    ...(isCloudBrand ? { flex: 1, minWidth: 110 } : {}),
    cellClass: isCloudBrand ? "text-muted-foreground" : undefined,
    sortable: true,
  };

  const colOwner: ColDef<File> = {
    field: "owner",
    headerName: "Owner",
    ...(isCloudBrand ? { flex: 1.4, minWidth: 180 } : {}),
    valueFormatter: (params: ValueFormatterParams<File>) =>
      getOwnerLabel(params.data),
    cellClass: isCloudBrand ? "text-muted-foreground" : undefined,
    sortable: true,
    valueGetter: (params: ValueGetterParams<File>) =>
      getOwnerLabel(params.data),
    comparator: serverSideComparator,
  };

  const colIndexedTime: ColDef<File> = {
    field: "indexed_time",
    headerName: "Date Ingested",
    ...(isCloudBrand ? { flex: 1.2, minWidth: 140 } : { minWidth: 130 }),
    sortable: true,
    comparator: serverSideComparator,
    cellClass: isCloudBrand ? "text-muted-foreground" : undefined,
    valueFormatter: ({ value }: ValueFormatterParams<File>) => {
      if (!value) return "—";
      const d = new Date(value);
      return Number.isNaN(d.getTime())
        ? "—"
        : d.toLocaleDateString(undefined, {
            year: "numeric",
            month: "short",
            day: "numeric",
          });
    },
  };

  const colChunks: ColDef<File> = {
    field: "chunkCount",
    headerName: "Chunks",
    ...(isCloudBrand ? { flex: 0.9, minWidth: 95 } : { width: 95 }),
    sortable: true,
    comparator: serverSideComparator,
    headerClass: "ag-center-header",
    cellStyle: { textAlign: "center" },
    cellClass: isCloudBrand ? "text-muted-foreground" : undefined,
    valueFormatter: (params: ValueFormatterParams<File>) =>
      formatChunkCountLabel(params.data?.chunkCount),
  };

  const colAvgScore: ColDef<File> = {
    field: "avgScore",
    headerName: "Avg score",
    hide: isWildcardQuery,
    ...(isCloudBrand ? { flex: 1, minWidth: 120 } : { width: 120 }),
    sortable: true,
    comparator: compareAvgScore,
    cellRenderer: ({ value }: CustomCellRendererProps<File>) => (
      <AvgScoreCellContent value={value} />
    ),
  };

  const colStatus: ColDef<File> = {
    field: "status",
    headerName: "Status",
    ...(isCloudBrand ? { flex: 1, minWidth: 130 } : { width: 130 }),
    sortable: true,
    valueGetter: (params: ValueGetterParams<File>) =>
      getFileStatus(params.data?.status),
    comparator: compareStatusRank,
    cellRenderer: ({ data }: CustomCellRendererProps<File>) => (
      <StatusCellContent
        data={data}
        isCloudBrand={isCloudBrand}
        isOpenragDocsRow={isOpenragDocsRow}
        hasOpenragRefreshCue={hasOpenragRefreshCue}
        selectTask={selectTask}
        getTaskIdForRow={getTaskIdForRow}
        openTaskMenu={openTaskMenu}
        setRecentTasksExpanded={setRecentTasksExpanded}
      />
    ),
  };

  const colActions: ColDef<File> = {
    colId: "actions",
    headerName: "",
    width: 56,
    minWidth: 56,
    ...(isCloudBrand ? { maxWidth: 56 } : { initialFlex: 0 }),
    sortable: false,
    filter: false,
    resizable: false,
    suppressMovable: true,
    cellRenderer: ({ data }: CustomCellRendererProps<File>) => {
      const status = getFileStatus(data?.status);
      const taskId = getTaskIdForRow(data);
      const filePath = data?.source_url || data?.filename || "";
      const variant = resolveActionsVariant(status, taskId, filePath);
      if (variant === "cancel" && data && taskId) {
        return (
          <CancelIngestionButton
            taskId={taskId}
            filePath={filePath}
            onCancel={cancelFile}
          />
        );
      }
      if (variant === "dropdown") {
        return (
          <KnowledgeActionsDropdown
            filename={data?.filename || ""}
            connectorType={data?.connector_type}
            webSourceId={data?.web_source_id}
            webPageId={data?.web_page_id}
            webChildCount={data?.web_child_count}
            searchQuery={effectiveSearchText}
          />
        );
      }
      return null;
    },
    cellStyle: {
      display: "flex",
      alignItems: "center",
      justifyContent: "center",
      padding: 0,
    },
  };

  // ── Layout-aware column sets ─────────────────────────────────────────────
  const columnDefs: ColDef<File>[] = isNarrow
    ? [colSource, colChunks, colAvgScore, colStatus, colActions]
    : [
        colSource,
        colSize,
        colType,
        colOwner,
        colIndexedTime,
        colChunks,
        colAvgScore,
        colStatus,
        colActions,
      ];

  const defaultColDef: ColDef<File> = {
    resizable: false,
    suppressMovable: true,
    ...(isCloudBrand ? { sortable: false } : {}),
    initialFlex: 1,
    minWidth: 100,
  };

  const onSelectionChanged = useCallback(() => {
    const api = getGridApi();
    if (!api) {
      return;
    }
    const nextSelected = syncGridSelectionToDeletableRows(
      api,
      isDeletableKnowledgeRow,
    );
    setSelectedRows((current) =>
      sameFileSelection(current, nextSelected) ? current : nextSelected,
    );
  }, [isDeletableKnowledgeRow, getGridApi]);

  const handleBulkDelete = async () => {
    const rowsToDelete = selectedRows.filter(isDeletableKnowledgeRow);
    if (rowsToDelete.length === 0) return;

    try {
      const deleteResults = await Promise.allSettled(
        rowsToDelete.map((row) =>
          row.web_source_id
            ? deleteWebsiteSourceMutation.mutateAsync({
                sourceId: row.web_source_id,
              })
            : deleteDocumentMutation.mutateAsync({
                filename: resolveDeleteFilename(row),
              }),
        ),
      );

      await Promise.all([
        refreshTasks(),
        queryClient.invalidateQueries({ queryKey: ["search"] }),
        queryClient.invalidateQueries({ queryKey: ["listFiles"] }),
        queryClient.refetchQueries({ queryKey: ["search"] }),
        queryClient.refetchQueries({ queryKey: ["listFiles"] }),
      ]);

      const deleted = deleteResults.filter(
        (result) => result.status === "fulfilled",
      );
      const failed = deleteResults.filter(
        (result): result is PromiseRejectedResult =>
          result.status === "rejected",
      );

      if (deleted.length > 0) {
        toast.success(
          `Deleted ${deleted.length} document${deleted.length > 1 ? "s" : ""}`,
        );
      }

      if (failed.length > 0) {
        toast.error(
          `${failed.length} document${failed.length > 1 ? "s" : ""} could not be deleted`,
          {
            description:
              failed[0].reason instanceof Error
                ? failed[0].reason.message
                : undefined,
          },
        );
      }
      setSelectedRows([]);
      setShowBulkDeleteDialog(false);

      // Clear selection in the grid
      getGridApi()?.deselectAll();
    } catch (error) {
      toast.error(
        error instanceof Error
          ? error.message
          : "Failed to delete some documents",
      );
      setShowBulkDeleteDialog(false);
    }
  };

  return (
    <>
      <div className="flex flex-col h-full">
        <div className="flex items-center justify-between mb-6">
          <h2
            className={cn(
              "text-lg font-semibold",
              isCloudBrand && "ibm-section-title",
            )}
          >
            Project Knowledge
          </h2>
          {!isCloudBrand && isNarrow && (
            <KnowledgeSearchInput
              placeholder="Search..."
              className="max-w-[180px]"
            />
          )}
        </div>
        {isCloudBrand ? (
          <div className="relative overflow-hidden h-12 shrink-0">
            <div
              className={cn(
                "transition-transform duration-200 ease-in-out",
                selectedRows.length > 0
                  ? "-translate-y-full pointer-events-none select-none"
                  : "translate-y-0",
              )}
            >
              <KnowledgeSearchBar />
            </div>
            <div
              className={cn(
                "absolute top-0 left-0 right-0 h-12 transition-transform duration-200 ease-in-out",
                selectedRows.length > 0
                  ? "translate-y-0"
                  : "translate-y-full pointer-events-none select-none",
              )}
            >
              <KnowledgeBatchActionsBar
                selectedCount={selectedRows.length}
                onDelete={() => setShowBulkDeleteDialog(true)}
                onCancel={() => {
                  setSelectedRows([]);
                  getGridApi()?.deselectAll();
                }}
              />
            </div>
          </div>
        ) : (
          <KnowledgeSearchBar
            selectedCount={selectedRows.length}
            onDeleteSelected={() => setShowBulkDeleteDialog(true)}
          />
        )}
        {!isWildcardQuery && searchWarnings.length > 0 && (
          <div className="mb-4 flex flex-col gap-2">
            {searchWarnings.map((warning, idx) => {
              const isEmbeddingWarning =
                warning.code === "embedding_unavailable";
              const semanticDown =
                isEmbeddingWarning &&
                warning.semantic_search_available === false;
              const title = isEmbeddingWarning
                ? semanticDown
                  ? "Semantic search degraded — keyword results only"
                  : "Semantic search partially degraded"
                : warning.message || "Search warning";
              const details =
                warning.models && warning.models.length > 0
                  ? ` Affected embedding model${warning.models.length > 1 ? "s" : ""}: ${warning.models.join(", ")}.`
                  : "";
              return (
                <Banner
                  key={`${warning.code}-${idx}`}
                  inset
                  className="bg-amber-500/10 text-amber-100 border border-amber-500/30"
                >
                  <BannerIcon icon={AlertTriangle} />
                  <BannerTitle>
                    <span className="font-medium">{title}.</span>
                    <span className="ml-1 opacity-90">
                      {isEmbeddingWarning
                        ? `The provider for some indexed documents is no longer reachable, so results rely on keyword matching.${details} Re-configure the provider or re-ingest those documents with another embedding model to restore semantic search.`
                        : warning.message}
                    </span>
                  </BannerTitle>
                </Banner>
              );
            })}
          </div>
        )}
        <KnowledgeDataTable
          rows={gridRows}
          columnDefs={columnDefs}
          defaultColDef={defaultColDef}
          loading={
            isLoading ||
            deleteDocumentMutation.isPending ||
            deleteWebsiteSourceMutation.isPending
          }
          gridRef={gridRef}
          isCloudBrand={isCloudBrand}
          rowSelection="multiple"
          getRowId={(params) => getFileIdentity(params.data)}
          isRowSelectable={(params) => isDeletableKnowledgeRow(params.data)}
          onGridReady={handleGridReady}
          onGridPreDestroyed={handleGridPreDestroyed}
          onSelectionChanged={onSelectionChanged}
          onSortChanged={onSortChanged}
          {...(dateRange && serverTotal === 0
            ? {
                emptyTitle: "No results for the selected date range",
                emptyDescription: "Try a wider range or clear the date filter.",
              }
            : {})}
        />

        <KnowledgePaginationFooter
          currentPage={currentPage}
          currentPageSize={currentPageSize}
          totalPages={totalPages}
          serverTotal={serverTotal}
          isLoading={isFetching}
          cursorCacheRef={cursorCacheRef}
          setCurrentPage={setCurrentPage}
          setCurrentPageSize={setCurrentPageSize}
        />
      </div>

      {/* Bulk Delete Confirmation Dialog */}
      <DeleteConfirmationDialog
        open={showBulkDeleteDialog}
        onOpenChange={setShowBulkDeleteDialog}
        title={selectedRows.length > 1 ? "Delete documents" : "Delete document"}
        description={`Are you sure you want to delete ${selectedRows.length} document${selectedRows.length > 1 ? "s" : ""}?`}
        confirmText={selectedRows.length > 1 ? "Delete all" : "Delete"}
        onConfirm={handleBulkDelete}
        isLoading={
          deleteDocumentMutation.isPending ||
          deleteWebsiteSourceMutation.isPending
        }
      >
        <p className="my-2">
          This will remove all chunks and data associated with these documents.
          This action cannot be undone.
        </p>
        <p className="my-2">Documents to be deleted:</p>
        {formatFilesToDelete(selectedRows)}
      </DeleteConfirmationDialog>
    </>
  );
}

function ProtectedSearchPageContent() {
  const searchParams = useSearchParams();
  const websiteId = searchParams.get("website");
  const websiteSearch = searchParams.get("q") ?? "";
  return (
    <ProtectedRoute>
      {websiteId ? (
        <WebsitePagesView
          key={`${websiteId}:${websiteSearch}`}
          sourceId={websiteId}
          initialSearch={websiteSearch}
        />
      ) : (
        <SearchPage />
      )}
    </ProtectedRoute>
  );
}

export default function ProtectedSearchPage() {
  return (
    <Suspense fallback={null}>
      <ProtectedSearchPageContent />
    </Suspense>
  );
}