refactor: use getServerSideProps

This commit is contained in:
Derrick Hammer 2023-12-17 18:57:24 -05:00
parent 175eac054f
commit f6e627e045
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 32 additions and 24 deletions

View File

@ -1,27 +1,34 @@
import { redirect } from "next/navigation"
import { headers } from "next/headers"
import Feed from "@/components/Feed"
import SearchBar from "@/components/SearchBar"
import { fetchFeedData } from "@/lib/feed.ts"
import * as GraphicSection from "@/components/GraphicSection"
import { ArrowIcon } from "@/components/ArrowIcon"
import Feed from "@/components/Feed";
import SearchBar from "@/components/SearchBar";
import { ApiResponse, fetchFeedData } from "@/lib/feed.ts";
import * as GraphicSection from "@/components/GraphicSection";
import { ArrowIcon } from "@/components/ArrowIcon";
import { GetServerSideProps } from "next";
import { Article } from "@/lib/prisma.ts";
type Props = {
searchParams?: {
q?: string | undefined
}
data: ApiResponse<Article>;
};
export const getServerSideProps: GetServerSideProps<Props> = async ({
req,
params,
}) => {
if (!req.headers.referer && params?.q) {
return {
redirect: {
destination: `/search?q=${params?.q}`,
permanent: false,
},
};
}
export default async function Home({ searchParams }: Props) {
const headerList = headers()
const referer = headerList.get("referer")
const data = await fetchFeedData({});
if (!referer && searchParams?.q) {
redirect(`/search?q=${searchParams.q}`)
}
const data = await fetchFeedData({})
return { props: { data } };
};
export default async function Home({ data }: Props) {
return (
<>
<SearchBar />
@ -34,7 +41,10 @@ export default async function Home({ searchParams }: Props) {
initialData={data.data}
/>
</div>
<GraphicSection.Root href="https://lumeweb.com" className="h-[100px] border border-gray-800 cursor-pointer [&:hover_.link]:underline [&:hover_.background]:rotate-12 [&:hover_.background]:scale-110">
<GraphicSection.Root
href="https://lumeweb.com"
className="h-[100px] border border-gray-800 cursor-pointer [&:hover_.link]:underline [&:hover_.background]:rotate-12 [&:hover_.background]:scale-110"
>
<GraphicSection.Background>
<img
src="/lume-logo-bg.png"
@ -50,9 +60,7 @@ export default async function Home({ searchParams }: Props) {
web together.
</p>
<div className="link flex text-gray-400">
<span>
Learn more about Lume and join our community
</span>
<span>Learn more about Lume and join our community</span>
<ArrowIcon className=" ml-2 mt-2" />
</div>
</div>
@ -60,5 +68,5 @@ export default async function Home({ searchParams }: Props) {
</GraphicSection.Root>
</div>
</>
)
);
}