feat: programmatically generate year book pages

This commit is contained in:
Cory Dransfeldt 2024-07-15 13:52:44 -07:00
parent eed026a6e8
commit b50a4670d2
No known key found for this signature in database
18 changed files with 92 additions and 101 deletions

View file

@ -1,7 +1,6 @@
import { createClient } from '@supabase/supabase-js'
const SUPABASE_URL = process.env.SUPABASE_URL
const SUPABASE_KEY = process.env.SUPABASE_KEY
const { SUPABASE_URL, SUPABASE_KEY } = process.env
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)
const PAGE_SIZE = 1000
@ -17,19 +16,18 @@ const fetchTagsForBook = async (bookId) => {
return []
}
return data.map(bt => bt.tags.name)
return data.map(bt => bt['tags']['name'])
}
async function fetchAllBooks() {
let books = []
let from = 0
let to = PAGE_SIZE - 1
while (true) {
const { data, error } = await supabase
.from('books')
.select(`*, art(filename_disk)`)
.range(from, to)
.range(from, from + PAGE_SIZE - 1)
if (error) {
console.error('Error fetching data from Supabase:', error)
@ -37,7 +35,7 @@ async function fetchAllBooks() {
}
for (const book of data) {
book.tags = await fetchTagsForBook(book['id'])
book['tags'] = await fetchTagsForBook(book['id'])
}
books = books.concat(data)
@ -45,20 +43,17 @@ async function fetchAllBooks() {
if (data.length < PAGE_SIZE) break
from += PAGE_SIZE
to += PAGE_SIZE
}
return books
}
export default async function () {
const books = await fetchAllBooks()
return books.map(book => ({
return books.map(book => {
const dateFinished = new Date(book['date_finished'])
const year = dateFinished.getUTCFullYear()
return {
title: book['title'],
author: book['author'] || '',
review: book['review'],
rating: book['star_rating'] !== 'unrated' ? book['star_rating'] : '',
favorite: book['favorite'],
description: book['description'],
image: `/${book?.['art']?.['filename_disk']}`,
url: `/books/${book['isbn']}`,
@ -68,5 +63,25 @@ export default async function () {
tags: book['tags'],
isbn: book['isbn'],
type: 'book',
}))
year,
}
})
}
const sortBooksByYear = (books) => {
const years = {}
books.forEach(book => {
const year = book['year']
if (!years[year]) {
years[year] = { value: year, data: [book] }
} else {
years[year]['data'].push(book)
}
})
return Object.values(years).filter(year => year.value > 2020)
}
export default async function () {
const books = await fetchAllBooks()
return { all: books, years: sortBooksByYear(books) }
}