import {createClient} from '@/lib/supabase/client' import {QueryData} from '@supabase/supabase-js' export async function getComments(slug: string) { try { const supabase = await createClient() const commentsQuery = supabase .from('comments') .select(` id, content, published, created_at, parent_id, user:users!inner(id, name, image), article:articles!inner(id, title, slug) `) .eq('article.slug', slug) .eq('published', true) .order('created_at', {ascending: false}) type Comments = QueryData const {data: comments, error} = await commentsQuery const commentMap = comments?.reduce<{ [key: number]: Comment }>((acc, comment) => { // @ts-ignore acc[comment.id] = {...comment, replies: []} return acc }, {}); return comments?.reduce((nested, comment) => { if (typeof commentMap !== 'undefined') { if (comment.parent_id !== null) { const parent = commentMap[comment.parent_id]; if (parent) { // @ts-ignore parent.replies?.push(commentMap[comment.id]) // @ts-ignore parent.replies?.sort((a, b) => a.id - b.id) } } else { nested.push(commentMap[comment.id]); } } return nested; }, []) } catch (error) { console.error(error) } }