Update getContentItems to return total count and items

The changes involved modifying the implementation of `getContentItems` across multiple files, specifically in the CMS-related codebase. This method now returns an object containing the total count of items and the items themselves. The updates also included necessary adjustments in the code where `getContentItems` is called to accommodate the new structure of the returned result.
This commit is contained in:
giancarlo
2024-04-10 16:29:19 +08:00
parent 44373c0372
commit f94557e333
8 changed files with 89 additions and 71 deletions

View File

@@ -23,7 +23,7 @@ async function BlogPage({ searchParams }: { searchParams: { page: string } }) {
const limit = 10;
const offset = page * limit;
const posts = await cms.getContentItems({
const { items: posts, total } = await cms.getContentItems({
collection: 'posts',
limit,
offset,

View File

@@ -5,12 +5,10 @@ import { DocsNavigation } from '~/(marketing)/docs/_components/docs-navigation';
async function DocsLayout({ children }: React.PropsWithChildren) {
const cms = await createCmsClient();
const pages = await cms.getContentItems({
const { items: pages } = await cms.getContentItems({
collection: 'documentation',
});
console.log(pages);
return (
<div className={'flex'}>
<DocsNavigation pages={buildDocumentationTree(pages)} />

View File

@@ -18,12 +18,12 @@ async function DocsPage() {
const client = await createCmsClient();
const { t } = await createI18nServerInstance();
const docs = await client.getContentItems({
const { items } = await client.getContentItems({
collection: 'documentation',
});
// Filter out any docs that have a parentId, as these are children of other docs
const cards = docs.filter((item) => !item.parentId);
const cards = items.filter((item) => !item.parentId);
return (
<PageBody>
@@ -33,7 +33,7 @@ async function DocsPage() {
subtitle={t('marketing:documentationSubtitle')}
/>
<div className={'flex flex-col items-center'}>
<div className={'container mx-auto flex flex-col items-center'}>
<DocsCards cards={cards} />
</div>
</div>

View File

@@ -9,14 +9,14 @@ invariant(appConfig.url, 'No NEXT_PUBLIC_SITE_URL environment variable found');
export async function GET() {
const urls = getSiteUrls();
const client = await createCmsClient();
const contentItems = await client.getContentItems();
const items = await getAllItems();
return getServerSideSitemap([
...urls,
...contentItems.map((item) => {
...items.map((path) => {
return {
loc: new URL(item.url, appConfig.url).href,
loc: new URL(path, appConfig.url).href,
lastmod: new Date().toISOString(),
};
}),
@@ -33,3 +33,23 @@ function getSiteUrls() {
};
});
}
async function getAllItems() {
const client = await createCmsClient();
const posts = client
.getContentItems({
collection: 'posts',
})
.then((response) => response.items)
.then((posts) => posts.map((post) => `/blog/${post.slug}`));
const docs = client
.getContentItems({
collection: 'documentation',
})
.then((response) => response.items)
.then((docs) => docs.map((doc) => `/docs/${doc.slug}`));
return Promise.all([posts, docs]).then((items) => items.flat());
}