Skip to content

Commit

Permalink
fix: backport crash fix (#2787)
Browse files Browse the repository at this point in the history
* fix: crash in some instances of useIsChannelMuted hook invocation

* fix: properly use hook in channel preview

* fix: edge cases and test
  • Loading branch information
isekovanic authored Nov 15, 2024
1 parent fe64697 commit 9b5b243
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefaultStreamChatGenerics>;

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'", () => {
Expand Down
10 changes: 5 additions & 5 deletions package/src/components/ChannelPreview/hooks/useIsChannelMuted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ export const useIsChannelMuted = <
channel: Channel<StreamChatGenerics>,
) => {
const { client } = useChatContext<StreamChatGenerics>();
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 };
};

0 comments on commit 9b5b243

Please sign in to comment.