Skip to content

Commit f667148

Browse files
authored
Merge pull request #2786 from GetStream/fix/is-muted-hook-crash
fix: is muted hook crash
2 parents 81507fb + bbf6a85 commit f667148

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

package/src/components/ChannelPreview/hooks/__tests__/useChannelPreviewMuted.test.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@ describe('useChannelPreviewMuted', () => {
2929
});
3030

3131
const mockChannel = {
32-
muteStatus: jest.fn().mockReturnValue(false),
32+
initialized: true,
33+
muteStatus: jest.fn().mockReturnValue({
34+
createdAt: Date.now(),
35+
expiresAt: Date.now() + 5000,
36+
muted: false,
37+
}),
3338
} as unknown as Channel<DefaultStreamChatGenerics>;
3439

3540
it('should return the correct mute status', () => {
3641
const { result } = renderHook(() => useIsChannelMuted(mockChannel));
37-
expect(result.current).toBe(false);
42+
expect(result.current.muted).toBe(false);
3843
});
3944

4045
it("should update the mute status when the notification.channel_mutes_updated event is emitted'", () => {

package/src/components/ChannelPreview/hooks/useIsChannelMuted.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ export const useIsChannelMuted = <
1212
channel: Channel<StreamChatGenerics>,
1313
) => {
1414
const { client } = useChatContext<StreamChatGenerics>();
15+
const initialized = channel?.initialized;
1516

16-
const [muted, setMuted] = useState(channel.muteStatus());
17+
const [muted, setMuted] = useState(() => initialized && channel.muteStatus());
1718

1819
useEffect(() => {
1920
const handleEvent = () => {
20-
setMuted(channel.muteStatus());
21+
setMuted(initialized && channel.muteStatus());
2122
};
2223

2324
client.on('notification.channel_mutes_updated', handleEvent);
2425
return () => client.off('notification.channel_mutes_updated', handleEvent);
25-
// eslint-disable-next-line react-hooks/exhaustive-deps
26-
}, [muted]);
26+
}, [channel, client, initialized, muted]);
2727

28-
return muted;
28+
return muted || { createdAt: null, expiresAt: null, muted: false };
2929
};

0 commit comments

Comments
 (0)