Skip to content

Commit

Permalink
add useChannelHiddenListener
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaap committed Aug 11, 2020
1 parent 65aaf77 commit 33739bd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/components/ChannelList/ChannelList.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { useNotificationRemovedFromChannelListener } from './hooks/useNotificati
import { useChannelDeletedListener } from './hooks/useChannelDeletedListener';
import { useChannelTruncatedListener } from './hooks/useChannelTruncatedListener';
import { useChannelUpdatedListener } from './hooks/useChannelUpdatedListener';
import { useChannelHiddenListener } from './hooks/useChannelHiddenListener';
import { useConnectionRecoveredListener } from './hooks/useConnectionRecoveredListener';
import { useUserPresenceChangedListener } from './hooks/useUserPresenceChangedListener';
import { usePaginatedChannels } from './hooks/usePaginatedChannels';
Expand Down Expand Up @@ -121,6 +122,7 @@ const ChannelList = (props) => {
props.onRemovedFromChannel,
);
useChannelDeletedListener(setChannels, props.onChannelDeleted);
useChannelHiddenListener(setChannels, props.onChannelHidden);
useChannelTruncatedListener(
setChannels,
props.onChannelTruncated,
Expand All @@ -134,18 +136,20 @@ const ChannelList = (props) => {
useEffect(() => {
/** @param {import('stream-chat').Event<string>} e */
const handleEvent = (e) => {
if (e.channel?.cid === channel?.cid) {
if (e?.cid === channel?.cid) {
setActiveChannel?.();
}
};

client.on('channel.deleted', handleEvent);
client.on('channel.hidden', handleEvent);

return () => {
client.off('channel.deleted', handleEvent);
client.off('channel.hidden', handleEvent);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [channel]);

// renders the channel preview or item
/** @param {import('stream-chat').Channel} item */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const useChannelDeletedListener = (setChannels, customHandler) => {
} else {
setChannels((channels) => {
const channelIndex = channels.findIndex(
(channel) => channel.cid === e.channel?.cid,
(channel) => channel.cid === e?.cid,
);

if (channelIndex < 0) return [...channels];
Expand Down
43 changes: 43 additions & 0 deletions src/components/ChannelList/hooks/useChannelHiddenListener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// @ts-check

import { useEffect, useContext } from 'react';
import { ChatContext } from '../../../context';

/**
* @typedef {import('stream-chat').Event<string>} ChannelHiddenEvent
* @typedef {React.Dispatch<React.SetStateAction<import('stream-chat').Channel[]>>} SetChannels
* @param {SetChannels} setChannels
* @param {(setChannels: SetChannels, event: ChannelHiddenEvent) => void} [customHandler]
*/
export const useChannelHiddenListener = (setChannels, customHandler) => {
const { client } = useContext(ChatContext);

useEffect(() => {
/** @param {import('stream-chat').Event<string>} e */
const handleEvent = (e) => {
if (customHandler && typeof customHandler === 'function') {
customHandler(setChannels, e);
} else {
setChannels((channels) => {
const channelIndex = channels.findIndex(
(channel) => channel.cid === e?.cid,
);
if (channelIndex < 0) return [...channels];

// Remove the hidden channel from the list.s
channels.splice(channelIndex, 1);

// eslint-disable-next-line consistent-return
return [...channels];
});
}
};

client.on('channel.hidden', handleEvent);

return () => {
client.off('channel.hidden', handleEvent);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [customHandler]);
};
4 changes: 4 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ export interface ChannelListProps {
thisArg: React.Dispatch<React.SetStateAction<Client.Channel[]>>,
e: Client.Event<string>,
): void;
onChannelHidden?(
thisArg: React.Dispatch<React.SetStateAction<Client.Channel[]>>,
e: Client.Event<string>,
): void;
onChannelUpdated?(
thisArg: React.Dispatch<React.SetStateAction<Client.Channel[]>>,
e: Client.Event<string>,
Expand Down

0 comments on commit 33739bd

Please sign in to comment.