From 4a4a2d1a8713940a3d250779f1ba945d43dcbf2a Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 22 Jul 2023 14:06:15 -0500 Subject: [PATCH] Add useGroupStream hook --- app/soapbox/actions/streaming.ts | 4 ---- app/soapbox/api/hooks/index.ts | 1 + app/soapbox/api/hooks/streaming/useGroupStream.ts | 10 ++++++++++ app/soapbox/features/group/group-timeline.tsx | 11 +++-------- 4 files changed, 14 insertions(+), 12 deletions(-) create mode 100644 app/soapbox/api/hooks/streaming/useGroupStream.ts diff --git a/app/soapbox/actions/streaming.ts b/app/soapbox/actions/streaming.ts index 1367187eb..c96176960 100644 --- a/app/soapbox/actions/streaming.ts +++ b/app/soapbox/actions/streaming.ts @@ -193,14 +193,10 @@ const connectTimelineStream = ( const connectHashtagStream = (id: string, tag: string, accept: (status: APIEntity) => boolean) => connectTimelineStream(`hashtag:${id}`, `hashtag&tag=${tag}`, null, accept); -const connectGroupStream = (id: string) => - connectTimelineStream(`group:${id}`, `group&group=${id}`); - export { STREAMING_CHAT_UPDATE, STREAMING_FOLLOW_RELATIONSHIPS_UPDATE, connectTimelineStream, connectHashtagStream, - connectGroupStream, type TimelineStreamOpts, }; diff --git a/app/soapbox/api/hooks/index.ts b/app/soapbox/api/hooks/index.ts index 1983c59f2..2809bd8f8 100644 --- a/app/soapbox/api/hooks/index.ts +++ b/app/soapbox/api/hooks/index.ts @@ -50,5 +50,6 @@ export { useCommunityStream } from './streaming/useCommunityStream'; export { usePublicStream } from './streaming/usePublicStream'; export { useDirectStream } from './streaming/useDirectStream'; export { useListStream } from './streaming/useListStream'; +export { useGroupStream } from './streaming/useGroupStream'; export { useRemoteStream } from './streaming/useRemoteStream'; export { useNostrStream } from './streaming/useNostrStream'; \ No newline at end of file diff --git a/app/soapbox/api/hooks/streaming/useGroupStream.ts b/app/soapbox/api/hooks/streaming/useGroupStream.ts new file mode 100644 index 000000000..f9db3f69e --- /dev/null +++ b/app/soapbox/api/hooks/streaming/useGroupStream.ts @@ -0,0 +1,10 @@ +import { useTimelineStream } from './useTimelineStream'; + +function useGroupStream(groupId: string) { + return useTimelineStream( + `group:${groupId}`, + `group&group=${groupId}`, + ); +} + +export { useGroupStream }; \ No newline at end of file diff --git a/app/soapbox/features/group/group-timeline.tsx b/app/soapbox/features/group/group-timeline.tsx index 75b7a95d3..b10c41f9e 100644 --- a/app/soapbox/features/group/group-timeline.tsx +++ b/app/soapbox/features/group/group-timeline.tsx @@ -4,9 +4,8 @@ import { FormattedMessage, useIntl } from 'react-intl'; import { Link } from 'react-router-dom'; import { groupCompose, setGroupTimelineVisible, uploadCompose } from 'soapbox/actions/compose'; -import { connectGroupStream } from 'soapbox/actions/streaming'; import { expandGroupFeaturedTimeline, expandGroupTimeline } from 'soapbox/actions/timelines'; -import { useGroup } from 'soapbox/api/hooks'; +import { useGroup, useGroupStream } from 'soapbox/api/hooks'; import { Avatar, HStack, Icon, Stack, Text, Toggle } from 'soapbox/components/ui'; import ComposeForm from 'soapbox/features/compose/components/compose-form'; import { useAppDispatch, useAppSelector, useDraggedFiles, useOwnAccount } from 'soapbox/hooks'; @@ -49,16 +48,12 @@ const GroupTimeline: React.FC = (props) => { dispatch(setGroupTimelineVisible(composeId, !groupTimelineVisible)); }; + useGroupStream(groupId); + useEffect(() => { dispatch(expandGroupTimeline(groupId)); dispatch(expandGroupFeaturedTimeline(groupId)); dispatch(groupCompose(composeId, groupId)); - - const disconnect = dispatch(connectGroupStream(groupId)); - - return () => { - disconnect(); - }; }, [groupId]); if (!group) {