|
1 | | -import { useCallback, useEffect, useRef, useState } from 'react'; |
2 | | -import { QueryStatus, v2 } from '@polycentric/react-native'; |
3 | | -import { |
4 | | - decodeV2PostBundle, |
5 | | - usePolycentricContext, |
6 | | - type PostData, |
7 | | -} from '@/src/common/lib/polycentric-hooks'; |
8 | | -import { EMPTY_POSTS, NOOP, type FeedHookResult } from './types'; |
9 | | - |
10 | | -type Sub = { unsubscribe: () => void }; |
| 1 | +import { useCallback, useEffect, useMemo, useRef } from 'react'; |
| 2 | +import { Query } from '@polycentric/react-native'; |
| 3 | +import { usePolycentricContext } from '@/src/common/lib/polycentric-hooks'; |
| 4 | +import { type FeedHookResult, NOOP } from './types'; |
| 5 | +import { EMPTY_FEED, useFeedsStore } from './useFeeds'; |
11 | 6 |
|
12 | 7 | export function useExploreFeed(options?: { |
13 | 8 | perServerLimit?: number; |
14 | 9 | enabled?: boolean; |
15 | 10 | }): FeedHookResult { |
16 | 11 | const { client } = usePolycentricContext(); |
17 | 12 | const enabled = options?.enabled ?? true; |
18 | | - const [items, setItems] = useState<PostData[]>(EMPTY_POSTS); |
19 | | - const [isLoading, setIsLoading] = useState(false); |
20 | | - const [error, setError] = useState<Error | null>(null); |
21 | | - |
22 | | - // Hold the live subscription so we can cancel on unmount / refresh. |
23 | | - // The rust core fans out to every configured server and emits each |
24 | | - // merged response as it arrives. |
25 | | - const subscriptionRef = useRef<Sub | null>(null); |
26 | | - |
27 | | - const cleanup = useCallback(() => { |
28 | | - subscriptionRef.current?.unsubscribe(); |
29 | | - subscriptionRef.current = null; |
30 | | - }, []); |
31 | | - |
32 | | - const fetchFeed = useCallback(() => { |
33 | | - cleanup(); |
34 | | - setItems(EMPTY_POSTS); |
35 | | - setError(null); |
36 | | - setIsLoading(true); |
37 | 13 |
|
38 | | - // Rust-side merge_fn already dedupes by EventKey — each next() |
39 | | - // carries the full merged feed plus a status. `Loading` stays in |
40 | | - // effect until the last per-server response arrives. |
41 | | - const observable = client.core.getExploreFeed( |
42 | | - client.activeIdentityKey ?? undefined, |
43 | | - undefined, |
44 | | - undefined, |
45 | | - undefined, |
46 | | - undefined, |
47 | | - ); |
48 | | - subscriptionRef.current = observable.subscribe({ |
49 | | - next: (result) => { |
50 | | - if (result.data) { |
51 | | - const response = v2.GetFeedResponse.fromBinary( |
52 | | - new Uint8Array(result.data), |
53 | | - ); |
54 | | - const posts: PostData[] = []; |
55 | | - for (const bundle of response.eventBundles) { |
56 | | - const decoded = decodeV2PostBundle(bundle); |
57 | | - if (decoded) posts.push(decoded); |
58 | | - } |
59 | | - setItems(posts); |
60 | | - } |
61 | | - setIsLoading(result.status === QueryStatus.Loading); |
62 | | - }, |
63 | | - error: (message: string) => { |
64 | | - console.warn('useExploreFeed error:', message); |
65 | | - setError(new Error(message)); |
66 | | - }, |
67 | | - complete: () => { |
68 | | - setIsLoading(false); |
69 | | - }, |
70 | | - }); |
71 | | - }, [client, cleanup]); |
| 14 | + const identity = client.activeIdentityKey ?? ''; |
| 15 | + const feedKey = `explore:${identity}`; |
| 16 | + const queryKey = useMemo(() => ['explore_feed', identity], [identity]); |
| 17 | + const query = useMemo( |
| 18 | + () => |
| 19 | + new Query.GetExploreFeed({ |
| 20 | + identity: identity === '' ? undefined : identity, |
| 21 | + }), |
| 22 | + [identity], |
| 23 | + ); |
| 24 | + |
| 25 | + const feed = useFeedsStore((s) => s.feeds.get(feedKey) ?? EMPTY_FEED); |
| 26 | + const subscribe = useFeedsStore((s) => s.subscribe); |
| 27 | + |
| 28 | + // Re-subscribe on every mount so the rust observable re-emits cached |
| 29 | + // state (instant) and `FetchMode::Default` triggers a fresh fan-out. |
| 30 | + // State persists in the store across mount/unmount. |
| 31 | + const unsubscribeRef = useRef<(() => void) | null>(null); |
72 | 32 |
|
73 | 33 | useEffect(() => { |
74 | | - if (enabled) fetchFeed(); |
75 | | - return cleanup; |
76 | | - }, [enabled, fetchFeed, cleanup]); |
| 34 | + if (!enabled) return; |
| 35 | + unsubscribeRef.current = subscribe(client, feedKey, queryKey, query); |
| 36 | + return () => { |
| 37 | + unsubscribeRef.current?.(); |
| 38 | + unsubscribeRef.current = null; |
| 39 | + }; |
| 40 | + }, [enabled, subscribe, client, feedKey, queryKey, query]); |
| 41 | + |
| 42 | + const refresh = useCallback(() => { |
| 43 | + unsubscribeRef.current?.(); |
| 44 | + unsubscribeRef.current = subscribe(client, feedKey, queryKey, query); |
| 45 | + }, [subscribe, client, feedKey, queryKey, query]); |
77 | 46 |
|
78 | 47 | return { |
79 | | - items, |
80 | | - isLoading, |
81 | | - error, |
| 48 | + items: feed.items, |
| 49 | + isLoading: feed.isLoading, |
| 50 | + error: feed.error, |
82 | 51 | loadMore: NOOP, |
83 | 52 | hasMore: false, |
84 | | - refresh: fetchFeed, |
| 53 | + refresh, |
85 | 54 | }; |
86 | 55 | } |
0 commit comments