Skip to content

Commit

Permalink
fix: edge cases and test
Browse files Browse the repository at this point in the history
  • Loading branch information
isekovanic committed Nov 15, 2024
1 parent 3e7f077 commit b23b320
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const useChannelPreviewData = <
| MessageResponse<StreamChatGenerics>
>(channel.state.messages[channel.state.messages.length - 1]);
const [unread, setUnread] = useState(channel.countUnread());
const muted = useIsChannelMuted(channel);
const { muted } = useIsChannelMuted(channel);

/**
* This effect listens for the `notification.mark_read` event and sets the unread count to 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ export const useIsChannelMuted = <
const { client } = useChatContext<StreamChatGenerics>();
const initialized = channel?.initialized;

const [muted, setMuted] = useState(() => initialized && channel.muteStatus()?.muted);
const [muted, setMuted] = useState(() => initialized && channel.muteStatus());

useEffect(() => {
const handleEvent = () => {
setMuted(initialized && channel.muteStatus()?.muted);
setMuted(initialized && channel.muteStatus());
};

client.on('notification.channel_mutes_updated', handleEvent);
return () => client.off('notification.channel_mutes_updated', handleEvent);
}, [channel, client, initialized, muted]);

return muted;
return muted || { createdAt: null, expiresAt: null, muted: false };
};

0 comments on commit b23b320

Please sign in to comment.