2024-09-26 03:12:20 +00:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { fetchFileSource } from "@/app/api/(client)/client";
|
2024-11-27 05:49:41 +00:00
|
|
|
import { base64Decode } from "@/lib/utils";
|
2024-09-26 03:12:20 +00:00
|
|
|
import { useQuery } from "@tanstack/react-query";
|
|
|
|
|
import { CodePreview, CodePreviewFile } from "./codePreview";
|
|
|
|
|
import { SearchResultFile } from "@/lib/types";
|
|
|
|
|
|
|
|
|
|
interface CodePreviewPanelProps {
|
|
|
|
|
fileMatch?: SearchResultFile;
|
|
|
|
|
onClose: () => void;
|
|
|
|
|
selectedMatchIndex: number;
|
|
|
|
|
onSelectedMatchIndexChange: (index: number) => void;
|
2024-11-27 05:49:41 +00:00
|
|
|
repoUrlTemplates: Record<string, string>;
|
2024-09-26 03:12:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const CodePreviewPanel = ({
|
|
|
|
|
fileMatch,
|
|
|
|
|
onClose,
|
|
|
|
|
selectedMatchIndex,
|
|
|
|
|
onSelectedMatchIndexChange,
|
2024-11-27 05:49:41 +00:00
|
|
|
repoUrlTemplates,
|
2024-09-26 03:12:20 +00:00
|
|
|
}: CodePreviewPanelProps) => {
|
|
|
|
|
|
|
|
|
|
const { data: file } = useQuery({
|
2024-11-07 02:28:10 +00:00
|
|
|
queryKey: ["source", fileMatch?.FileName, fileMatch?.Repository, fileMatch?.Branches],
|
2024-09-26 03:12:20 +00:00
|
|
|
queryFn: async (): Promise<CodePreviewFile | undefined> => {
|
|
|
|
|
if (!fileMatch) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 02:28:10 +00:00
|
|
|
// 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 = fileMatch.Branches && fileMatch.Branches.length > 0 ? fileMatch.Branches[0] : undefined;
|
|
|
|
|
|
|
|
|
|
return fetchFileSource({
|
|
|
|
|
fileName: fileMatch.FileName,
|
|
|
|
|
repository: fileMatch.Repository,
|
|
|
|
|
branch,
|
|
|
|
|
})
|
2024-09-26 03:12:20 +00:00
|
|
|
.then(({ source }) => {
|
2024-11-27 05:49:41 +00:00
|
|
|
const link = (() => {
|
|
|
|
|
const template = repoUrlTemplates[fileMatch.Repository];
|
|
|
|
|
if (!template) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
return template
|
|
|
|
|
.replace("{{.Version}}", branch ?? "HEAD")
|
|
|
|
|
.replace("{{.Path}}", fileMatch.FileName);
|
|
|
|
|
})();
|
2024-09-26 03:12:20 +00:00
|
|
|
|
2024-10-16 00:33:44 +00:00
|
|
|
const decodedSource = base64Decode(source);
|
2024-09-26 03:12:20 +00:00
|
|
|
|
|
|
|
|
// Filter out filename matches
|
|
|
|
|
const filteredMatches = fileMatch.ChunkMatches.filter((match) => {
|
|
|
|
|
return !match.FileName;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
content: decodedSource,
|
|
|
|
|
filepath: fileMatch.FileName,
|
|
|
|
|
matches: filteredMatches,
|
|
|
|
|
link: link,
|
|
|
|
|
language: fileMatch.Language,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
enabled: fileMatch !== undefined,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<CodePreview
|
|
|
|
|
file={file}
|
|
|
|
|
onClose={onClose}
|
|
|
|
|
selectedMatchIndex={selectedMatchIndex}
|
|
|
|
|
onSelectedMatchIndexChange={onSelectedMatchIndexChange}
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|