feat: massive refactor

This commit is contained in:
Cory Dransfeldt 2024-10-09 18:10:28 -07:00
parent d424082c95
commit 1880790c05
No known key found for this signature in database
38 changed files with 245 additions and 761 deletions

View file

@ -9,7 +9,6 @@ const fetchAllPosts = async () => {
let posts = []
let page = 0
let fetchMore = true
const uniquePosts = new Set()
while (fetchMore) {
const { data, error } = await supabase
@ -25,44 +24,18 @@ const fetchAllPosts = async () => {
if (data.length < PAGE_SIZE) fetchMore = false
for (const post of data) {
if (uniquePosts.has(post['url'])) continue
uniquePosts.add(post['url'])
posts.push(post)
}
posts = posts.concat(data)
page++
}
return posts
}
const processPosts = async (posts) => {
return Promise.all(posts.map(async post => {
post['artists'] = post['artists'] ? post['artists'].sort((a, b) => a['name'].localeCompare(b['name'])) : null
post['books'] = post['books'] ? post['books'].map(book => ({
title: book['title'],
author: book['author'],
description: book['description'],
url: `/books/${book['isbn']}`,
})).sort((a, b) => a['title'].localeCompare(b['title'])) : null
post['movies'] = post['movies'] ? post['movies'].map(movie => {
movie['url'] = `/watching/movies/${movie['tmdb_id']}`
return movie
}).sort((a, b) => b['year'] - a['year']) : null
post['genres'] = post['genres'] ? post['genres'].sort((a, b) => a['name'].localeCompare(b['name'])) : null
post['shows'] = post['shows'] ? post['shows'].map(show => {
show['url'] = `/watching/shows/${show['tmdb_id']}`
return show
}).sort((a, b) => b['year'] - a['year']) : null
if (post['image']) post['image'] = post['image']['filename_disk']
return post
}))
}
export default async function () {
const posts = await fetchAllPosts()
return await processPosts(posts)
try {
return await fetchAllPosts()
} catch (error) {
console.error('Error fetching and processing posts:', error)
return []
}
}