-
Notifications
You must be signed in to change notification settings - Fork 172
/
page.tsx
121 lines (101 loc) · 3.33 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import ChatInput from '@/components/ChatInput'
import Messages from '@/components/Messages'
import { fetchRedis } from '@/helpers/redis'
import { authOptions } from '@/lib/auth'
import { messageArrayValidator } from '@/lib/validations/message'
import { getServerSession } from 'next-auth'
import Image from 'next/image'
import { notFound } from 'next/navigation'
// The following generateMetadata functiion was written after the video and is purely optional
export async function generateMetadata({
params,
}: {
params: { chatId: string }
}) {
const session = await getServerSession(authOptions)
if (!session) notFound()
const [userId1, userId2] = params.chatId.split('--')
const { user } = session
const chatPartnerId = user.id === userId1 ? userId2 : userId1
const chatPartnerRaw = (await fetchRedis(
'get',
`user:${chatPartnerId}`
)) as string
const chatPartner = JSON.parse(chatPartnerRaw) as User
return { title: `FriendZone | ${chatPartner.name} chat` }
}
interface PageProps {
params: {
chatId: string
}
}
async function getChatMessages(chatId: string) {
try {
const results: string[] = await fetchRedis(
'zrange',
`chat:${chatId}:messages`,
0,
-1
)
const dbMessages = results.map((message) => JSON.parse(message) as Message)
const reversedDbMessages = dbMessages.reverse()
const messages = messageArrayValidator.parse(reversedDbMessages)
return messages
} catch (error) {
notFound()
}
}
const page = async ({ params }: PageProps) => {
const { chatId } = params
const session = await getServerSession(authOptions)
if (!session) notFound()
const { user } = session
const [userId1, userId2] = chatId.split('--')
if (user.id !== userId1 && user.id !== userId2) {
notFound()
}
const chatPartnerId = user.id === userId1 ? userId2 : userId1
// new
const chatPartnerRaw = (await fetchRedis(
'get',
`user:${chatPartnerId}`
)) as string
const chatPartner = JSON.parse(chatPartnerRaw) as User
const initialMessages = await getChatMessages(chatId)
return (
<div className='flex-1 justify-between flex flex-col h-full max-h-[calc(100vh-6rem)]'>
<div className='flex sm:items-center justify-between py-3 border-b-2 border-gray-200'>
<div className='relative flex items-center space-x-4'>
<div className='relative'>
<div className='relative w-8 sm:w-12 h-8 sm:h-12'>
<Image
fill
referrerPolicy='no-referrer'
src={chatPartner.image}
alt={`${chatPartner.name} profile picture`}
className='rounded-full'
/>
</div>
</div>
<div className='flex flex-col leading-tight'>
<div className='text-xl flex items-center'>
<span className='text-gray-700 mr-3 font-semibold'>
{chatPartner.name}
</span>
</div>
<span className='text-sm text-gray-600'>{chatPartner.email}</span>
</div>
</div>
</div>
<Messages
chatId={chatId}
chatPartner={chatPartner}
sessionImg={session.user.image}
sessionId={session.user.id}
initialMessages={initialMessages}
/>
<ChatInput chatId={chatId} chatPartner={chatPartner} />
</div>
)
}
export default page