Skip to content

Commit

Permalink
Merge pull request #12 from farcasterxyz/nickcherry/pull-to-refresh-feed
Browse files Browse the repository at this point in the history
[mobile] Pull to Refresh Feed
  • Loading branch information
nickcherry authored Jan 25, 2024
2 parents 96e9f27 + 584f10d commit d49f578
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
14 changes: 7 additions & 7 deletions mobile/src/components/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ const queryClient = new QueryClient();

export function App() {
return (
<NavigationContainer>
<QueryClientProvider client={queryClient}>
<StatusBar style="auto" />
<SafeAreaProvider>
<SafeAreaProvider>
<NavigationContainer>
<QueryClientProvider client={queryClient}>
<StatusBar style="auto" />
<Suspense fallback={<FullscreenLoader />}>
<Navigator />
</Suspense>
</SafeAreaProvider>
</QueryClientProvider>
</NavigationContainer>
</QueryClientProvider>
</NavigationContainer>
</SafeAreaProvider>
);
}
6 changes: 5 additions & 1 deletion mobile/src/components/navigation/Navigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ const Stack = createNativeStackNavigator();
export function Navigator() {
return (
<Stack.Navigator>
<Stack.Screen name="Feed" component={FeedScreen} />
<Stack.Screen
name="Feed"
component={FeedScreen}
options={{ headerShown: false }}
/>
</Stack.Navigator>
);
}
31 changes: 25 additions & 6 deletions mobile/src/screens/FeedScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { baseApiUrl } from '@constants/api';
import { FeedApiResponse } from '@shared/types/api';
import { Cast as CastType } from '@shared/types/models';
import { FlashList } from '@shopify/flash-list';
import { useSuspenseQuery } from '@tanstack/react-query';
import { useQueryClient, useSuspenseQuery } from '@tanstack/react-query';
import { buildScreen } from '@utils/buildScreen';
import { View } from 'react-native';
import { useCallback, useState } from 'react';

const feedKey = ['feed'];

async function fetchFeed(): Promise<FeedApiResponse> {
const res = await fetch(`${baseApiUrl}/feed?fid=145`);
Expand All @@ -17,14 +19,31 @@ function renderItem({ item }: { item: CastType }) {
}

export const FeedScreen = buildScreen(() => {
const queryClient = useQueryClient();

const [isRefreshing, setIsRefreshing] = useState(false);

const { feed } = useSuspenseQuery({
queryKey: ['feed'],
queryKey: feedKey,
queryFn: fetchFeed,
}).data;

const onRefresh = useCallback(async () => {
try {
setIsRefreshing(true);
queryClient.setQueryData(feedKey, await fetchFeed());
} finally {
setIsRefreshing(false);
}
}, [queryClient]);

return (
<View className="h-full">
<FlashList data={feed} renderItem={renderItem} estimatedItemSize={190} />
</View>
<FlashList
data={feed}
renderItem={renderItem}
estimatedItemSize={190}
refreshing={isRefreshing}
onRefresh={onRefresh}
/>
);
});
9 changes: 6 additions & 3 deletions mobile/src/utils/buildScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { FullscreenLoader } from '@components/loader/FullscreenLoader';
import { FC, Suspense } from 'react';
import { SafeAreaView } from 'react-native';

export function buildScreen<Props extends Record<string, unknown>>(
Component: FC<Props>,
) {
return (props: Props) => (
<Suspense fallback={<FullscreenLoader />}>
{<Component {...props} />}
</Suspense>
<SafeAreaView className="flex-1">
<Suspense fallback={<FullscreenLoader />}>
{<Component {...props} />}
</Suspense>
</SafeAreaView>
);
}
2 changes: 1 addition & 1 deletion web/src/lib/services/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { processEmbeds } from './embeds';

export async function getFeed({
fid,
limit = 100,
limit = 50,
}: {
fid: string;
limit?: number;
Expand Down

0 comments on commit d49f578

Please sign in to comment.