'use client'; import { FileHeader } from "@/app/[domain]/components/fileHeader"; import { Separator } from "@/components/ui/separator"; import { DoubleArrowDownIcon, DoubleArrowUpIcon } from "@radix-ui/react-icons"; import { useCallback, useMemo } from "react"; import { FileMatch } from "./fileMatch"; import { Repository, SearchResultFile } from "@/features/search/types"; export const MAX_MATCHES_TO_PREVIEW = 3; interface FileMatchContainerProps { file: SearchResultFile; onOpenFile: () => void; onMatchIndexChanged: (matchIndex: number) => void; showAllMatches: boolean; onShowAllMatchesButtonClicked: () => void; isBranchFilteringEnabled: boolean; repoMetadata: Record; yOffset: number; } export const FileMatchContainer = ({ file, onOpenFile, onMatchIndexChanged, showAllMatches, onShowAllMatchesButtonClicked, isBranchFilteringEnabled, repoMetadata, yOffset, }: FileMatchContainerProps) => { const matchCount = useMemo(() => { return file.chunks.length; }, [file]); const matches = useMemo(() => { const sortedMatches = file.chunks.sort((a, b) => { return a.contentStart.lineNumber - b.contentStart.lineNumber; }); if (!showAllMatches) { return sortedMatches.slice(0, MAX_MATCHES_TO_PREVIEW); } return sortedMatches; }, [file, showAllMatches]); const fileNameRange = useMemo(() => { if (file.fileName.matchRanges.length > 0) { const range = file.fileName.matchRanges[0]; return { from: range.start.column - 1, to: range.end.column - 1, } } return undefined; }, [file.fileName.matchRanges]); const isMoreContentButtonVisible = useMemo(() => { return matchCount > MAX_MATCHES_TO_PREVIEW; }, [matchCount]); const onOpenMatch = useCallback((index: number) => { const matchIndex = matches.slice(0, index).reduce((acc, match) => { return acc + match.matchRanges.length; }, 0); onOpenFile(); onMatchIndexChanged(matchIndex); }, [matches, onMatchIndexChanged, onOpenFile]); const branches = useMemo(() => { if (!file.branches) { return []; } return file.branches; }, [file.branches]); const branchDisplayName = useMemo(() => { if (!isBranchFilteringEnabled || branches.length === 0) { return undefined; } return `${branches[0]}${branches.length > 1 ? ` +${branches.length - 1}` : ''}`; }, [isBranchFilteringEnabled, branches]); return (
{/* Title */}
{ onOpenFile(); }} >
{/* Matches */} {matches.map((match, index) => (
{ onOpenMatch(index); }} /> {(index !== matches.length - 1 || isMoreContentButtonVisible) && ( )}
))} {/* Show more button */} {isMoreContentButtonVisible && (
{ if (e.key !== "Enter") { return; } onShowAllMatchesButtonClicked(); }} onClick={onShowAllMatchesButtonClicked} >

{showAllMatches ? : } {showAllMatches ? `Show fewer matches` : `Show ${matchCount - MAX_MATCHES_TO_PREVIEW} more matches`}

)}
); }