/
page.tsx
60 lines (51 loc) · 1.59 KB
/
page.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"use client";
import { useMemo } from "react";
import { useSearchParams } from "next/navigation";
import { RoomProvider, useThreads } from "@/liveblocks.config";
import { Loading } from "@/components/Loading";
import { Composer, Thread } from "@liveblocks/react-comments";
import { ClientSideSuspense } from "@liveblocks/react";
import { ErrorBoundary } from "react-error-boundary";
/**
* Displays a list of threads, along with a composer for creating
* new threads.
*/
function Example() {
const { threads } = useThreads();
return (
<main>
{threads.map((thread) => (
<Thread key={thread.id} thread={thread} className="thread" />
))}
<Composer className="composer" />
</main>
);
}
export default function Page() {
const roomId = useExampleRoomId("liveblocks:examples:nextjs-comments-ai");
return (
<RoomProvider id={roomId} initialPresence={{}}>
<ErrorBoundary
fallback={
<div className="error">There was an error while getting threads.</div>
}
>
<ClientSideSuspense fallback={<Loading />}>
{() => <Example />}
</ClientSideSuspense>
</ErrorBoundary>
</RoomProvider>
);
}
/**
* This function is used when deploying an example on liveblocks.io.
* You can ignore it completely if you run the example locally.
*/
function useExampleRoomId(roomId: string) {
const params = useSearchParams();
const exampleId = params?.get("exampleId");
const exampleRoomId = useMemo(() => {
return exampleId ? `${roomId}-${exampleId}` : roomId;
}, [roomId, exampleId]);
return exampleRoomId;
}