-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathuseIsChannelMuted.ts
More file actions
34 lines (25 loc) · 939 Bytes
/
useIsChannelMuted.ts
File metadata and controls
34 lines (25 loc) · 939 Bytes
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
import { useEffect, useState } from 'react';
import type { Channel } from 'stream-chat';
import { useChatContext } from '../../../contexts/chatContext/ChatContext';
import type { DefaultStreamChatGenerics } from '../../../types/types';
const defaultMuteStatus = {
createdAt: null,
expiresAt: null,
muted: false,
};
export const useIsChannelMuted = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
>(
channel: Channel<StreamChatGenerics>,
) => {
const { client } = useChatContext<StreamChatGenerics>();
const [muted, setMuted] = useState(() => channel.muteStatus());
useEffect(() => {
const handleEvent = () => {
setMuted(channel.muteStatus());
};
client.on('notification.channel_mutes_updated', handleEvent);
return () => client.off('notification.channel_mutes_updated', handleEvent);
}, [channel, client, muted]);
return muted ?? defaultMuteStatus;
};