144 lines · 5.3 KBRaw
| 1 | import { Empty, LinkButton } from "@refs/design-system"; |
| 2 | import { LayerCard } from "@refs/design-system"; |
| 3 | import { ClockCounterClockwiseIcon, FileDashedIcon } from "@refs/design-system/icons"; |
| 4 | import { useEffect, useState } from "react"; |
| 5 | import { data } from "react-router"; |
| 6 | import type { Route } from "./+types/repo-blob"; |
| 7 | import { repoLink, splatSegments, type File } from "@refs/backend/contract"; |
| 8 | import { BranchPicker, PathBreadcrumbs } from "@refs/design-system"; |
| 9 | import type { CodeToken } from "../components/highlight"; |
| 10 | import { found, refs, fullName } from "../lib/session"; |
| 11 | |
| 12 | export const meta: Route.MetaFunction = ({ params }) => [{ title: `${params["*"]} · ${fullName(params)} · refs` }]; |
| 13 | |
| 14 | export async function loader({ params, context }: Route.LoaderArgs) { |
| 15 | const segments = splatSegments(params["*"]); |
| 16 | if (!segments) throw data(null, { status: 404 }); |
| 17 | const repo = await found(refs(context).repo(fullName(params))); |
| 18 | const [file, refList] = await found(Promise.all([repo.file(segments), repo.refs()])); |
| 19 | return { repo: repo.summary.name, refs: refList, file }; |
| 20 | } |
| 21 | |
| 22 | function formatSize(bytes: number): string { |
| 23 | if (bytes < 1024) return `${bytes} B`; |
| 24 | if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; |
| 25 | return `${(bytes / 1024 / 1024).toFixed(1)} MB`; |
| 26 | } |
| 27 | |
| 28 | /** The highlighter is large and works in the browser only, so the server build leaves it out. */ |
| 29 | const loadHighlighter = import.meta.env.SSR ? null : () => import("../components/highlight"); |
| 30 | |
| 31 | /** |
| 32 | * The colours of a file's lines, or null while they load and when the file stays plain. The server sends plain |
| 33 | * text, so the code is readable at once; the colours arrive after and change no layout. |
| 34 | */ |
| 35 | function useHighlight(name: string, lines: readonly string[] | null): CodeToken[][] | null { |
| 36 | const [tokens, setTokens] = useState<CodeToken[][] | null>(null); |
| 37 | useEffect(() => { |
| 38 | setTokens(null); |
| 39 | if (!lines || !loadHighlighter) return; |
| 40 | let current = true; |
| 41 | loadHighlighter() |
| 42 | .then((module) => module.highlightLines(name, lines)) |
| 43 | .then((result) => current && setTokens(result)) |
| 44 | .catch((error: unknown) => console.error("highlighting failed", error)); |
| 45 | return () => { |
| 46 | current = false; |
| 47 | }; |
| 48 | }, [name, lines]); |
| 49 | return tokens; |
| 50 | } |
| 51 | |
| 52 | function CodeLine({ text, tokens }: { text: string; tokens: readonly CodeToken[] | undefined }) { |
| 53 | if (!tokens) return text; |
| 54 | return tokens.map((token, index) => |
| 55 | token.light || token.dark ? ( |
| 56 | <span key={index} style={{ ["--token-light" as string]: token.light, ["--token-dark" as string]: token.dark }}> |
| 57 | {token.text} |
| 58 | </span> |
| 59 | ) : ( |
| 60 | token.text |
| 61 | ), |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | function FileBodyView({ file }: { file: File }) { |
| 66 | const tokens = useHighlight(file.name, file.body.kind === "text" ? file.body.lines : null); |
| 67 | const body = file.body; |
| 68 | if (body.kind === "image") { |
| 69 | return ( |
| 70 | <div className="checkerboard flex justify-center p-8"> |
| 71 | <img src={body.rawUrl} alt={file.name} className="max-w-full" /> |
| 72 | </div> |
| 73 | ); |
| 74 | } |
| 75 | if (body.kind === "markdown") return <article className="markdown px-6 py-5 sm:px-8 sm:py-6" dangerouslySetInnerHTML={{ __html: body.html }} />; |
| 76 | if (body.kind === "text") { |
| 77 | return ( |
| 78 | <div className="overflow-x-auto"> |
| 79 | <table className="code-view"> |
| 80 | <tbody> |
| 81 | {body.lines.map((line, i) => ( |
| 82 | <tr key={i} id={`L${i + 1}`}> |
| 83 | <td className="line-number"> |
| 84 | <a href={`#L${i + 1}`}>{i + 1}</a> |
| 85 | </td> |
| 86 | <td className="line"> |
| 87 | <CodeLine text={line} tokens={tokens?.[i]} /> |
| 88 | </td> |
| 89 | </tr> |
| 90 | ))} |
| 91 | </tbody> |
| 92 | </table> |
| 93 | </div> |
| 94 | ); |
| 95 | } |
| 96 | return ( |
| 97 | <div className="p-6"> |
| 98 | <Empty |
| 99 | size="sm" |
| 100 | icon={<FileDashedIcon size={32} />} |
| 101 | title={body.kind === "binary" ? "Binary file not shown" : "File too large to display"} |
| 102 | contents={ |
| 103 | <LinkButton variant="secondary" href={body.rawUrl}> |
| 104 | View raw |
| 105 | </LinkButton> |
| 106 | } |
| 107 | /> |
| 108 | </div> |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | export default function RepoBlob({ loaderData }: Route.ComponentProps) { |
| 113 | const { repo, refs: refList, file } = loaderData; |
| 114 | const { ref, path } = file.location; |
| 115 | const rawUrl = repoLink(repo, "raw", ref, path); |
| 116 | const lines = file.body.kind === "text" ? `${file.body.lines.length} lines · ` : ""; |
| 117 | return ( |
| 118 | <div className="flex flex-col gap-4"> |
| 119 | <div className="flex flex-wrap items-center justify-between gap-3"> |
| 120 | <div className="flex min-w-0 items-center gap-3"> |
| 121 | <BranchPicker repo={repo} refs={refList} current={ref} /> |
| 122 | <PathBreadcrumbs repo={repo} current={ref} path={path} /> |
| 123 | </div> |
| 124 | <LinkButton variant="secondary" href={repoLink(repo, "commits", ref)} icon={<ClockCounterClockwiseIcon />}> |
| 125 | History |
| 126 | </LinkButton> |
| 127 | </div> |
| 128 | <LayerCard> |
| 129 | <LayerCard.Secondary className="justify-between py-2 font-normal"> |
| 130 | <span className="text-compact tabular-nums"> |
| 131 | {lines} |
| 132 | {formatSize(file.size)} |
| 133 | </span> |
| 134 | <LinkButton variant="secondary" size="sm" href={rawUrl}> |
| 135 | Raw |
| 136 | </LinkButton> |
| 137 | </LayerCard.Secondary> |
| 138 | <LayerCard.Primary className="gap-0 p-0"> |
| 139 | <FileBodyView file={file} /> |
| 140 | </LayerCard.Primary> |
| 141 | </LayerCard> |
| 142 | </div> |
| 143 | ); |
| 144 | } |