Skip to content

Commit 6ddb917

Browse files
refactor into fileMatch component
1 parent a695f0d commit 6ddb917

File tree

2 files changed

+65
-40
lines changed

2 files changed

+65
-40
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use client';
2+
3+
import { useMemo } from "react";
4+
import { CodePreview } from "./codePreview";
5+
import { SearchResultFile, SearchResultFileMatch } from "@/lib/types";
6+
7+
8+
interface FileMatchProps {
9+
match: SearchResultFileMatch;
10+
file: SearchResultFile;
11+
onOpen: () => void;
12+
}
13+
14+
export const FileMatch = ({
15+
match,
16+
file,
17+
onOpen,
18+
}: FileMatchProps) => {
19+
const content = useMemo(() => {
20+
return atob(match.Content);
21+
}, [match.Content]);
22+
23+
// If it's just the title, don't show a code preview
24+
if (match.FileName) {
25+
return null;
26+
}
27+
28+
return (
29+
<div
30+
tabIndex={0}
31+
className="cursor-pointer p-1 focus:ring-inset focus:ring-4 bg-white dark:bg-[#282c34]"
32+
onKeyDown={(e) => {
33+
if (e.key !== "Enter") {
34+
return;
35+
}
36+
onOpen();
37+
}}
38+
onClick={onOpen}
39+
>
40+
<CodePreview
41+
content={content}
42+
language={file.Language}
43+
ranges={match.Ranges}
44+
lineOffset={match.ContentStart.LineNumber - 1}
45+
/>
46+
</div>
47+
);
48+
}

src/app/search/components/searchResultsPanel/fileMatchContainer.tsx

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import Image from "next/image";
66
import { DoubleArrowDownIcon, DoubleArrowUpIcon, FileIcon } from "@radix-ui/react-icons";
77
import clsx from "clsx";
88
import { Separator } from "@/components/ui/separator";
9-
import { CodePreview } from "./codePreview";
109
import { SearchResultFile } from "@/lib/types";
10+
import { FileMatch } from "./fileMatch";
1111

1212
const MAX_MATCHES_TO_PREVIEW = 3;
1313

@@ -128,45 +128,22 @@ export const FileMatchContainer = ({
128128
)}
129129
</div>
130130
</div>
131-
{matches.map((match, index) => {
132-
const content = atob(match.Content);
133-
134-
// If it's just the title, don't show a code preview
135-
if (match.FileName) {
136-
return null;
137-
}
138-
139-
const lineOffset = match.ContentStart.LineNumber - 1;
140-
141-
return (
142-
<div
143-
key={index}
144-
>
145-
<div
146-
tabIndex={0}
147-
className="cursor-pointer p-1 focus:ring-inset focus:ring-4 bg-white dark:bg-[#282c34]"
148-
onKeyDown={(e) => {
149-
if (e.key !== "Enter") {
150-
return;
151-
}
152-
onOpenMatch(index);
153-
}}
154-
onClick={() => onOpenMatch(index)}
155-
>
156-
<CodePreview
157-
content={content}
158-
language={file.Language}
159-
ranges={match.Ranges}
160-
lineOffset={lineOffset}
161-
/>
162-
</div>
163-
164-
{(index !== matches.length - 1 || isMoreContentButtonVisible) && (
165-
<Separator className="dark:bg-gray-400" />
166-
)}
167-
</div>
168-
);
169-
})}
131+
{matches.map((match, index) => (
132+
<div
133+
key={index}
134+
>
135+
<FileMatch
136+
match={match}
137+
file={file}
138+
onOpen={() => {
139+
onOpenMatch(index);
140+
}}
141+
/>
142+
{(index !== matches.length - 1 || isMoreContentButtonVisible) && (
143+
<Separator className="dark:bg-gray-400" />
144+
)}
145+
</div>
146+
))}
170147
{isMoreContentButtonVisible && (
171148
<div
172149
tabIndex={0}

0 commit comments

Comments
 (0)