sourcebot/packages/web/src/app/[domain]/browse/[...path]/components/codePreviewPanel.tsx
Brendan Kellam d63f3cf9d9
Some checks failed
Publish to ghcr / build (linux/amd64, blacksmith-4vcpu-ubuntu-2404) (push) Has been cancelled
Publish to ghcr / build (linux/arm64, blacksmith-8vcpu-ubuntu-2204-arm) (push) Has been cancelled
Update Roadmap Released / update (push) Has been cancelled
Publish to ghcr / merge (push) Has been cancelled
chore(web): Improve error messages for file loading errors (#665)
2025-12-05 11:58:19 -08:00

86 lines
No EOL
3.3 KiB
TypeScript

import { getRepoInfoByName } from "@/actions";
import { PathHeader } from "@/app/[domain]/components/pathHeader";
import { Separator } from "@/components/ui/separator";
import { cn, getCodeHostInfoForRepo, isServiceError } from "@/lib/utils";
import Image from "next/image";
import { PureCodePreviewPanel } from "./pureCodePreviewPanel";
import { getFileSource } from "@/features/search/fileSourceApi";
interface CodePreviewPanelProps {
path: string;
repoName: string;
revisionName?: string;
}
export const CodePreviewPanel = async ({ path, repoName, revisionName }: CodePreviewPanelProps) => {
const [fileSourceResponse, repoInfoResponse] = await Promise.all([
getFileSource({
fileName: path,
repository: repoName,
branch: revisionName,
}),
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,
webUrl: repoInfoResponse.webUrl,
});
// @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.webUrl ?
fileSourceResponse.webUrl.replace("version=GBHEAD", "version=GBmain") : fileSourceResponse.webUrl;
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,
webUrl: repoInfoResponse.webUrl,
}}
branchDisplayName={revisionName}
/>
{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 />
<PureCodePreviewPanel
source={fileSourceResponse.source}
language={fileSourceResponse.language}
repoName={repoName}
path={path}
revisionName={revisionName ?? 'HEAD'}
/>
</>
)
}