-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move things around, use ChannelState.membership instead
- Loading branch information
1 parent
bf2999c
commit d7a0d12
Showing
7 changed files
with
167 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/components/ChannelList/hooks/useChannelMembershipState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useEffect, useState } from 'react'; | ||
import type { Channel, ChannelState, ExtendableGenerics } from 'stream-chat'; | ||
|
||
import { useChatContext } from '../../../context'; | ||
|
||
export const useChannelMembershipState = <SCG extends ExtendableGenerics>( | ||
channel?: Channel<SCG>, | ||
) => { | ||
const [membership, setMembership] = useState<ChannelState<SCG>['membership']>( | ||
channel?.state.membership || {}, | ||
); | ||
|
||
const { client } = useChatContext<SCG>(); | ||
|
||
useEffect(() => { | ||
if (!channel) return; | ||
|
||
const subscriptions = ['member.updated'].map((v) => | ||
client.on(v, () => { | ||
setMembership(channel.state.membership); | ||
}), | ||
); | ||
|
||
return () => subscriptions.forEach((subscription) => subscription.unsubscribe()); | ||
}, [client, channel]); | ||
|
||
return membership; | ||
}; |
57 changes: 57 additions & 0 deletions
57
src/components/ChannelList/hooks/useMemberUpdatedListener.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useEffect } from 'react'; | ||
import type { Dispatch, SetStateAction } from 'react'; | ||
import type { Channel, ExtendableGenerics } from 'stream-chat'; | ||
|
||
import { useChatContext } from '../../../context'; | ||
import { findLastPinnedChannelIndex } from '../utils'; | ||
|
||
export const useMemberUpdatedListener = <SCG extends ExtendableGenerics>({ | ||
considerPinnedChannels = false, | ||
lockChannelOrder = false, | ||
setChannels, | ||
}: { | ||
setChannels: Dispatch<SetStateAction<Channel<SCG>[]>>; | ||
considerPinnedChannels?: boolean; | ||
lockChannelOrder?: boolean; | ||
}) => { | ||
const { client } = useChatContext<SCG>(); | ||
|
||
useEffect(() => { | ||
// do nothing if channel order is locked or pinned channels aren't being considered | ||
if (lockChannelOrder || !considerPinnedChannels) return; | ||
|
||
const subscription = client.on('member.updated', (e) => { | ||
if (!e.member || !e.channel_type) return; | ||
// const member = e.member; | ||
const channelType = e.channel_type; | ||
const channelId = e.channel_id; | ||
|
||
setChannels((currentChannels) => { | ||
const targetChannel = client.channel(channelType, channelId); | ||
// assumes that channel instances are not changing | ||
const targetChannelIndex = currentChannels.indexOf(targetChannel); | ||
const targetChannelExistsWithinList = targetChannelIndex >= 0; | ||
|
||
const newChannels = [...currentChannels]; | ||
|
||
if (targetChannelExistsWithinList) { | ||
newChannels.splice(targetChannelIndex, 1); | ||
} | ||
|
||
const lastPinnedChannelIndex = findLastPinnedChannelIndex({ channels: newChannels }) ?? 0; | ||
const newTargetChannelIndex = lastPinnedChannelIndex + 1; | ||
|
||
// skip re-render if the position of the channel does not change | ||
if (currentChannels[newTargetChannelIndex] === targetChannel) { | ||
return currentChannels; | ||
} | ||
|
||
newChannels.splice(newTargetChannelIndex, 0, targetChannel); | ||
|
||
return newChannels; | ||
}); | ||
}); | ||
|
||
return subscription.unsubscribe; | ||
}, [client, considerPinnedChannels, lockChannelOrder, setChannels]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters