feat: massive refactor
This commit is contained in:
parent
d424082c95
commit
1880790c05
38 changed files with 245 additions and 761 deletions
|
@ -5,24 +5,6 @@ const SUPABASE_KEY = process.env.SUPABASE_KEY
|
|||
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)
|
||||
const PAGE_SIZE = 1000
|
||||
|
||||
const fetchAllTags = async () => {
|
||||
const { data, error } = await supabase
|
||||
.from('links_tags')
|
||||
.select('links_id, tags(name)')
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching all tags from Supabase:', error)
|
||||
return {}
|
||||
}
|
||||
|
||||
return data.reduce((acc, { links_id, tags }) => {
|
||||
if (!tags || !tags.name) return acc
|
||||
if (!acc[links_id]) acc[links_id] = []
|
||||
acc[links_id].push(tags['name'])
|
||||
return acc
|
||||
}, {})
|
||||
}
|
||||
|
||||
const fetchAllLinks = async () => {
|
||||
let links = []
|
||||
let page = 0
|
||||
|
@ -30,9 +12,8 @@ const fetchAllLinks = async () => {
|
|||
|
||||
while (fetchMore) {
|
||||
const { data, error } = await supabase
|
||||
.from('links')
|
||||
.select('*, authors (name, url, mastodon)')
|
||||
.order('date', { ascending: false })
|
||||
.from('optimized_links')
|
||||
.select('*')
|
||||
.range(page * PAGE_SIZE, (page + 1) * PAGE_SIZE - 1)
|
||||
|
||||
if (error) {
|
||||
|
@ -49,15 +30,11 @@ const fetchAllLinks = async () => {
|
|||
return links
|
||||
}
|
||||
|
||||
const processLinks = (links, tagsByLinkId) => {
|
||||
return links.map(link => {
|
||||
link['tags'] = tagsByLinkId[link['id']] || []
|
||||
link['type'] = 'link'
|
||||
return link
|
||||
})
|
||||
}
|
||||
|
||||
export default async function () {
|
||||
const [links, tagsByLinkId] = await Promise.all([fetchAllLinks(), fetchAllTags()])
|
||||
return processLinks(links, tagsByLinkId)
|
||||
try {
|
||||
return await fetchAllLinks()
|
||||
} catch (error) {
|
||||
console.error('Error fetching and processing links:', error)
|
||||
return []
|
||||
}
|
||||
}
|
Reference in a new issue