Skip to content

Instantly share code, notes, and snippets.

@bm-vs
Last active July 17, 2023 14:07
Show Gist options
  • Save bm-vs/5ee065b43ea9f2ae964ac2cbc1b266c0 to your computer and use it in GitHub Desktop.
Save bm-vs/5ee065b43ea9f2ae964ac2cbc1b266c0 to your computer and use it in GitHub Desktop.
ArticleListAll RSC
'use client';
// imports
interface Props {
section: ArticleListAllType;
articlesQuery: string;
isDraft: boolean;
}
export const ArticleListAllDraft: FC<Props> = ({section, articlesQuery, isDraft}) => {
return (
<PreviewSuspense fallback={<FullPageSpinner />}>
<Preview section={section} articlesQuery={articlesQuery} isDraft={isDraft} />
</PreviewSuspense>
);
};
const Preview: FC<Props> = ({section, articlesQuery, isDraft}) => {
const articles = usePreview(null, articlesQuery);
return <ArticleListAllRender section={section} articles={articles} isDraft={isDraft} />;
};
// imports
interface Props {
section: ArticleListAllType;
isDraft: boolean;
}
export const ArticleListAll: FC<Props> = ({section, isDraft}) => {
return isDraft ? (
<ArticleListAllDraft section={section} articlesQuery={articlesQuery} isDraft={isDraft} />
) : (
<ArticleListAllServer section={section} articlesQuery={articlesQuery} isDraft={isDraft} />
);
};
/**
* Fetches all articles
*/
const articlesQuery = groq`
*[_type == "article" && defined(slug.current) && !(_id in path('drafts.**'))]{
...
}
`;
// imports
interface Props {
section: ArticleListAllType;
articles: ArticleType[];
isDraft: boolean;
}
export const ArticleListAllRender: FC<Props> = ({section, articles, isDraft}) => {
const {theme, title} = section;
return (
<div
className={clsx(
'flex',
'items-center',
'justify-center',
'p-5',
)}
>
{/* render whatever */}
</div>
);
};
// imports
interface Props {
section: ArticleListAllType;
articlesQuery: string;
isDraft: boolean;
}
export const ArticleListAllServer = async ({section, articlesQuery, isDraft}: Props) => {
const articles = await sanityFetch(articlesQuery, z.array(articleSchema));
return <ArticleListAllRender section={section} articles={articles} isDraft={isDraft} />;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment