| 1 | "use client"; |
| 2 | |
| 3 | import Link from "next/link" |
| 4 | import { useTheme } |
| 5 | import { Fragment, type |
| 6 | import type { ThemedToken |
| 7 | |
| 8 | import type { components |
| 9 | import { highlightLines } |
| 10 | |
| 11 | export type CodeSearchMatch |
| 12 | repository?: components |
| 13 | }; |
| 14 | |
| 15 | export type CodeSearchRepository |
| 16 | |
| 17 | type Range |
| 18 | |
| 19 | /** One file's matching lines. They render as plain text and are highlighted with the active theme alone once the file nears the viewport. */ |
| 20 | export function CodeSearchFile({ |
| 21 | file, |
| 22 | repository, |
| 23 | }: { |
| 24 | file: CodeSearchMatch |
| 25 | /** Required when the file carries no repository of its own: a search scoped to one repository. */ |
| 26 | repository?: CodeSearchRepository |
| 27 | }) { |
| 28 | const container = |
| 29 | const [visible, |
| 30 | const [highlighted, |
| 31 | theme: string |
| 32 | tokens: ThemedToken |
| 33 | }>(); |
| 34 | const { resolvedTheme |
| 35 | |
| 36 | useEffect(() => |
| 37 | const node = |
| 38 | if (!node) |
| 39 | |
| 40 | const observer = |
| 41 | ([entry]) => |
| 42 | if (!entry.isIntersecting |
| 43 | observer.disconnect(); |
| 44 | setVisible(true); |
| 45 | }, |
| 46 | { rootMargin: "800px 0px" |
| 47 | ); |
| 48 | |
| 49 | observer.observe(node |
| 50 | return () => |
| 51 | }, []); |
| 52 | |
| 53 | useEffect(() => |
| 54 | if (!visible || |
| 55 | |
| 56 | let cancelled = |
| 57 | const filename = |
| 58 | // The matches are scattered lines, not a file, so each is highlighted on its own: a grammar state carried across a gap would colour the next line wrongly. |
| 59 | Promise.all( |
| 60 | file.lines.map |
| 61 | async ({ line |
| 62 | (await highlightLines(line |
| 63 | ), |
| 64 | ).then((tokens |
| 65 | if (!cancelled) |
| 66 | }); |
| 67 | |
| 68 | return () => |
| 69 | cancelled = true; |
| 70 | }; |
| 71 | }, [visible, |
| 72 | |
| 73 | // Tokens coloured for another theme carry none of the active theme's colours, so a theme switch shows plain text until the new ones land. |
| 74 | const tokens = |
| 75 | highlighted && highlighted. |
| 76 | ? highlighted.tokens |
| 77 | : undefined; |
| 78 | const { owner, |
| 79 | // Line numbers refer to the commit the index was built from, so link to that commit rather than a branch that may have moved. |
| 80 | const blob = |
| 81 | // `matchCount` counts matches, and one line can hold several |
| 82 | const hidden = |
| 83 | file.matchCount - |
| 84 | file.lines.reduce |
| 85 | |
| 86 | return ( |
| 87 | // ponytail: off-screen files skip layout and paint but keep their DOM nodes; a windowed list frees those too if thousands of long files ever weigh on memory. |
| 88 | <article |
| 89 | ref={container} |
| 90 | className="overflow-hidden rounded-lg border [contain-intrinsic-size:auto_200px] [content-visibility:auto]" |
| 91 | > |
| 92 | <header className="flex items-center gap-2 border-b bg-muted/40 px-4 py-2 text-sm |
| 93 | {file.repository && |
| 94 | <> |
| 95 | <Link |
| 96 | href={`/${owner |
| 97 | className="text-muted-foreground hover:text-foreground hover:underline" |
| 98 | > |
| 99 | {owner}/{file |
| 100 | </Link> |
| 101 | <span className="text-muted-foreground/60 |
| 102 | </> |
| 103 | )} |
| 104 | <Link |
| 105 | href={blob} |
| 106 | className="min-w-0 truncate font-medium hover:underline" |
| 107 | > |
| 108 | {file.path} |
| 109 | </Link> |
| 110 | </header> |
| 111 | |
| 112 | {file.lines.length |
| 113 | <div className="overflow-x-auto |
| 114 | <table className="w-full border-collapse font-mono text-xs |
| 115 | <tbody> |
| 116 | {file.lines.map |
| 117 | const previous = |
| 118 | |
| 119 | return ( |
| 120 | <Fragment key={ |
| 121 | {previous && line |
| 122 | <tr aria-hidden className |
| 123 | <td className="select-none border-y border-r py-0.5 pr-3 text-right text-muted-foreground |
| 124 | ⋯ |
| 125 | </td> |
| 126 | <td className="border-y |
| 127 | </tr> |
| 128 | )} |
| 129 | <tr className="hover:bg-muted/40 |
| 130 | <td className="w-12 select-none border-r py-0.5 pr-3 text-right align-top text-muted-foreground |
| 131 | <Link |
| 132 | href={`${blob |
| 133 | className="hover:text-foreground" |
| 134 | > |
| 135 | {line.lineNumber} |
| 136 | </Link> |
| 137 | </td> |
| 138 | <td className="py-0.5 pl-4 whitespace-pre |
| 139 | {markMatches( |
| 140 | tokens?.[l] |
| 141 | line.ranges, |
| 142 | )} |
| 143 | </td> |
| 144 | </tr> |
| 145 | </Fragment> |
| 146 | ); |
| 147 | })} |
| 148 | </tbody> |
| 149 | </table> |
| 150 | </div> |
| 151 | )} |
| 152 | {hidden > 0 |
| 153 | <Link |
| 154 | href={blob} |
| 155 | className="block border-t bg-muted/20 px-4 py-1.5 text-xs text-muted-foreground hover:text-foreground hover:underline" |
| 156 | > |
| 157 | {hidden} more {hidden |
| 158 | </Link> |
| 159 | )} |
| 160 | </article> |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | /** Syntax tokens with the matched spans marked. A match can start or end mid-token, so tokens are cut at match boundaries and each piece keeps its colour; the pieces of one match share a single mark. */ |
| 165 | export function markMatches(tokens |
| 166 | const pieces: |
| 167 | |
| 168 | for (const token |
| 169 | const end = |
| 170 | let at = |
| 171 | while (at < |
| 172 | const range = |
| 173 | const to = |
| 174 | ? Math.min( |
| 175 | : Math.min( |
| 176 | pieces.push({ |
| 177 | range, |
| 178 | node: ( |
| 179 | <span key={at |
| 180 | {token.content.slice |
| 181 | </span> |
| 182 | ), |
| 183 | }); |
| 184 | at = to; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | const parts: |
| 189 | for (let i |
| 190 | const { range |
| 191 | if (!range) |
| 192 | parts.push(pieces |
| 193 | continue; |
| 194 | } |
| 195 | const nodes: |
| 196 | while (pieces[i |
| 197 | parts.push( |
| 198 | <mark key={` |
| 199 | {nodes} |
| 200 | </mark>, |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | return parts; |
| 205 | } |