-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathindex.tsx
More file actions
76 lines (68 loc) · 2.47 KB
/
index.tsx
File metadata and controls
76 lines (68 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use client';
import { useQuery } from "@tanstack/react-query";
import { CodePreview } from "./codePreview";
import { SearchResultFile } from "@/features/search/types";
import { useDomain } from "@/hooks/useDomain";
import { SymbolIcon } from "@radix-ui/react-icons";
import { SetStateAction, Dispatch, useMemo } from "react";
import { getFileSource } from "@/features/search/fileSourceApi";
import { unwrapServiceError } from "@/lib/utils";
interface CodePreviewPanelProps {
previewedFile: SearchResultFile;
selectedMatchIndex: number;
onClose: () => void;
onSelectedMatchIndexChange: Dispatch<SetStateAction<number>>;
}
export const CodePreviewPanel = ({
previewedFile,
selectedMatchIndex,
onClose,
onSelectedMatchIndexChange,
}: CodePreviewPanelProps) => {
const domain = useDomain();
// If there are multiple branches pointing to the same revision of this file, it doesn't
// matter which branch we use here, so use the first one.
const branch = useMemo(() => {
return previewedFile.branches && previewedFile.branches.length > 0 ? previewedFile.branches[0] : undefined;
}, [previewedFile]);
const { data: file, isLoading, isPending, isError } = useQuery({
queryKey: ["source", previewedFile, branch, domain],
queryFn: () => unwrapServiceError(
getFileSource({
fileName: previewedFile.fileName.text,
repository: previewedFile.repository,
branch,
}, domain)
),
select: (data) => {
return {
content: data.source,
filepath: previewedFile.fileName.text,
matches: previewedFile.chunks,
link: previewedFile.webUrl,
language: previewedFile.language,
revision: branch ?? "HEAD",
};
}
});
if (isLoading || isPending) {
return <div className="flex flex-col items-center justify-center h-full">
<SymbolIcon className="h-6 w-6 animate-spin" />
<p className="font-semibold text-center">Loading...</p>
</div>
}
if (isError) {
return (
<p>Failed to load file source</p>
)
}
return (
<CodePreview
file={file}
repoName={previewedFile.repository}
selectedMatchIndex={selectedMatchIndex}
onSelectedMatchIndexChange={onSelectedMatchIndexChange}
onClose={onClose}
/>
)
}