|
1 | | -"use client"; |
2 | | - |
3 | | -import { useContext } from "react"; |
4 | 1 | import { Course } from "@courselit/common-models"; |
5 | | -import { Section } from "@courselit/page-primitives"; |
6 | | -import Post from "./post"; |
7 | | -import { ThemeContext } from "@components/contexts"; |
| 2 | +import { Caption, Header1, Section, Text1 } from "@courselit/page-primitives"; |
| 3 | +import { formattedLocaleDate, getFullSiteSetup } from "@ui-lib/utils"; |
| 4 | +import { getAddressFromHeaders } from "@ui-lib/utils"; |
| 5 | +import { headers } from "next/headers"; |
| 6 | +import { ProductWithAdminProps } from "@/hooks/use-product"; |
| 7 | +import { FetchBuilder } from "@courselit/utils"; |
| 8 | +import { Text2 } from "@courselit/page-primitives"; |
| 9 | +import Link from "next/link"; |
| 10 | +import { truncate } from "@ui-lib/utils"; |
| 11 | +import Image from "next/image"; |
| 12 | +import ClientSideTextRenderer from "./client-side-text-renderer"; |
| 13 | +import { |
| 14 | + Breadcrumb, |
| 15 | + BreadcrumbItem, |
| 16 | + BreadcrumbLink, |
| 17 | + BreadcrumbList, |
| 18 | + BreadcrumbPage, |
| 19 | + BreadcrumbSeparator, |
| 20 | +} from "@components/ui/breadcrumb"; |
8 | 21 |
|
9 | | -export default function BlogPost({ |
| 22 | +export default async function ProductPage({ |
10 | 23 | params, |
11 | 24 | }: { |
12 | 25 | params: { slug: string; id: string }; |
13 | 26 | course: Course; |
14 | 27 | }) { |
15 | | - const { theme } = useContext(ThemeContext); |
| 28 | + const address = getAddressFromHeaders(headers); |
| 29 | + const siteInfo = await getFullSiteSetup(address); |
| 30 | + if (!siteInfo) { |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + const { theme } = siteInfo; |
| 35 | + const product = await getProduct(address, params.id); |
| 36 | + if (!product) { |
| 37 | + return null; |
| 38 | + } |
16 | 39 |
|
17 | 40 | return ( |
18 | 41 | <Section theme={theme.theme}> |
19 | 42 | <div className="flex flex-col gap-4 max-w-[640px] mx-auto"> |
20 | | - <Post courseId={params.id} /> |
| 43 | + <Breadcrumb className="mb-4"> |
| 44 | + <BreadcrumbList> |
| 45 | + <BreadcrumbItem> |
| 46 | + <BreadcrumbLink asChild> |
| 47 | + <Text2 |
| 48 | + className="cursor-pointer" |
| 49 | + theme={theme.theme} |
| 50 | + > |
| 51 | + <Link href="/blog">Blog</Link> |
| 52 | + </Text2> |
| 53 | + </BreadcrumbLink> |
| 54 | + </BreadcrumbItem> |
| 55 | + <BreadcrumbSeparator /> |
| 56 | + <BreadcrumbItem> |
| 57 | + <BreadcrumbPage> |
| 58 | + <Text2 theme={theme.theme}> |
| 59 | + {truncate(product?.title, 20)} |
| 60 | + </Text2> |
| 61 | + </BreadcrumbPage> |
| 62 | + </BreadcrumbItem> |
| 63 | + </BreadcrumbList> |
| 64 | + </Breadcrumb> |
| 65 | + <Header1 theme={theme.theme}>{product?.title}</Header1> |
| 66 | + <div className="flex items-center gap-4 mb-6"> |
| 67 | + <Image |
| 68 | + src={ |
| 69 | + product?.user?.avatar?.file || |
| 70 | + "/courselit_backdrop_square.webp" |
| 71 | + } |
| 72 | + alt={product?.user?.name || ""} |
| 73 | + width={32} |
| 74 | + height={32} |
| 75 | + className="rounded-full aspect-square" |
| 76 | + /> |
| 77 | + <div className="flex items-center gap-2"> |
| 78 | + <Text2 theme={theme.theme}> |
| 79 | + {truncate(product?.user?.name, 50)} |
| 80 | + </Text2> |
| 81 | + <Caption theme={theme.theme}> |
| 82 | + · {formattedLocaleDate(product?.updatedAt, "long")} |
| 83 | + </Caption> |
| 84 | + </div> |
| 85 | + </div> |
| 86 | + {product?.featuredImage && ( |
| 87 | + <div className="mb-4 border rounded overflow-hidden"> |
| 88 | + <Image |
| 89 | + alt={product.featuredImage.caption || ""} |
| 90 | + src={product.featuredImage.file!} |
| 91 | + width={640} |
| 92 | + height={360} |
| 93 | + /> |
| 94 | + </div> |
| 95 | + )} |
| 96 | + {product?.description && ( |
| 97 | + <Text1 theme={theme.theme} component="span"> |
| 98 | + <ClientSideTextRenderer |
| 99 | + json={JSON.parse(product.description)} |
| 100 | + /> |
| 101 | + </Text1> |
| 102 | + )} |
21 | 103 | </div> |
22 | 104 | </Section> |
23 | 105 | ); |
24 | 106 | } |
| 107 | + |
| 108 | +export const getProduct = async ( |
| 109 | + backend: string, |
| 110 | + id: string, |
| 111 | +): Promise<ProductWithAdminProps | undefined> => { |
| 112 | + const query = ` |
| 113 | + query ($id: String!) { |
| 114 | + course: getCourse(id: $id) { |
| 115 | + title |
| 116 | + description |
| 117 | + type |
| 118 | + slug |
| 119 | + courseId |
| 120 | + featuredImage { |
| 121 | + file |
| 122 | + thumbnail |
| 123 | + caption |
| 124 | + }, |
| 125 | + updatedAt |
| 126 | + user { |
| 127 | + name |
| 128 | + avatar { |
| 129 | + file |
| 130 | + thumbnail |
| 131 | + caption |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + `; |
| 137 | + const fetch = new FetchBuilder() |
| 138 | + .setUrl(`${backend}/api/graph`) |
| 139 | + .setPayload({ query, variables: { id } }) |
| 140 | + .setIsGraphQLEndpoint(true) |
| 141 | + .build(); |
| 142 | + |
| 143 | + try { |
| 144 | + const response = await fetch.exec(); |
| 145 | + return response.course; |
| 146 | + } catch (e: any) { |
| 147 | + console.log("Error fetching product", e.message); // eslint-disable-line no-console |
| 148 | + } |
| 149 | +}; |
0 commit comments