chore: return multiple charts if available

This commit is contained in:
Cory Dransfeldt 2024-05-13 13:37:17 -07:00
parent 2a486efa97
commit 404a940c8c
No known key found for this signature in database
2 changed files with 32 additions and 18 deletions

View file

@ -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": {

View file

@ -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)
}