| 1 | import { ImageResponse } from "next/og"; |
| 2 | import { type ReactNode, |
| 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 dark lavender theme in globals.css is spelled out. |
| 12 | const ACCENT = " |
| 13 | const CARD = " |
| 14 | const FOREGROUND = " |
| 15 | const MUTED = " |
| 16 | const HAIRLINE = " |
| 17 | const PRIMARY = " |
| 18 | |
| 19 | export type OgStat |
| 20 | export type OgLanguage |
| 21 | export type OgBadge |
| 22 | |
| 23 | /** The landing page's hero: a card rising out of a lavender panel. */ |
| 24 | export function ogCard({ |
| 25 | eyebrow, |
| 26 | icon, |
| 27 | avatar, |
| 28 | badge, |
| 29 | title, |
| 30 | description, |
| 31 | stats, |
| 32 | languages, |
| 33 | }: { |
| 34 | eyebrow: string |
| 35 | /** Sits before the eyebrow, in place of its dot. */ |
| 36 | icon?: IconName |
| 37 | /** Drawn large beside the title, with the description under it. */ |
| 38 | avatar?: string |
| 39 | badge?: OgBadge |
| 40 | title: string |
| 41 | description?: string |
| 42 | stats?: OgStat |
| 43 | /** Drawn as a bar above the footer, in the order the API returns. */ |
| 44 | languages?: OgLanguage |
| 45 | }) { |
| 46 | return new ImageResponse |
| 47 | <div |
| 48 | style={{ |
| 49 | width: "100%", |
| 50 | height: "100%", |
| 51 | display: "flex", |
| 52 | background: ACCENT, |
| 53 | padding: "56px 56px 0", |
| 54 | fontFamily: "sans-serif", |
| 55 | }} |
| 56 | > |
| 57 | <div |
| 58 | style={{ |
| 59 | display: "flex", |
| 60 | flex: 1, |
| 61 | flexDirection: "column", |
| 62 | justifyContent: "space-between", |
| 63 | padding: "52px 60px 56px", |
| 64 | background: CARD, |
| 65 | color: FOREGROUND, |
| 66 | border: `1px solid ${HAIRLINE |
| 67 | borderBottom: "none", |
| 68 | borderRadius: "24px 24px 0 0", |
| 69 | boxShadow: "0 30px 60px -30px rgba(0, 0, 0, 0.6)", |
| 70 | }} |
| 71 | > |
| 72 | <div |
| 73 | style={{ |
| 74 | display: "flex", |
| 75 | alignItems: "center", |
| 76 | justifyContent: "space-between", |
| 77 | }} |
| 78 | > |
| 79 | <div |
| 80 | style={{ |
| 81 | display: "flex", |
| 82 | alignItems: "center", |
| 83 | gap: 14, |
| 84 | fontSize: 22, |
| 85 | letterSpacing: 2, |
| 86 | textTransform: "uppercase", |
| 87 | color: PRIMARY, |
| 88 | }} |
| 89 | > |
| 90 | {icon ? ( |
| 91 | <Icon name={ |
| 92 | ) : ( |
| 93 | <Dot color={ |
| 94 | )} |
| 95 | {truncate(eyebrow, |
| 96 | </div> |
| 97 | {badge ? <Badge |
| 98 | </div> |
| 99 | |
| 100 | {usable(avatar) |
| 101 | <div style={{ |
| 102 | <img |
| 103 | src={avatar} |
| 104 | width={170} |
| 105 | height={170} |
| 106 | alt="" |
| 107 | style={{ borderRadius: |
| 108 | /> |
| 109 | <div style={{ |
| 110 | <Title text={ |
| 111 | {description ? ( |
| 112 | <div style={{ |
| 113 | {truncate(plain(description |
| 114 | </div> |
| 115 | ) : null} |
| 116 | </div> |
| 117 | </div> |
| 118 | ) : ( |
| 119 | <div style={{ |
| 120 | <Title text={ |
| 121 | {description ? ( |
| 122 | <div |
| 123 | style={{ |
| 124 | display: "flex", |
| 125 | fontSize: 28, |
| 126 | lineHeight: 1.4, |
| 127 | color: MUTED, |
| 128 | }} |
| 129 | > |
| 130 | {truncate(plain(description |
| 131 | </div> |
| 132 | ) : null} |
| 133 | </div> |
| 134 | )} |
| 135 | |
| 136 | <div style={{ |
| 137 | {languages?.length ? |
| 138 | <div |
| 139 | style={{ |
| 140 | display: "flex", |
| 141 | height: 8, |
| 142 | borderRadius: 8, |
| 143 | overflow: "hidden", |
| 144 | background: HAIRLINE, |
| 145 | }} |
| 146 | > |
| 147 | {languages.map(( |
| 148 | <div |
| 149 | key={language} |
| 150 | style={{ |
| 151 | display: "flex", |
| 152 | width: `${percent} |
| 153 | background: languageColor(language |
| 154 | }} |
| 155 | /> |
| 156 | ))} |
| 157 | </div> |
| 158 | ) : null} |
| 159 | |
| 160 | <div |
| 161 | style={{ |
| 162 | display: "flex", |
| 163 | alignItems: "center", |
| 164 | justifyContent: "space-between", |
| 165 | gap: 24, |
| 166 | }} |
| 167 | > |
| 168 | <div |
| 169 | style={{ |
| 170 | display: "flex", |
| 171 | gap: 10, |
| 172 | minWidth: 0, |
| 173 | overflow: "hidden", |
| 174 | }} |
| 175 | > |
| 176 | {stats?.map(( |
| 177 | <Chip key={ |
| 178 | <Icon name={ |
| 179 | {truncate(stat.label |
| 180 | </Chip> |
| 181 | ))} |
| 182 | {/* a card is read at a glance: the long tail would not be legible */ |
| 183 | {languages?.slice(0 |
| 184 | <Chip key={ |
| 185 | <Dot color={ |
| 186 | {language} {percent |
| 187 | </Chip> |
| 188 | ))} |
| 189 | </div> |
| 190 | |
| 191 | <div |
| 192 | style={{ |
| 193 | display: "flex", |
| 194 | alignItems: "center", |
| 195 | gap: 10, |
| 196 | fontSize: 28, |
| 197 | fontWeight: 600, |
| 198 | letterSpacing: -0.5, |
| 199 | flexShrink: 0, |
| 200 | }} |
| 201 | > |
| 202 | <Icon name=" |
| 203 | Ghost |
| 204 | </div> |
| 205 | </div> |
| 206 | </div> |
| 207 | </div> |
| 208 | </div>, |
| 209 | size, |
| 210 | ); |
| 211 | } |
| 212 | |
| 213 | function Badge({ badge |
| 214 | const color = |
| 215 | return ( |
| 216 | <div |
| 217 | style={{ |
| 218 | display: "flex", |
| 219 | alignItems: "center", |
| 220 | gap: 10, |
| 221 | padding: "8px 18px", |
| 222 | borderRadius: 999, |
| 223 | border: `1px solid ${HAIRLINE |
| 224 | fontSize: 20, |
| 225 | fontWeight: 600, |
| 226 | letterSpacing: 1.5, |
| 227 | textTransform: "uppercase", |
| 228 | color, |
| 229 | }} |
| 230 | > |
| 231 | <Dot color={ |
| 232 | {truncate(badge.label |
| 233 | </div> |
| 234 | ); |
| 235 | } |
| 236 | |
| 237 | function Title({ text |
| 238 | return ( |
| 239 | <div |
| 240 | style={{ |
| 241 | display: "flex", |
| 242 | fontSize: titleSize(text |
| 243 | fontWeight: 600, |
| 244 | letterSpacing: "-0.045em", |
| 245 | lineHeight: 1.05, |
| 246 | // long identifiers and file names have no spaces to break on |
| 247 | wordBreak: "break-word", |
| 248 | }} |
| 249 | > |
| 250 | {truncate(text, |
| 251 | </div> |
| 252 | ); |
| 253 | } |
| 254 | |
| 255 | /** The rounded-full chips from the landing page's feature list. */ |
| 256 | function Chip({ children |
| 257 | return ( |
| 258 | <div |
| 259 | style={{ |
| 260 | display: "flex", |
| 261 | alignItems: "center", |
| 262 | gap: 10, |
| 263 | padding: "8px 18px", |
| 264 | borderRadius: 999, |
| 265 | border: `1px solid ${HAIRLINE |
| 266 | fontSize: 22, |
| 267 | color: MUTED, |
| 268 | }} |
| 269 | > |
| 270 | {children} |
| 271 | </div> |
| 272 | ); |
| 273 | } |
| 274 | |
| 275 | function Dot({ color |
| 276 | return ( |
| 277 | <div |
| 278 | style={{ |
| 279 | display: "flex", |
| 280 | width: 12, |
| 281 | height: 12, |
| 282 | borderRadius: 12, |
| 283 | background: color, |
| 284 | }} |
| 285 | /> |
| 286 | ); |
| 287 | } |
| 288 | |
| 289 | /** Keeps a long title on three lines or fewer. `max` caps it when the avatar takes half the row. */ |
| 290 | function titleSize(title: |
| 291 | const size = |
| 292 | title.length <= 22 |
| 293 | ? 82 |
| 294 | : title.length <= |
| 295 | ? 62 |
| 296 | : title.length <= |
| 297 | ? 46 |
| 298 | : 38; |
| 299 | |
| 300 | return Math.min |
| 301 | } |
| 302 | |
| 303 | /** Issue and release bodies are markdown; a card shows the words, not the syntax. */ |
| 304 | export function plain(markdown |
| 305 | return markdown |
| 306 | .replace(/!?\[ |
| 307 | .replace(/^\s{0,3} |
| 308 | .replace(/[*_~`] |
| 309 | .replace(/\s+/ |
| 310 | .trim(); |
| 311 | } |
| 312 | |
| 313 | function truncate(value: |
| 314 | return value.length |
| 315 | } |
| 316 | |
| 317 | /** "1 label", "2 labels", "2 entries". */ |
| 318 | export function plural(value |
| 319 | return `${value} |
| 320 | } |
| 321 | |
| 322 | /** Files, lines and (for a search across repositories) repositories a code search matched. */ |
| 323 | export function codeSearchStats( |
| 324 | files: { |
| 325 | ): OgStat[] |
| 326 | const repositories = |
| 327 | files.flatMap(({ |
| 328 | repository ? [`${ |
| 329 | ), |
| 330 | ); |
| 331 | |
| 332 | return [ |
| 333 | { icon: "fileCode", |
| 334 | { |
| 335 | icon: "code", |
| 336 | label: plural( |
| 337 | files.reduce((sum |
| 338 | "matching line", |
| 339 | ), |
| 340 | }, |
| 341 | ...(repositories.size |
| 342 | ? [ |
| 343 | { |
| 344 | icon: "bookMarked" as |
| 345 | label: plural(repositories |
| 346 | }, |
| 347 | ] |
| 348 | : []), |
| 349 | ]; |
| 350 | } |
| 351 | |
| 352 | /** Keeps both ends of a path readable when the middle has to go. */ |
| 353 | export function shortenPath(path |
| 354 | if (path.length |
| 355 | |
| 356 | const segments = |
| 357 | const tail = |
| 358 | const head = |
| 359 | |
| 360 | return `${head} |
| 361 | ? `${head}/…/ |
| 362 | : `…/${truncate( |
| 363 | } |
| 364 | |
| 365 | /** 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. */ |
| 366 | function usable(avatar? |
| 367 | if (!avatar) |
| 368 | return avatar.startsWith |
| 369 | ? true |
| 370 | : /^data:image\/( |
| 371 | } |
| 372 | |
| 373 | /** Satori renders plain SVG, so icons are drawn from their raw geometry. */ |
| 374 | function Icon({ |
| 375 | name, |
| 376 | size, |
| 377 | color = "currentColor" |
| 378 | }: { |
| 379 | name: IconName |
| 380 | size: number |
| 381 | color?: string |
| 382 | }) { |
| 383 | return ( |
| 384 | <svg |
| 385 | width={size} |
| 386 | height={size} |
| 387 | viewBox="0 0 24 24" |
| 388 | fill="none" |
| 389 | stroke={color} |
| 390 | strokeWidth={2} |
| 391 | strokeLinecap="round" |
| 392 | strokeLinejoin="round" |
| 393 | > |
| 394 | {icons[name]. |
| 395 | createElement(tag, { |
| 396 | )} |
| 397 | </svg> |
| 398 | ); |
| 399 | } |