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 Feed from "@/components/Feed";
import { headers } from "next/headers" import SearchBar from "@/components/SearchBar";
import Feed from "@/components/Feed" import { ApiResponse, fetchFeedData } from "@/lib/feed.ts";
import SearchBar from "@/components/SearchBar" import * as GraphicSection from "@/components/GraphicSection";
import { fetchFeedData } from "@/lib/feed.ts" import { ArrowIcon } from "@/components/ArrowIcon";
import * as GraphicSection from "@/components/GraphicSection" import { GetServerSideProps } from "next";
import { ArrowIcon } from "@/components/ArrowIcon" import { Article } from "@/lib/prisma.ts";
type Props = { type Props = {
searchParams?: { data: ApiResponse<Article>;
q?: string | undefined };
}
}
export default async function Home({ searchParams }: Props) { export const getServerSideProps: GetServerSideProps<Props> = async ({
const headerList = headers() req,
const referer = headerList.get("referer") params,
}) => {
if (!referer && searchParams?.q) { if (!req.headers.referer && params?.q) {
redirect(`/search?q=${searchParams.q}`) return {
redirect: {
destination: `/search?q=${params?.q}`,
permanent: false,
},
};
} }
const data = await fetchFeedData({}) const data = await fetchFeedData({});
return { props: { data } };
};
export default async function Home({ data }: Props) {
return ( return (
<> <>
<SearchBar /> <SearchBar />
@ -34,7 +41,10 @@ export default async function Home({ searchParams }: Props) {
initialData={data.data} initialData={data.data}
/> />
</div> </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> <GraphicSection.Background>
<img <img
src="/lume-logo-bg.png" src="/lume-logo-bg.png"
@ -50,15 +60,13 @@ export default async function Home({ searchParams }: Props) {
web together. web together.
</p> </p>
<div className="link flex text-gray-400"> <div className="link flex text-gray-400">
<span> <span>Learn more about Lume and join our community</span>
Learn more about Lume and join our community <ArrowIcon className=" ml-2 mt-2" />
</span>
<ArrowIcon className=" ml-2 mt-2"/>
</div> </div>
</div> </div>
</GraphicSection.Foreground> </GraphicSection.Foreground>
</GraphicSection.Root> </GraphicSection.Root>
</div> </div>
</> </>
) );
} }