|
| 1 | +import { |
| 2 | + getSite, |
| 3 | + queryByPublished, |
| 4 | + queryRelationById, |
| 5 | + getSponsorPageBlocks, |
| 6 | +} from '@/services/notion.server'; |
| 7 | +import { NextSeo } from 'next-seo'; |
| 8 | +import Layout from '@/layout/Layout'; |
| 9 | +import { Site } from '@/models/site.model'; |
| 10 | +import { Post, PostType } from '@/models/post.model'; |
| 11 | +import PostsCards from '@/components/PostsCards'; |
| 12 | +import { GetStaticPaths, GetStaticProps, InferGetStaticPropsType } from 'next'; |
| 13 | +import { Sponsor } from '@/models/sponsor.model'; |
| 14 | +import Image from 'next/image'; |
| 15 | + |
| 16 | +interface StaticParams { |
| 17 | + site: Site; |
| 18 | + sponsor: Sponsor; |
| 19 | + podcasts: Post[]; |
| 20 | +} |
| 21 | + |
| 22 | +export const getStaticPaths: GetStaticPaths = async () => { |
| 23 | + const paths: { params: { sponsorPath: string } }[] = []; |
| 24 | + const sponsors = await queryByPublished('sponsor', 10000); |
| 25 | + console.log('sponsors', sponsors); |
| 26 | + for (const p of sponsors.results as any) { |
| 27 | + if (p?.properties?.slug?.url) { |
| 28 | + paths.push({ |
| 29 | + params: { |
| 30 | + sponsorPath: `${p?.properties?.slug?.url}`, |
| 31 | + }, |
| 32 | + }); |
| 33 | + } |
| 34 | + } |
| 35 | + return { |
| 36 | + paths, |
| 37 | + fallback: true, |
| 38 | + }; |
| 39 | +}; |
| 40 | + |
| 41 | +export const getStaticProps: GetStaticProps<StaticParams> = async ({ |
| 42 | + params, |
| 43 | + preview, |
| 44 | + previewData, |
| 45 | +}) => { |
| 46 | + const { sponsorPath } = params as any; |
| 47 | + |
| 48 | + if (!sponsorPath) { |
| 49 | + return { |
| 50 | + notFound: true, |
| 51 | + }; |
| 52 | + } |
| 53 | + const site = getSite(); |
| 54 | + const sponsor = await getSponsorPageBlocks(sponsorPath); |
| 55 | + |
| 56 | + if (!sponsor) { |
| 57 | + console.log('Sponsor not found'); |
| 58 | + return { |
| 59 | + notFound: true, |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + const [podcasts] = await Promise.all([ |
| 64 | + queryRelationById(sponsor.id, 'sponsors', PostType.podcast), |
| 65 | + ]); |
| 66 | + |
| 67 | + return { |
| 68 | + props: { |
| 69 | + site, |
| 70 | + sponsor, |
| 71 | + podcasts: podcasts?.results || null, |
| 72 | + }, |
| 73 | + }; |
| 74 | +}; |
| 75 | + |
| 76 | +export default function SponsorPage({ |
| 77 | + site, |
| 78 | + sponsor, |
| 79 | + podcasts, |
| 80 | +}: InferGetStaticPropsType<typeof getStaticProps>) { |
| 81 | + return ( |
| 82 | + <> |
| 83 | + <NextSeo |
| 84 | + title={`${sponsor?.company || ''} | CodingCatDev`} |
| 85 | + canonical={`https://codingcat.dev/sponsors/`} |
| 86 | + ></NextSeo> |
| 87 | + <Layout site={site}> |
| 88 | + <section className="grid grid-cols-1 gap-20 p-4 sm:p-10 place-items-center"> |
| 89 | + <a href={sponsor.url} rel="noreferrer noopener" target="_blank"> |
| 90 | + <article className="grid items-start grid-cols-1 gap-4 shadow-lg rounded-xl justify-items-center justify-self-center hover:shadow-sm"> |
| 91 | + <div className="w-full rounded-lg shadow-lg bg-primary-100 dark:bg-primary-500"> |
| 92 | + <div className="p-2"> |
| 93 | + <div |
| 94 | + style={{ |
| 95 | + overflow: 'hidden', |
| 96 | + paddingTop: '56.25%', |
| 97 | + position: 'relative', |
| 98 | + width: '100%', |
| 99 | + height: 0, |
| 100 | + }} |
| 101 | + > |
| 102 | + <div className="absolute inset-0 flex flex-col items-center justify-center p-2"> |
| 103 | + {sponsor?.coverPhoto?.public_id && ( |
| 104 | + <Image |
| 105 | + src={sponsor.coverPhoto.public_id} |
| 106 | + alt={`Sponsorship Image for ${sponsor.company}`} |
| 107 | + layout="fill" |
| 108 | + /> |
| 109 | + )} |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + <h2 className="flex flex-wrap content-center pl-1 text-2xl font-semibold"> |
| 113 | + {sponsor.company} |
| 114 | + </h2> |
| 115 | + <p className="m-2 text-lg">{sponsor.description}</p> |
| 116 | + </div> |
| 117 | + <div className="flex w-full mt-2 rounded-b-lg bg-primary-900"> |
| 118 | + <p className="px-4 py-2 text-sm text-white uppercase border-none cursor-pointer"> |
| 119 | + More Information -{'>'} |
| 120 | + </p> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + </article> |
| 124 | + </a> |
| 125 | + </section> |
| 126 | + {podcasts && podcasts.length > 0 && ( |
| 127 | + <section className="grid w-full gap-10 px-4 mx-auto xl:px-10"> |
| 128 | + <h2 className="mt-4 text-4xl text-primary-900 lg:text-5xl"> |
| 129 | + Podcasts |
| 130 | + </h2> |
| 131 | + {podcasts && <PostsCards posts={podcasts} />} |
| 132 | + </section> |
| 133 | + )} |
| 134 | + </Layout> |
| 135 | + </> |
| 136 | + ); |
| 137 | +} |
0 commit comments