From 404a940c8c6f50c72e059155db156e085724f1c4 Mon Sep 17 00:00:00 2001 From: Cory Dransfeldt Date: Mon, 13 May 2024 13:37:17 -0700 Subject: [PATCH] chore: return multiple charts if available --- package.json | 2 +- src/_data/weeklyArtistChart.js | 48 ++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 2f571161..67e35d6a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "coryd.dev", - "version": "14.4.0", + "version": "14.5.0", "description": "The source for my personal site. Built using 11ty.", "type": "module", "scripts": { diff --git a/src/_data/weeklyArtistChart.js b/src/_data/weeklyArtistChart.js index 32154124..4520c0d7 100644 --- a/src/_data/weeklyArtistChart.js +++ b/src/_data/weeklyArtistChart.js @@ -46,20 +46,22 @@ export default async function() { const endOfWeekSeconds = endOfWeek.toSeconds() const weekNumber = now.toFormat('kkkk-WW') - let { data: existingRecord } = await supabase + let { data: recentCharts } = await supabase .from('weekly_charts') .select('*') - .eq('week', weekNumber) - .single(); + .order('date', { ascending: false }) + .limit(10); - if (existingRecord) { - const formattedData = formatData(JSON.parse(existingRecord['data'])) - return [{ - title: formattedData['content'], - description: formattedData['description'], - url: `https://coryd.dev/now?ts=${existingRecord['week']}#artists`, - date: existingRecord['date'] - }] + if (recentCharts.some(chart => chart['week'] === weekNumber)) { + return recentCharts.map(chart => { + const formattedData = formatData(JSON.parse(chart['data'])) + return { + title: formattedData['content'], + description: formattedData['description'], + url: `https://coryd.dev/now?ts=${chart['week']}#artists`, + date: chart['date'] + } + }) } // Fetch the listens data for the past week @@ -99,12 +101,24 @@ export default async function() { .insert([{ week: weekNumber, date: now.toISODate(), data: JSON.stringify(topArtists) }]) if (insertError) throw insertError const formattedData = formatData(topArtists) - return [{ - title: formattedData['content'], - description: formattedData['description'], - url: `https://coryd.dev/now?ts=${weekNumber}#artists`, - date: now.toISODate() - }] + const recentChartData = recentCharts.map(chart => { + const formattedData = formatData(JSON.parse(chart['data'])) + return { + title: formattedData['content'], + description: formattedData['description'], + url: `https://coryd.dev/now?ts=${chart['week']}#artists`, + date: chart['date'] + } + }) + return [ + { + title: formattedData['content'], + description: formattedData['description'], + url: `https://coryd.dev/now?ts=${weekNumber}#artists`, + date: now.toISODate() + }, + ...recentChartData + ] } catch (error) { console.error('Error:', error.message) }