parent
bea4707743
commit
77f0f4d377
@ -0,0 +1,11 @@
|
||||
import { useFeatures } from 'soapbox/hooks';
|
||||
|
||||
import { useTimelineStream } from './useTimelineStream';
|
||||
|
||||
function useNostrStream() {
|
||||
const features = useFeatures();
|
||||
const enabled = features.nostrSign && Boolean(window.nostr);
|
||||
return useTimelineStream('nostr', 'nostr', null, null, { enabled });
|
||||
}
|
||||
|
||||
export { useNostrStream };
|
@ -0,0 +1,41 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
|
||||
import { connectTimelineStream } from 'soapbox/actions/streaming';
|
||||
import { useAppDispatch, useAppSelector, useInstance } from 'soapbox/hooks';
|
||||
import { getAccessToken } from 'soapbox/utils/auth';
|
||||
|
||||
function useTimelineStream(...args: Parameters<typeof connectTimelineStream>) {
|
||||
// TODO: get rid of streaming.ts and move the actual opts here.
|
||||
const { enabled = true } = args[4] ?? {};
|
||||
|
||||
const dispatch = useAppDispatch();
|
||||
const instance = useInstance();
|
||||
const stream = useRef<(() => void) | null>(null);
|
||||
|
||||
const accessToken = useAppSelector(getAccessToken);
|
||||
const streamingUrl = instance.urls.get('streaming_api');
|
||||
|
||||
const connect = () => {
|
||||
if (enabled && accessToken && streamingUrl && !stream.current) {
|
||||
stream.current = dispatch(connectTimelineStream(...args));
|
||||
}
|
||||
};
|
||||
|
||||
const disconnect = () => {
|
||||
if (stream.current) {
|
||||
stream.current();
|
||||
stream.current = null;
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
connect();
|
||||
return disconnect;
|
||||
}, [accessToken, streamingUrl, enabled]);
|
||||
|
||||
return {
|
||||
disconnect,
|
||||
};
|
||||
}
|
||||
|
||||
export { useTimelineStream };
|
@ -0,0 +1,22 @@
|
||||
import { fetchAnnouncements } from 'soapbox/actions/announcements';
|
||||
import { expandNotifications } from 'soapbox/actions/notifications';
|
||||
import { expandHomeTimeline } from 'soapbox/actions/timelines';
|
||||
import { useStatContext } from 'soapbox/contexts/stat-context';
|
||||
|
||||
import { useTimelineStream } from './useTimelineStream';
|
||||
|
||||
import type { AppDispatch } from 'soapbox/store';
|
||||
|
||||
function useUserStream() {
|
||||
const statContext = useStatContext();
|
||||
return useTimelineStream('home', 'user', refresh, null, { statContext });
|
||||
}
|
||||
|
||||
/** Refresh home timeline and notifications. */
|
||||
function refresh(dispatch: AppDispatch, done?: () => void) {
|
||||
return dispatch(expandHomeTimeline({}, () =>
|
||||
dispatch(expandNotifications({}, () =>
|
||||
dispatch(fetchAnnouncements(done))))));
|
||||
}
|
||||
|
||||
export { useUserStream };
|
Loading…
Reference in new issue