2024-09-26 03:12:20 +00:00
|
|
|
'use client';
|
|
|
|
|
|
2025-05-28 23:08:42 +00:00
|
|
|
import { useCallback, useMemo } from "react";
|
2025-05-03 18:33:58 +00:00
|
|
|
import { SearchResultFile, SearchResultChunk } from "@/features/search/types";
|
2024-10-16 00:33:44 +00:00
|
|
|
import { base64Decode } from "@/lib/utils";
|
2025-05-28 23:08:42 +00:00
|
|
|
import { LightweightCodeHighlighter } from "@/app/[domain]/components/lightweightCodeHighlighter";
|
2024-09-26 03:12:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
interface FileMatchProps {
|
2025-05-03 18:33:58 +00:00
|
|
|
match: SearchResultChunk;
|
2024-09-26 03:12:20 +00:00
|
|
|
file: SearchResultFile;
|
2025-05-28 23:08:42 +00:00
|
|
|
onOpen: (startLineNumber: number, endLineNumber: number, isCtrlKeyPressed: boolean) => void;
|
2024-09-26 03:12:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const FileMatch = ({
|
|
|
|
|
match,
|
|
|
|
|
file,
|
2025-05-28 23:08:42 +00:00
|
|
|
onOpen: _onOpen,
|
2024-09-26 03:12:20 +00:00
|
|
|
}: FileMatchProps) => {
|
2025-05-28 23:08:42 +00:00
|
|
|
|
2024-09-26 03:12:20 +00:00
|
|
|
const content = useMemo(() => {
|
2025-05-03 18:33:58 +00:00
|
|
|
return base64Decode(match.content);
|
|
|
|
|
}, [match.content]);
|
2024-09-26 03:12:20 +00:00
|
|
|
|
2025-05-28 23:08:42 +00:00
|
|
|
const onOpen = useCallback((isCtrlKeyPressed: boolean) => {
|
|
|
|
|
const startLineNumber = match.contentStart.lineNumber;
|
|
|
|
|
const endLineNumber = content.trimEnd().split('\n').length + startLineNumber - 1;
|
|
|
|
|
|
|
|
|
|
_onOpen(startLineNumber, endLineNumber, isCtrlKeyPressed);
|
|
|
|
|
}, [content, match.contentStart.lineNumber, _onOpen]);
|
|
|
|
|
|
2024-09-26 03:12:20 +00:00
|
|
|
// If it's just the title, don't show a code preview
|
2025-05-03 18:33:58 +00:00
|
|
|
if (match.matchRanges.length === 0) {
|
2024-09-26 03:12:20 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
tabIndex={0}
|
2025-05-28 23:08:42 +00:00
|
|
|
className="cursor-pointer focus:ring-inset focus:ring-4 bg-background hover:bg-editor-lineHighlight"
|
2024-09-26 03:12:20 +00:00
|
|
|
onKeyDown={(e) => {
|
|
|
|
|
if (e.key !== "Enter") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-05-28 23:08:42 +00:00
|
|
|
|
|
|
|
|
onOpen(e.metaKey || e.ctrlKey);
|
|
|
|
|
}}
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
onOpen(e.metaKey || e.ctrlKey);
|
2024-09-26 03:12:20 +00:00
|
|
|
}}
|
2025-05-28 23:08:42 +00:00
|
|
|
title="open file: click, open file preview: cmd/ctrl + click"
|
2024-09-26 03:12:20 +00:00
|
|
|
>
|
2025-05-28 23:08:42 +00:00
|
|
|
<LightweightCodeHighlighter
|
2025-05-03 18:33:58 +00:00
|
|
|
language={file.language}
|
2025-05-28 23:08:42 +00:00
|
|
|
highlightRanges={match.matchRanges}
|
|
|
|
|
lineNumbers={true}
|
|
|
|
|
lineNumbersOffset={match.contentStart.lineNumber}
|
|
|
|
|
renderWhitespace={true}
|
|
|
|
|
>
|
|
|
|
|
{content}
|
|
|
|
|
</LightweightCodeHighlighter>
|
2024-09-26 03:12:20 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|