-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFriendRequestsSidebarOptions.tsx
71 lines (59 loc) · 2.26 KB
/
FriendRequestsSidebarOptions.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
/** @format */
"use client";
import { pusherClient } from "@/lib/pusher";
import { toPusherKey } from "@/lib/utils";
import { User } from "lucide-react";
import { init } from "next/dist/compiled/webpack/webpack";
import Link from "next/link";
import { FC, useEffect, useState } from "react";
interface FriendRequestsSidebarOptionsProps {
sessionId: string;
initialUnseenRequestCount: number;
}
const FriendRequestsSidebarOptions: FC<FriendRequestsSidebarOptionsProps> = ({
sessionId,
initialUnseenRequestCount,
}) => {
const [unseenRequestCount, setunseenRequestCount] = useState<number>(
initialUnseenRequestCount
);
useEffect(() => {
pusherClient.subscribe(
toPusherKey(`user:${sessionId}:incoming_friend_requests`)
);
pusherClient.subscribe(toPusherKey(`user:${sessionId}:friends`));
const friendRequestHandler = () => {
setunseenRequestCount((prev) => prev + 1);
};
const addedFriendHandler = () => {
setunseenRequestCount((prev) => prev - 1);
};
pusherClient.bind("incoming_friend_requests", friendRequestHandler);
pusherClient.bind("new_friend", addedFriendHandler);
return () => {
pusherClient.unsubscribe(
toPusherKey(`user:${sessionId}:incoming_friend_requests`)
);
pusherClient.unsubscribe(toPusherKey(`user:${sessionId}:friends`));
pusherClient.unbind("incoming_friend_requests", friendRequestHandler);
pusherClient.unbind("new_friend", addedFriendHandler);
};
}, [sessionId]);
return (
<Link
href='/dashboard/requests'
className='text-gray-700 hover:text-indigo-600 hover:bg-gray-50 group flex items-center gap-x-3 rounded-md p-2 text-sm leading-6 font-semibold'
>
<div className='text-gray-400 border-gray-200 group-hover:border-indigo-600 group-hover:text-indigo-600 flex h-6 w-6 shrink-0 items-center justify-center rounded-lg border text-[0.625rem] font-medium bg-white'>
<User className='h-4 w-4' />
</div>
<p className='truncate'>Friend requests</p>
{unseenRequestCount > 0 && (
<div className='flex items-center justify-center h-5 w-5 bg-indigo-600 text-white rounded-full text-xs'>
{unseenRequestCount}
</div>
)}
</Link>
);
};
export default FriendRequestsSidebarOptions;