-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathcodePreviewPanel.tsx
More file actions
138 lines (128 loc) · 5.69 KB
/
Copy pathcodePreviewPanel.tsx
File metadata and controls
138 lines (128 loc) · 5.69 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { getRepoInfoByName } from "@/actions";
import { PathHeader } from "@/app/(app)/components/pathHeader";
import { Button } from "@/components/ui/button";
import { Separator } from "@/components/ui/separator";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import { cn, getCodeHostInfoForRepo, isServiceError, truncateSha } from "@/lib/utils";
import { X } from "lucide-react";
import Image from "next/image";
import Link from "next/link";
import { getBrowsePath } from "../../../hooks/utils";
import { PureCodePreviewPanel } from "./pureCodePreviewPanel";
import { getFileSource } from '@/features/git';
interface CodePreviewPanelProps {
path: string;
repoName: string;
revisionName?: string;
// When set, the file's content is fetched at this ref while the
// surrounding browse context (path header) stays at `revisionName`.
previewRef?: string;
}
export const CodePreviewPanel = async ({ path, repoName, revisionName, previewRef }: CodePreviewPanelProps) => {
const contentRef = previewRef ?? revisionName;
const [fileSourceResponse, repoInfoResponse] = await Promise.all([
getFileSource({
path,
repo: repoName,
ref: contentRef,
}, { source: 'sourcebot-web-client' }),
getRepoInfoByName(repoName),
]);
if (isServiceError(fileSourceResponse)) {
return <div>Error loading file source: {fileSourceResponse.message}</div>
}
if (isServiceError(repoInfoResponse)) {
return <div>Error loading repo info: {repoInfoResponse.message}</div>
}
const codeHostInfo = getCodeHostInfoForRepo({
codeHostType: repoInfoResponse.codeHostType,
name: repoInfoResponse.name,
displayName: repoInfoResponse.displayName,
externalWebUrl: repoInfoResponse.externalWebUrl,
});
// @todo: this is a hack to support linking to files for ADO. ADO doesn't support web urls with HEAD so we replace it with main. THis
// will break if the default branch is not main.
const fileWebUrl = repoInfoResponse.codeHostType === "azuredevops" && fileSourceResponse.externalWebUrl ?
fileSourceResponse.externalWebUrl.replace("version=GBHEAD", "version=GBmain") : fileSourceResponse.externalWebUrl;
return (
<>
<div className="flex flex-row py-1 px-2 items-center justify-between">
<PathHeader
path={path}
repo={{
name: repoName,
codeHostType: repoInfoResponse.codeHostType,
displayName: repoInfoResponse.displayName,
externalWebUrl: repoInfoResponse.externalWebUrl,
}}
revisionName={contentRef}
/>
{fileWebUrl && (
<a
href={fileWebUrl}
target="_blank"
rel="noopener noreferrer"
className="flex flex-row items-center gap-2 px-2 py-0.5 rounded-md flex-shrink-0"
>
<Image
src={codeHostInfo.icon}
alt={codeHostInfo.codeHostName}
className={cn('w-4 h-4 flex-shrink-0', codeHostInfo.iconClassName)}
/>
<span className="text-sm font-medium">Open in {codeHostInfo.codeHostName}</span>
</a>
)}
</div>
<Separator />
{previewRef && (
<div className="flex flex-row items-center justify-between gap-2 px-4 py-2 border-b shrink-0">
<span className="text-sm">
Previewing file at revision{" "}
<Link
href={getBrowsePath({
repoName,
revisionName,
path: '',
pathType: 'commit',
commitSha: previewRef,
})}
className="font-mono text-link hover:underline"
>
{truncateSha(previewRef)}
</Link>
</span>
<Tooltip key={previewRef}>
<TooltipTrigger>
<Button
asChild
variant="ghost"
size="icon"
className="h-6 w-6 text-muted-foreground"
>
<Link
href={getBrowsePath({
repoName,
revisionName,
path,
pathType: 'blob',
})}
aria-label="Close preview"
>
<X className="h-4 w-4" />
</Link>
</Button>
</TooltipTrigger>
<TooltipContent>Close preview</TooltipContent>
</Tooltip>
</div>
)}
<PureCodePreviewPanel
source={fileSourceResponse.source}
language={fileSourceResponse.language}
repoName={repoName}
path={path}
revisionName={contentRef ?? 'HEAD'}
/>
</>
)
}