Add useCommunityStream hook, refresh socket when timelineId or path changes
This commit is contained in:
parent
757f6600f8
commit
3cef200a44
5 changed files with 23 additions and 14 deletions
|
@ -190,9 +190,6 @@ const connectTimelineStream = (
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const connectCommunityStream = ({ onlyMedia }: Record<string, any> = {}) =>
|
|
||||||
connectTimelineStream(`community${onlyMedia ? ':media' : ''}`, `public:local${onlyMedia ? ':media' : ''}`);
|
|
||||||
|
|
||||||
const connectPublicStream = ({ onlyMedia }: Record<string, any> = {}) =>
|
const connectPublicStream = ({ onlyMedia }: Record<string, any> = {}) =>
|
||||||
connectTimelineStream(`public${onlyMedia ? ':media' : ''}`, `public${onlyMedia ? ':media' : ''}`);
|
connectTimelineStream(`public${onlyMedia ? ':media' : ''}`, `public${onlyMedia ? ':media' : ''}`);
|
||||||
|
|
||||||
|
@ -215,7 +212,6 @@ export {
|
||||||
STREAMING_CHAT_UPDATE,
|
STREAMING_CHAT_UPDATE,
|
||||||
STREAMING_FOLLOW_RELATIONSHIPS_UPDATE,
|
STREAMING_FOLLOW_RELATIONSHIPS_UPDATE,
|
||||||
connectTimelineStream,
|
connectTimelineStream,
|
||||||
connectCommunityStream,
|
|
||||||
connectPublicStream,
|
connectPublicStream,
|
||||||
connectRemoteStream,
|
connectRemoteStream,
|
||||||
connectHashtagStream,
|
connectHashtagStream,
|
||||||
|
|
|
@ -46,4 +46,5 @@ export { useUpdateGroupTag } from './groups/useUpdateGroupTag';
|
||||||
|
|
||||||
// Streaming
|
// Streaming
|
||||||
export { useUserStream } from './streaming/useUserStream';
|
export { useUserStream } from './streaming/useUserStream';
|
||||||
|
export { useCommunityStream } from './streaming/useCommunityStream';
|
||||||
export { useNostrStream } from './streaming/useNostrStream';
|
export { useNostrStream } from './streaming/useNostrStream';
|
14
app/soapbox/api/hooks/streaming/useCommunityStream.ts
Normal file
14
app/soapbox/api/hooks/streaming/useCommunityStream.ts
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
import { useTimelineStream } from './useTimelineStream';
|
||||||
|
|
||||||
|
interface UseCommunityStreamOpts {
|
||||||
|
onlyMedia?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
function useCommunityStream({ onlyMedia }: UseCommunityStreamOpts = {}) {
|
||||||
|
return useTimelineStream(
|
||||||
|
`community${onlyMedia ? ':media' : ''}`,
|
||||||
|
`public:local${onlyMedia ? ':media' : ''}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export { useCommunityStream };
|
|
@ -6,6 +6,7 @@ import { getAccessToken } from 'soapbox/utils/auth';
|
||||||
|
|
||||||
function useTimelineStream(...args: Parameters<typeof connectTimelineStream>) {
|
function useTimelineStream(...args: Parameters<typeof connectTimelineStream>) {
|
||||||
// TODO: get rid of streaming.ts and move the actual opts here.
|
// TODO: get rid of streaming.ts and move the actual opts here.
|
||||||
|
const [timelineId, path] = args;
|
||||||
const { enabled = true } = args[4] ?? {};
|
const { enabled = true } = args[4] ?? {};
|
||||||
|
|
||||||
const dispatch = useAppDispatch();
|
const dispatch = useAppDispatch();
|
||||||
|
@ -31,7 +32,7 @@ function useTimelineStream(...args: Parameters<typeof connectTimelineStream>) {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
connect();
|
connect();
|
||||||
return disconnect;
|
return disconnect;
|
||||||
}, [accessToken, streamingUrl, enabled]);
|
}, [accessToken, streamingUrl, timelineId, path, enabled]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
disconnect,
|
disconnect,
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import React, { useEffect } from 'react';
|
import React, { useEffect } from 'react';
|
||||||
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
|
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
|
||||||
|
|
||||||
import { connectCommunityStream } from 'soapbox/actions/streaming';
|
|
||||||
import { expandCommunityTimeline } from 'soapbox/actions/timelines';
|
import { expandCommunityTimeline } from 'soapbox/actions/timelines';
|
||||||
|
import { useCommunityStream } from 'soapbox/api/hooks';
|
||||||
import PullToRefresh from 'soapbox/components/pull-to-refresh';
|
import PullToRefresh from 'soapbox/components/pull-to-refresh';
|
||||||
import { Column } from 'soapbox/components/ui';
|
import { Column } from 'soapbox/components/ui';
|
||||||
import { useAppSelector, useAppDispatch, useSettings } from 'soapbox/hooks';
|
import { useAppSelector, useAppDispatch, useSettings } from 'soapbox/hooks';
|
||||||
|
@ -18,7 +18,7 @@ const CommunityTimeline = () => {
|
||||||
const dispatch = useAppDispatch();
|
const dispatch = useAppDispatch();
|
||||||
|
|
||||||
const settings = useSettings();
|
const settings = useSettings();
|
||||||
const onlyMedia = settings.getIn(['community', 'other', 'onlyMedia']);
|
const onlyMedia = !!settings.getIn(['community', 'other', 'onlyMedia'], false);
|
||||||
const next = useAppSelector(state => state.timelines.get('community')?.next);
|
const next = useAppSelector(state => state.timelines.get('community')?.next);
|
||||||
|
|
||||||
const timelineId = 'community';
|
const timelineId = 'community';
|
||||||
|
@ -28,16 +28,13 @@ const CommunityTimeline = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleRefresh = () => {
|
const handleRefresh = () => {
|
||||||
return dispatch(expandCommunityTimeline({ onlyMedia } as any));
|
return dispatch(expandCommunityTimeline({ onlyMedia }));
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useCommunityStream({ onlyMedia });
|
||||||
dispatch(expandCommunityTimeline({ onlyMedia } as any));
|
|
||||||
const disconnect = dispatch(connectCommunityStream({ onlyMedia } as any));
|
|
||||||
|
|
||||||
return () => {
|
useEffect(() => {
|
||||||
disconnect();
|
dispatch(expandCommunityTimeline({ onlyMedia }));
|
||||||
};
|
|
||||||
}, [onlyMedia]);
|
}, [onlyMedia]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
Loading…
Reference in a new issue