import client, { previewClient } from "./sanity"; const getUniquePosts = (posts) => { const slugs = new Set(); return posts.filter((post) => { if (slugs.has(post.slug)) { return false; } else { slugs.add(post.slug); return true; } }); }; const postFields = ` _id, name, title, 'date': publishedAt, excerpt, 'slug': slug.current, 'coverImage': mainImage.asset->url, 'author': author->{name, 'picture': image.asset->url}, `; const getClient = (preview) => (preview ? previewClient : client); export async function getPreviewPostBySlug(slug) { const data = await getClient(true).fetch( `*[_type == "post" && slug.current == $slug] | order(dpublishedAtate desc){ ${postFields} body }`, { slug } ); return data[0]; } export async function getAllPostsWithSlug() { const data = await client.fetch(`*[_type == "post"]{ 'slug': slug.current }`); return data; } export async function getAllPostsForHome(preview) { const results = await getClient(preview) .fetch(`*[_type == "post"] | order(date desc, _updatedAt desc){ ${postFields} }`); return getUniquePosts(results); } /* Relación Post-Author */ export async function getPostAndAuthors() { const data = await client.fetch( `*[_type == "post"]{title, publishedAt,'author': *[_type == "author" && _id == ^.author._ref && genre== 'comedia']{genre,name, 'picture': image.asset->url} }` ); return data; } export async function getPostAndMorePosts(slug, preview) { const curClient = getClient(preview); const [post, morePosts] = await Promise.all([ curClient .fetch( `*[_type == "post" && slug.current == $slug] | order(_updatedAt desc) { ${postFields} body, 'comments': *[_type == "comment" && post._ref == ^._id]{name, text, _createdAt} }`, { slug } ) .then((res) => res?.[0]), curClient.fetch( `*[_type == "post" && slug.current != $slug] | order(publishedAt desc, _updatedAt desc){ ${postFields} body, }[0...2]`, { slug } ), ]); return { post, morePosts: getUniquePosts(morePosts) }; }