From 9b5b243cf91bc80a1fbe50913dbbdc954c973bca Mon Sep 17 00:00:00 2001 From: Ivan Sekovanikj <31964049+isekovanic@users.noreply.github.com> Date: Fri, 15 Nov 2024 16:55:07 +0100 Subject: [PATCH] fix: backport crash fix (#2787) * fix: crash in some instances of useIsChannelMuted hook invocation * fix: properly use hook in channel preview * fix: edge cases and test --- .../hooks/__tests__/useChannelPreviewMuted.test.tsx | 9 +++++++-- .../ChannelPreview/hooks/useIsChannelMuted.ts | 10 +++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/package/src/components/ChannelPreview/hooks/__tests__/useChannelPreviewMuted.test.tsx b/package/src/components/ChannelPreview/hooks/__tests__/useChannelPreviewMuted.test.tsx index f39d28316..1d52486f2 100644 --- a/package/src/components/ChannelPreview/hooks/__tests__/useChannelPreviewMuted.test.tsx +++ b/package/src/components/ChannelPreview/hooks/__tests__/useChannelPreviewMuted.test.tsx @@ -29,12 +29,17 @@ describe('useChannelPreviewMuted', () => { }); const mockChannel = { - muteStatus: jest.fn().mockReturnValue(false), + initialized: true, + muteStatus: jest.fn().mockReturnValue({ + createdAt: Date.now(), + expiresAt: Date.now() + 5000, + muted: false, + }), } as unknown as Channel; it('should return the correct mute status', () => { const { result } = renderHook(() => useIsChannelMuted(mockChannel)); - expect(result.current).toBe(false); + expect(result.current.muted).toBe(false); }); it("should update the mute status when the notification.channel_mutes_updated event is emitted'", () => { diff --git a/package/src/components/ChannelPreview/hooks/useIsChannelMuted.ts b/package/src/components/ChannelPreview/hooks/useIsChannelMuted.ts index 9b8e87fc3..580bd0c41 100644 --- a/package/src/components/ChannelPreview/hooks/useIsChannelMuted.ts +++ b/package/src/components/ChannelPreview/hooks/useIsChannelMuted.ts @@ -12,18 +12,18 @@ export const useIsChannelMuted = < channel: Channel, ) => { const { client } = useChatContext(); + const initialized = channel?.initialized; - const [muted, setMuted] = useState(channel.muteStatus()); + const [muted, setMuted] = useState(() => initialized && channel.muteStatus()); useEffect(() => { const handleEvent = () => { - setMuted(channel.muteStatus()); + setMuted(initialized && channel.muteStatus()); }; client.on('notification.channel_mutes_updated', handleEvent); return () => client.off('notification.channel_mutes_updated', handleEvent); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [muted]); + }, [channel, client, initialized, muted]); - return muted; + return muted || { createdAt: null, expiresAt: null, muted: false }; };