|
| 1 | +import { html } from 'satori-html'; |
| 2 | +import colors from 'tailwindcss/colors'; |
| 3 | + |
| 4 | +import { getRandomElementFromArray, limitString } from '@/utils/strings'; |
| 5 | + |
| 6 | +export interface TemplateProps { |
| 7 | + title: string; |
| 8 | + heroImageUrl: string; |
| 9 | + avatarImageUrl: string; |
| 10 | + siteUrl: string; |
| 11 | +} |
| 12 | + |
| 13 | +const clrs = ['gray', 'indigo', 'yellow', 'blue', 'cyan', 'lime', 'sky']; |
| 14 | +const directions = ['to right', 'to bottom', '45deg']; |
| 15 | +const shades = [50, 100, 200]; |
| 16 | +const rnd = getRandomElementFromArray; |
| 17 | + |
| 18 | +const getRandomGradient = () => |
| 19 | + `background: linear-gradient(${rnd(directions)}, ${colors[rnd(clrs)][rnd(shades)]}, ${colors[rnd(clrs)][rnd(shades)]})`; |
| 20 | + |
| 21 | +const gradients = [ |
| 22 | + `background: linear-gradient(to right, ${colors.gray[100]}, ${colors.gray[300]})`, |
| 23 | + `background: linear-gradient(to right, ${colors.indigo[200]}, ${colors.yellow[100]})`, |
| 24 | + `background: linear-gradient(to right, ${colors.blue[100]}, ${colors.blue[200]})`, |
| 25 | + `background: linear-gradient(to right, ${colors.fuchsia[200]}, ${colors.cyan[200]})`, |
| 26 | + `background: linear-gradient(to right, ${colors.teal[200]}, ${colors.lime[100]})`, |
| 27 | + `background: linear-gradient(to right, ${colors.sky[200]}, ${colors.sky[100]})`, |
| 28 | + `background: linear-gradient(to right, ${colors.sky[200]}, ${colors.cyan[100]})`, |
| 29 | +]; |
| 30 | + |
| 31 | +const templateHtml = ({ title, heroImageUrl, avatarImageUrl, siteUrl }: TemplateProps) => { |
| 32 | + // 2 rows - max 30 chars |
| 33 | + // 1 row - max 18 chars |
| 34 | + const isLongSiteUrl = siteUrl.length > 17; |
| 35 | + |
| 36 | + // max 6 rows x 10-15 chars |
| 37 | + const limitedTitle = limitString(title, 70); |
| 38 | + |
| 39 | + // const randomGradient = getRandomElementFromArray(gradients); |
| 40 | + const randomGradient = getRandomGradient(); |
| 41 | + |
| 42 | + return html` |
| 43 | + <div class="flex p-8 h-full" style="${randomGradient}"> |
| 44 | + <div class="flex w-full flex-row justify-between text-slate-900"> |
| 45 | + <!-- left column --> |
| 46 | + <div class="w-[550px] flex flex-col justify-between mr-6"> |
| 47 | + <!-- title --> |
| 48 | + <div class="flex flex-grow text-6xl font-semibold mb-4">${limitedTitle}</div> |
| 49 | +
|
| 50 | + <!-- avatar and site --> |
| 51 | + <div class="flex items-center ${isLongSiteUrl ? 'flex-col justify-end items-start' : ''}"> |
| 52 | + <img |
| 53 | + src=${avatarImageUrl} |
| 54 | + alt="Nemanja Mitic" |
| 55 | + width="120" |
| 56 | + height="120" |
| 57 | + class="rounded-full mr-8" |
| 58 | + /> |
| 59 | + <div class="flex items-center ${isLongSiteUrl ? 'mt-4 text-3xl' : 'text-4xl'}"> |
| 60 | + <div>${siteUrl}</div> |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + </div> |
| 64 | +
|
| 65 | + <!-- right column --> |
| 66 | + <div class="w-[550px] flex items-center"> |
| 67 | + <img src="${heroImageUrl}" class="h-full w-full rounded-md" style="object-fit: cover" /> |
| 68 | + </div> |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + `; |
| 72 | +}; |
| 73 | + |
| 74 | +export default templateHtml; |
0 commit comments