| 1 | import { ImageResponse } from "next/og"; |
| 2 | import { createElement } |
| 3 | |
| 4 | import { type IconName, |
| 5 | import { languageColor } |
| 6 | |
| 7 | /** Shared by every card route. */ |
| 8 | export const size = |
| 9 | export const contentType = |
| 10 | |
| 11 | // Satori resolves no CSS variables, so the palette is spelled out. |
| 12 | const INK = " |
| 13 | const FOREGROUND = " |
| 14 | const MUTED = " |
| 15 | const HAIRLINE = " |
| 16 | const ACCENT = " |
| 17 | |
| 18 | export type OgStat |
| 19 | export type OgLanguage |
| 20 | export type OgBadge |
| 21 | |
| 22 | export function ogCard({ |
| 23 | eyebrow, |
| 24 | icon, |
| 25 | avatar, |
| 26 | badge, |
| 27 | title, |
| 28 | description, |
| 29 | stats, |
| 30 | languages, |
| 31 | }: { |
| 32 | eyebrow: string |
| 33 | /** Sits before the eyebrow. */ |
| 34 | icon?: IconName |
| 35 | /** Drawn large beside the title, with the description under it. */ |
| 36 | avatar?: string |
| 37 | badge?: OgBadge |
| 38 | title: string |
| 39 | description?: string |
| 40 | stats?: OgStat |
| 41 | /** Drawn as a bar above the footer, in the order the API returns. */ |
| 42 | languages?: OgLanguage |
| 43 | }) { |
| 44 | return new ImageResponse |
| 45 | <div |
| 46 | style={{ |
| 47 | width: "100%", |
| 48 | height: "100%", |
| 49 | display: "flex", |
| 50 | flexDirection: "column", |
| 51 | justifyContent: "space-between", |
| 52 | background: INK, |
| 53 | color: FOREGROUND, |
| 54 | padding: 72, |
| 55 | fontFamily: "sans-serif", |
| 56 | }} |
| 57 | > |
| 58 | <div |
| 59 | style={{ |
| 60 | display: "flex", |
| 61 | alignItems: "center", |
| 62 | justifyContent: "space-between", |
| 63 | }} |
| 64 | > |
| 65 | <div style={{ |
| 66 | {icon ? <Icon |
| 67 | <div style={{ |
| 68 | {truncate(eyebrow, |
| 69 | </div> |
| 70 | </div> |
| 71 | |
| 72 | {badge ? ( |
| 73 | <div |
| 74 | style={{ |
| 75 | display: "flex", |
| 76 | alignItems: "center", |
| 77 | gap: 12, |
| 78 | fontSize: 24, |
| 79 | letterSpacing: 2, |
| 80 | textTransform: "uppercase", |
| 81 | color: badge.color |
| 82 | }} |
| 83 | > |
| 84 | <div |
| 85 | style={{ |
| 86 | display: "flex", |
| 87 | width: 12, |
| 88 | height: 12, |
| 89 | borderRadius: 12, |
| 90 | background: badge.color |
| 91 | }} |
| 92 | /> |
| 93 | {badge.label} |
| 94 | </div> |
| 95 | ) : null} |
| 96 | </div> |
| 97 | |
| 98 | {usable(avatar) |
| 99 | <div style={{ |
| 100 | <img |
| 101 | src={avatar} |
| 102 | width={200} |
| 103 | height={200} |
| 104 | alt="" |
| 105 | style={{ borderRadius: |
| 106 | /> |
| 107 | <div style={{ |
| 108 | <div |
| 109 | style={{ |
| 110 | display: "flex", |
| 111 | fontSize: titleSize(title |
| 112 | fontWeight: 600, |
| 113 | letterSpacing: -1, |
| 114 | lineHeight: 1.15, |
| 115 | wordBreak: "break-word", |
| 116 | }} |
| 117 | > |
| 118 | {truncate(title, |
| 119 | </div> |
| 120 | {description ? ( |
| 121 | <div style={{ |
| 122 | {truncate(description, |
| 123 | </div> |
| 124 | ) : null} |
| 125 | </div> |
| 126 | </div> |
| 127 | ) : ( |
| 128 | <div style={{ |
| 129 | {description ? ( |
| 130 | <div style={{ |
| 131 | {truncate(description, |
| 132 | </div> |
| 133 | ) : null} |
| 134 | <div |
| 135 | style={{ |
| 136 | display: "flex", |
| 137 | fontSize: titleSize(title |
| 138 | fontWeight: 600, |
| 139 | letterSpacing: -1, |
| 140 | lineHeight: 1.15, |
| 141 | // long identifiers and file names have no spaces to break on |
| 142 | wordBreak: "break-word", |
| 143 | }} |
| 144 | > |
| 145 | {truncate(title, |
| 146 | </div> |
| 147 | </div> |
| 148 | )} |
| 149 | |
| 150 | <div style={{ |
| 151 | {languages?.length ? |
| 152 | <div style={{ |
| 153 | <div |
| 154 | style={{ |
| 155 | display: "flex", |
| 156 | height: 10, |
| 157 | borderRadius: 10, |
| 158 | overflow: "hidden", |
| 159 | background: HAIRLINE, |
| 160 | }} |
| 161 | > |
| 162 | {languages.map(( |
| 163 | <div |
| 164 | key={language} |
| 165 | style={{ |
| 166 | display: "flex", |
| 167 | width: `${percent} |
| 168 | background: languageColor(language |
| 169 | }} |
| 170 | /> |
| 171 | ))} |
| 172 | </div> |
| 173 | |
| 174 | {/* a card is read at a glance: the long tail would not be legible */ |
| 175 | <div |
| 176 | style={{ |
| 177 | display: "flex", |
| 178 | alignItems: "center", |
| 179 | gap: 28, |
| 180 | fontSize: 22, |
| 181 | color: MUTED, |
| 182 | }} |
| 183 | > |
| 184 | {languages.slice(0 |
| 185 | <div |
| 186 | key={language} |
| 187 | style={{ display: |
| 188 | > |
| 189 | <div |
| 190 | style={{ |
| 191 | display: "flex", |
| 192 | width: 12, |
| 193 | height: 12, |
| 194 | borderRadius: 12, |
| 195 | background: languageColor(language |
| 196 | }} |
| 197 | /> |
| 198 | {language} {percent |
| 199 | </div> |
| 200 | ))} |
| 201 | </div> |
| 202 | </div> |
| 203 | ) : null} |
| 204 | |
| 205 | <div style={{ |
| 206 | <div |
| 207 | style={{ |
| 208 | display: "flex", |
| 209 | alignItems: "center", |
| 210 | justifyContent: "space-between", |
| 211 | fontSize: 26, |
| 212 | color: MUTED, |
| 213 | }} |
| 214 | > |
| 215 | <div style={{ |
| 216 | {stats?.map(( |
| 217 | <div |
| 218 | key={stat.label |
| 219 | style={{ display: |
| 220 | > |
| 221 | <Icon name={ |
| 222 | {stat.label} |
| 223 | </div> |
| 224 | ))} |
| 225 | </div> |
| 226 | |
| 227 | <div |
| 228 | style={{ |
| 229 | display: "flex", |
| 230 | alignItems: "center", |
| 231 | gap: 12, |
| 232 | color: FOREGROUND, |
| 233 | }} |
| 234 | > |
| 235 | <Icon name=" |
| 236 | Ghost |
| 237 | </div> |
| 238 | </div> |
| 239 | </div> |
| 240 | </div>, |
| 241 | size, |
| 242 | ); |
| 243 | } |
| 244 | |
| 245 | /** Keeps a long title on three lines or fewer. `max` caps it when the avatar takes half the row. */ |
| 246 | function titleSize(title: |
| 247 | const size = |
| 248 | title.length <= 22 |
| 249 | ? 82 |
| 250 | : title.length <= |
| 251 | ? 62 |
| 252 | : title.length <= |
| 253 | ? 46 |
| 254 | : 38; |
| 255 | |
| 256 | return Math.min |
| 257 | } |
| 258 | |
| 259 | function truncate(value: |
| 260 | return value.length |
| 261 | } |
| 262 | |
| 263 | /** "1 label", "2 labels", "2 entries". */ |
| 264 | export function plural(value |
| 265 | return `${value} |
| 266 | } |
| 267 | |
| 268 | /** Files, lines and (for a search across repositories) repositories a code search matched. */ |
| 269 | export function codeSearchStats( |
| 270 | files: { |
| 271 | ): OgStat[] |
| 272 | const repositories = |
| 273 | files.flatMap(({ |
| 274 | repository ? [`${ |
| 275 | ), |
| 276 | ); |
| 277 | |
| 278 | return [ |
| 279 | { icon: "fileCode", |
| 280 | { |
| 281 | icon: "code", |
| 282 | label: plural( |
| 283 | files.reduce((sum |
| 284 | "matching line", |
| 285 | ), |
| 286 | }, |
| 287 | ...(repositories.size |
| 288 | ? [ |
| 289 | { |
| 290 | icon: "bookMarked" as |
| 291 | label: plural(repositories |
| 292 | }, |
| 293 | ] |
| 294 | : []), |
| 295 | ]; |
| 296 | } |
| 297 | |
| 298 | /** Keeps both ends of a path readable when the middle has to go. */ |
| 299 | export function shortenPath(path |
| 300 | if (path.length |
| 301 | |
| 302 | const segments = |
| 303 | const tail = |
| 304 | const head = |
| 305 | |
| 306 | return `${head} |
| 307 | ? `${head}/…/ |
| 308 | : `…/${truncate( |
| 309 | } |
| 310 | |
| 311 | /** Satori decodes png, jpeg, gif and svg only; a webp data URI (what the auth provider stores) crashes it, so anything else falls back to the icon. */ |
| 312 | function usable(avatar? |
| 313 | if (!avatar) |
| 314 | return avatar.startsWith |
| 315 | ? true |
| 316 | : /^data:image\/( |
| 317 | } |
| 318 | |
| 319 | /** Satori renders plain SVG, so icons are drawn from their raw geometry. */ |
| 320 | function Icon({ |
| 321 | name, |
| 322 | size, |
| 323 | color = "currentColor" |
| 324 | }: { |
| 325 | name: IconName |
| 326 | size: number |
| 327 | color?: string |
| 328 | }) { |
| 329 | return ( |
| 330 | <svg |
| 331 | width={size} |
| 332 | height={size} |
| 333 | viewBox="0 0 24 24" |
| 334 | fill="none" |
| 335 | stroke={color} |
| 336 | strokeWidth={2} |
| 337 | strokeLinecap="round" |
| 338 | strokeLinejoin="round" |
| 339 | > |
| 340 | {icons[name]. |
| 341 | createElement(tag, { |
| 342 | )} |
| 343 | </svg> |
| 344 | ); |
| 345 | } |