From 5262079f20db00571e98eafb396884d99bf3410e Mon Sep 17 00:00:00 2001 From: Andy Hattemer Date: Sun, 10 Nov 2024 21:02:16 -0500 Subject: [PATCH 1/3] feat: sparkline img api for emails --- src/pages/api/sparkline.jsx | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/pages/api/sparkline.jsx diff --git a/src/pages/api/sparkline.jsx b/src/pages/api/sparkline.jsx new file mode 100644 index 0000000000..1867880606 --- /dev/null +++ b/src/pages/api/sparkline.jsx @@ -0,0 +1,54 @@ +import { ImageResponse } from 'next/og'; + +export const config = { + runtime: 'edge', +}; + +export default async function handler(req) { + const { searchParams } = req.nextUrl; + const series = searchParams.get('series')?.split(',') || [1, 1]; + const w = searchParams.get('w') || 140; + const h = searchParams.get('h') || 32; + const normalizer = Math.max(...series); + + const fade = 10; + let alpha = 1; + const paths = []; + for (let i = 0; i < fade; i++) { + paths.push( + ( + `${acc + }${k == 0 ? 'M' : 'L'}${k * (w / (series.length - 1))} ${h + i * 2 - (v / normalizer) * h} ` + ), '')} + /> + ); + alpha = i === 0 ? 0.25 : alpha / 1.25; + } + + return new ImageResponse( + ( +
+ + {paths} + +
+ ), + { + width: w, + height: h, + } + ); +} From 340ba5320c928580d2401a97332abb68e40909e9 Mon Sep 17 00:00:00 2001 From: Andy Hattemer Date: Sun, 10 Nov 2024 21:04:53 -0500 Subject: [PATCH 2/3] fix: parse w, h --- src/pages/api/sparkline.jsx | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/pages/api/sparkline.jsx b/src/pages/api/sparkline.jsx index 1867880606..1ddf7bb56b 100644 --- a/src/pages/api/sparkline.jsx +++ b/src/pages/api/sparkline.jsx @@ -7,8 +7,8 @@ export const config = { export default async function handler(req) { const { searchParams } = req.nextUrl; const series = searchParams.get('series')?.split(',') || [1, 1]; - const w = searchParams.get('w') || 140; - const h = searchParams.get('h') || 32; + const w = parseInt(searchParams.get('w'), 10) || 140; + const h = parseInt(searchParams.get('h'), 10) || 32; const normalizer = Math.max(...series); const fade = 10; @@ -20,10 +20,13 @@ export default async function handler(req) { key={i} stroke={`rgba(0,191,131,${alpha})`} strokeWidth={i === 0 ? 1 : 2} - d={series.reduce((acc, v, k) => ( - `${acc - }${k == 0 ? 'M' : 'L'}${k * (w / (series.length - 1))} ${h + i * 2 - (v / normalizer) * h} ` - ), '')} + d={series.reduce( + (acc, v, k) => + `${ + acc + }${k == 0 ? 'M' : 'L'}${k * (w / (series.length - 1))} ${h + i * 2 - (v / normalizer) * h} `, + '' + )} /> ); alpha = i === 0 ? 0.25 : alpha / 1.25; From 2e7d781bbe2cd3aff9dacad70d3bb0dc68678896 Mon Sep 17 00:00:00 2001 From: Andy Hattemer Date: Sun, 10 Nov 2024 21:35:59 -0500 Subject: [PATCH 3/3] fix: grayscale --- src/pages/api/sparkline.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/api/sparkline.jsx b/src/pages/api/sparkline.jsx index 1ddf7bb56b..87aca377de 100644 --- a/src/pages/api/sparkline.jsx +++ b/src/pages/api/sparkline.jsx @@ -18,7 +18,7 @@ export default async function handler(req) { paths.push(