Skip to content

Commit 566ca02

Browse files
brandon-futomarkharding
authored andcommitted
Support refreshing threads
Changelog: enhancement
1 parent 4118271 commit 566ca02

2 files changed

Lines changed: 21 additions & 14 deletions

File tree

apps/polycentric/src/features/post/ThreadList.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import { useCallback, useMemo, useState } from 'react';
1+
import { useMemo, useState } from 'react';
22
import { RefreshControl, useWindowDimensions, View } from 'react-native';
33
import { type PostData } from '@/src/common/lib/polycentric-hooks';
44
import { Post } from './Post';
55
import { useThread } from './hooks/useThread';
66
import { Atoms } from '@/src/common/theme';
77
import { ComposerInput } from '../composer';
88
import { List, ListProps } from '@/src/common/components/List';
9+
import { isWeb } from '@/src/common/util/platform';
910

1011
type ThreadListProps = Omit<ListProps<PostData>, 'data' | 'renderItem'> & {
1112
post: PostData;
1213
};
1314

1415
export function ThreadList({ post, ...rest }: ThreadListProps) {
15-
const [refreshing, setRefreshing] = useState(false);
1616
const { height: windowHeight } = useWindowDimensions();
1717

18-
const { thread } = useThread(post);
18+
const thread = useThread(post);
1919

2020
// Mount the subject by itself first.
2121
// Other items may or may not be available.
@@ -27,16 +27,11 @@ export function ThreadList({ post, ...rest }: ThreadListProps) {
2727

2828
const items = useMemo(() => {
2929
if (!isFirstLayoutComplete) return [post];
30-
return thread.length > 0 ? thread : [post];
31-
}, [isFirstLayoutComplete, thread, post]);
30+
return thread.items.length > 0 ? thread.items : [post];
31+
}, [isFirstLayoutComplete, thread.items, post]);
3232

3333
const subjectIndex = items.findIndex((p) => p.id === post.id);
3434

35-
const handleRefresh = useCallback(() => {
36-
setRefreshing(true);
37-
setTimeout(() => setRefreshing(false), 0);
38-
}, []);
39-
4035
return (
4136
<List
4237
{...rest}
@@ -67,7 +62,12 @@ export function ThreadList({ post, ...rest }: ThreadListProps) {
6762
}}
6863
ListFooterComponent={<View style={{ height: windowHeight }} />}
6964
refreshControl={
70-
<RefreshControl refreshing={refreshing} onRefresh={handleRefresh} />
65+
!isWeb ? (
66+
<RefreshControl
67+
refreshing={thread.isRefreshing}
68+
onRefresh={thread.refresh}
69+
/>
70+
) : undefined
7171
}
7272
/>
7373
);

apps/polycentric/src/features/post/hooks/useThread.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import {
1212
} from '@/src/common/lib/polycentric-hooks';
1313
import {
1414
getQueryCache,
15+
RefreshStrategy,
1516
setQueryCache,
1617
useQuery,
1718
} from '@/src/common/query/hooks/useQuery';
19+
import { FeedHookResult, NOOP } from '../../feed/hooks/types';
1820

1921
const DUMMY_EVENT_KEY: EventKey = {
2022
collection: COLLECTION.FEED,
@@ -33,7 +35,7 @@ export function threadQueryKey(parentId: string, limit = 0): string[] {
3335
export function useThread(
3436
post: PostData | undefined,
3537
options?: { limit?: number },
36-
): { thread: PostData[]; isLoading: boolean; error: Error | null } {
38+
): FeedHookResult {
3739
const eventKey: EventKey = useMemo(() => {
3840
if (!post) return DUMMY_EVENT_KEY;
3941

@@ -57,7 +59,7 @@ export function useThread(
5759
!!post,
5860
);
5961

60-
const thread = useMemo(() => {
62+
const items = useMemo(() => {
6163
if (!query.data) return [];
6264
const response = v2.GetPostThreadResponse.fromBinary(
6365
new Uint8Array(query.data),
@@ -71,9 +73,14 @@ export function useThread(
7173
}, [query.data]);
7274

7375
return {
74-
thread,
76+
items,
7577
isLoading: query.isLoading,
78+
isRefreshing: query.hasPendingRefresh,
7679
error: query.error ? new Error(query.error) : null,
80+
refresh: () => query.refresh(RefreshStrategy.Lazy),
81+
// TODO: paginate threads
82+
loadMore: NOOP,
83+
hasMore: false,
7784
};
7885
}
7986

0 commit comments

Comments
 (0)