'use client'; import { getRepoCodeHostInfo } from "@/lib/utils"; import { useCallback, useMemo } from "react"; import Image from "next/image"; import { DoubleArrowDownIcon, DoubleArrowUpIcon, FileIcon } from "@radix-ui/react-icons"; import clsx from "clsx"; import { Separator } from "@/components/ui/separator"; import { SearchResultFile } from "@/lib/types"; import { FileMatch } from "./fileMatch"; export const MAX_MATCHES_TO_PREVIEW = 3; interface FileMatchContainerProps { file: SearchResultFile; onOpenFile: () => void; onMatchIndexChanged: (matchIndex: number) => void; showAllMatches: boolean; onShowAllMatchesButtonClicked: () => void; } export const FileMatchContainer = ({ file, onOpenFile, onMatchIndexChanged, showAllMatches, onShowAllMatchesButtonClicked, }: FileMatchContainerProps) => { const matchCount = useMemo(() => { return file.ChunkMatches.length; }, [file]); const matches = useMemo(() => { const sortedMatches = file.ChunkMatches.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(() => { for (const match of matches) { if (match.FileName && match.Ranges.length > 0) { const range = match.Ranges[0]; return { from: range.Start.Column - 1, to: range.End.Column - 1, } } } return null; }, [matches]); const { repoIcon, repoName, repoLink } = useMemo(() => { const info = getRepoCodeHostInfo(file.Repository); if (info) { return { repoName: info.repoName, repoLink: info.repoLink, repoIcon: {info.costHostName} } } return { repoName: file.Repository, repoLink: undefined, repoIcon: } }, [file]); 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.Ranges.length; }, 0); onOpenFile(); onMatchIndexChanged(matchIndex); }, [matches, onMatchIndexChanged, onOpenFile]); return (
{/* Title */}
{ onOpenFile(); }} >
{repoIcon} { if (repoLink) { window.open(repoLink, "_blank"); } }} > {repoName} ยท
{!fileNameRange ? file.FileName : ( <> {file.FileName.slice(0, fileNameRange.from)} {file.FileName.slice(fileNameRange.from, fileNameRange.to)} {file.FileName.slice(fileNameRange.to)} )}
{/* 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`}

)}
); }