Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: remove getting started #69

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 21 additions & 58 deletions src/app/(routes)/blog/_components/posts.tsx
Original file line number Diff line number Diff line change
@@ -1,64 +1,27 @@
import { Text } from "@/app/_components/text";
import ArticleSnippetCard from "./article-snippet-card";
import ArticleFullCard from "./article-full-card";
import {
BlogPostWithRelevantEntries,
retrieveContentfulPublishedSlugs,
} from "@/app/_lib/contentful";
import { retrieveContentfulPublishedSlugs } from "@/app/_lib/contentful";
import { SearchParams } from "../page";
import { useState } from "react";

export async function Posts({
getStartedSnippets,
recentArticleSlugs,
searchParams,
}: {
searchParams: SearchParams;
recentArticleSlugs: string[];
getStartedSnippets: (BlogPostWithRelevantEntries | undefined)[];
}) {
export async function Posts({ searchParams }: { searchParams: SearchParams }) {
const search = searchParams["search"];
if (!search) {
return (
<>
<div className="flex w-full flex-col gap-4">
<Text variant="body" className="text-grey-400">
Get started with Across
</Text>
<div className="w-full overflow-x-scroll scrollbar-hide">
<div className="grid w-[1024px] grid-cols-3 gap-5 md:w-full">
{getStartedSnippets.slice(0, 3).map((snippet) => (
<div className="w-full" key={snippet?.fields.slug}>
<ArticleSnippetCard article={snippet!} />
</div>
))}
</div>
</div>
</div>
<div className="flex w-full flex-col gap-4">
<Text variant="body" className="text-grey-400">
Most recent articles
</Text>
{recentArticleSlugs.map((slug) => (
<ArticleFullCard key={slug} slug={slug} />
))}
</div>
</>
);
} else {
const searchedSlugs = await retrieveContentfulPublishedSlugs({
query: search,
});
return searchedSlugs.length === 0 ? (
<h2 className="text-2xl my-auto flex-1 text-grey-400">No results.</h2>
) : (
<div className="flex w-full flex-col gap-4">
<Text variant="body" className="text-grey-400">
Search results
</Text>
{searchedSlugs.map((slug) => (
<ArticleFullCard key={slug} slug={slug} />
))}
</div>
);
}
const isSearch = Boolean(!!search);
const pageLength = 6;
const searchedSlugs = await retrieveContentfulPublishedSlugs({
query: search ? decodeURI(search) : undefined,
sortByRecent: true,
limit: pageLength,
});

return (
<div className="flex w-full flex-col gap-4">
<Text variant="body" className="text-grey-400">
{isSearch ? "Search results" : "Most recent articles"}
</Text>
{searchedSlugs.map((slug) => (
<ArticleFullCard key={slug} slug={slug} />
))}
</div>
);
}
27 changes: 3 additions & 24 deletions src/app/(routes)/blog/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import BackgroundBanner from "./_components/background-banner";
import Filter from "./_components/filter";

import { Suspense } from "react";
import {
retrieveContentfulPublishedSlugs,
retrieveContentfulEntry,
} from "@/app/_lib/contentful";
import { retrieveContentfulPublishedSlugs } from "@/app/_lib/contentful";
import { Posts } from "./_components/posts";
import { createCacheKey } from "@/app/_lib/cache";

Expand All @@ -18,28 +15,14 @@ type PageProps = {
};

export default async function BlogHomePage({ searchParams }: PageProps) {
const recentArticleSlugs = await retrieveContentfulPublishedSlugs({
limit: 6,
avoidTags: ["get-started"],
sortByRecent: true,
});
const getStartedSlugs = await retrieveContentfulPublishedSlugs({
limit: 3,
includeTags: ["get-started"],
});
const getStartedSnippets = await Promise.all(
getStartedSlugs.map((s) => retrieveContentfulEntry(s)),
);

const key = createCacheKey({
searchParams,
});

return (
<>
<BackgroundBanner />
<main className="relative z-[1] mx-auto flex w-full max-w-4xl flex-col items-center gap-6 px-4 py-10 lg:gap-8 lg:px-0">
<Text variant="heading-2" className="-mb-6 lg:-mb-8">
<Text variant="heading-2" className="-mb-6">
Copy link
Contributor

@gsteenkamp89 gsteenkamp89 Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: pass your key generated using createCacheKey as a key to <Suspense>. this will force it to show the fallback text while it fetches new data. Unless you don't want that.

Copy link
Contributor Author

@james-a-morris james-a-morris Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done on line 32

Across Blog
</Text>
<Suspense>
Expand All @@ -51,11 +34,7 @@ export default async function BlogHomePage({ searchParams }: PageProps) {
<h2 className="text-text-secondary text-2xl my-auto flex-1">Searching...</h2>
}
>
<Posts
getStartedSnippets={getStartedSnippets}
recentArticleSlugs={recentArticleSlugs}
searchParams={searchParams}
/>
<Posts searchParams={searchParams} />
<BackToTopButton />
</Suspense>
</main>
Expand Down
3 changes: 3 additions & 0 deletions src/app/_lib/contentful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ export async function retrieveContentfulPublishedSlugs({
avoidTags,
includeTags,
sortByRecent,
skip,
}: {
query?: string;
limit?: number;
avoidTags?: string[];
includeTags?: string[];
sortByRecent?: boolean;
skip?: number;
} = {}): Promise<string[]> {
const client = getProductionClient();
const options = {
Expand All @@ -69,6 +71,7 @@ export async function retrieveContentfulPublishedSlugs({
...(avoidTags ? { "fields.tag[nin]": avoidTags.join(",").toLowerCase() } : {}),
...(includeTags ? { "fields.tag[in]": includeTags.join(",").toLowerCase() } : {}),
...(sortByRecent ? { order: "-fields.publishDate" } : {}),
...(skip ? { skip } : {}),
} as const;
const entries =
await client.withoutUnresolvableLinks.getEntries<TypeAcrossBlogPostSkeleton>(options);
Expand Down
Loading