|
1 | 1 | import Navigation from "@/components/navigation"; |
2 | 2 | import styles from "./page.module.css"; |
3 | | -import { PlainClient } from "@team-plain/typescript-sdk"; |
| 3 | +import { plainClient } from "@/utils/plainClient"; |
| 4 | +import { ThreadRow } from "@/components/threadRow"; |
| 5 | +import { PaginationControls } from "@/components/paginationControls"; |
4 | 6 |
|
5 | 7 | export const fetchCache = "force-no-store"; |
6 | 8 |
|
7 | | -const apiKey = process.env.PLAIN_API_KEY; |
8 | | -if (!apiKey) { |
9 | | - throw new Error("Please set the `PLAIN_API_KEY` environment variable"); |
10 | | -} |
11 | | - |
12 | | -export const client = new PlainClient({ |
13 | | - apiKey, |
14 | | -}); |
| 9 | +// When adapting this example get the tenant id as part of auth or fetch it afterwards |
| 10 | +const TENANT_EXTERNAL_ID = "abcd1234"; |
15 | 11 |
|
16 | 12 | export default async function Home({ |
17 | | - params, |
18 | | - searchParams, |
| 13 | + searchParams, |
19 | 14 | }: { |
20 | | - params: { slug: string }; |
21 | | - searchParams: { [key: string]: string | string[] | undefined }; |
| 15 | + searchParams: { [key: string]: string | undefined }; |
22 | 16 | }) { |
23 | | - const threads = await client.getThreads({ |
24 | | - filters: { |
25 | | - // customerIds: ["c_01J28ZQKJX9CVRXVHBMAXNSV5G"], |
26 | | - tenantIdentifiers: [{ tenantId: "te_01J299SM3E25EJHT7JKYS6G7K5" }], |
27 | | - }, |
28 | | - }); |
29 | | - |
30 | | - console.log(threads.data?.threads.length); |
| 17 | + const threads = await plainClient.getThreads({ |
| 18 | + filters: { |
| 19 | + // If you want to only allow customers to view threads they have raised then you can filter by customerIds instead. |
| 20 | + // Note that if you provide multiple filters they are combined with AND rather than OR. |
| 21 | + // customerIds: ["c_01J28ZQKJX9CVRXVHBMAXNSV5G"], |
| 22 | + tenantIdentifiers: [{ externalId: TENANT_EXTERNAL_ID }], |
| 23 | + }, |
| 24 | + after: searchParams.after as string | undefined, |
| 25 | + before: searchParams.before as string | undefined, |
| 26 | + }); |
31 | 27 |
|
32 | | - return ( |
33 | | - <> |
34 | | - <Navigation title="Plain Headless Portal example" /> |
35 | | - <main className={styles.main}> |
36 | | - <div> |
37 | | - {threads.data?.threads.map((thread) => { |
38 | | - return ( |
39 | | - <div key={`thread-row-${thread.id}`}> |
40 | | - <a href={`/thread/${thread.id}`}>{thread.title}</a> |
41 | | - </div> |
42 | | - ); |
43 | | - })} |
44 | | - </div> |
45 | | - </main> |
46 | | - </> |
47 | | - ); |
| 28 | + return ( |
| 29 | + <> |
| 30 | + <Navigation title="Plain Headless Portal example" /> |
| 31 | + <main className={styles.main}> |
| 32 | + <h2>Support requests</h2> |
| 33 | + {threads.data && ( |
| 34 | + <> |
| 35 | + <div className={styles.list}> |
| 36 | + {threads.data?.threads.map((thread) => { |
| 37 | + return ( |
| 38 | + <ThreadRow thread={thread} key={`thread-row-${thread.id}`} /> |
| 39 | + ); |
| 40 | + })} |
| 41 | + </div> |
| 42 | + <PaginationControls pageInfo={threads.data.pageInfo} /> |
| 43 | + </> |
| 44 | + )} |
| 45 | + </main> |
| 46 | + </> |
| 47 | + ); |
48 | 48 | } |
0 commit comments