Skip to content

Commit

Permalink
combine feed components
Browse files Browse the repository at this point in the history
  • Loading branch information
gkasdorf committed Oct 13, 2023
1 parent 1de9c5a commit 6294eee
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 85 deletions.
11 changes: 6 additions & 5 deletions src/components/Feed/components/Community/CommunityHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ function CommunityHeader(): React.JSX.Element | null {
const navigation = useNavigation<NativeStackNavigationProp<any>>();

// Get the title
const communityTitle = useCommunityName(params.id);
const communityBanner = useCommunityBanner(params.id);
const communityNsfw = useCommunityNsfw(params.id);
const communityTitle = useCommunityName(params?.id);
const communityBanner = useCommunityBanner(params?.id);
const communityNsfw = useCommunityNsfw(params?.id);

// Load the data
const { isLoading, data } = useLoadData<
GetCommunityResponse | number | undefined
>(
async () => {
return await instance.getCommunity(params.name);
if (params?.id == null) return;
return await instance.getCommunity(params?.name);
},
// We don't want to load the data again if we already have it
() => {
Expand All @@ -53,7 +54,7 @@ function CommunityHeader(): React.JSX.Element | null {
}

// If we don't have the data and we don't have the ID, then we can't display anything
if (!isLoading && data == null && params.id == null) {
if (!isLoading && data == null && params?.id == null) {
return null;
}

Expand Down
75 changes: 0 additions & 75 deletions src/components/Feed/components/CommunityFeed.tsx

This file was deleted.

25 changes: 22 additions & 3 deletions src/components/Feed/components/MainFeed.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback } from 'react';
import React, { useCallback, useEffect, useMemo } from 'react';
import instance from '@api/Instance';
import VStack from '@components/Common/Stack/VStack';
import { useRoute } from '@react-navigation/core';
Expand All @@ -7,6 +7,9 @@ import FeedItem from '@components/Feed/components/Feed/FeedItem';
import { FlatList, RefreshControl } from 'react-native';
import { useLoadData } from '@hooks/useLoadData';
import FeedLoadingIndicator from '@components/Feed/components/Feed/FeedLoadingIndicator';
import { cleanupPosts } from '@helpers/state';
import IGetPostOptions from '@api/common/types/IGetPostOptions';
import CommunityHeader from '@components/Feed/components/Community/CommunityHeader';

interface RenderItem {
item: number;
Expand All @@ -20,16 +23,29 @@ const keyExtractor = (item: number): string => item.toString();

export default function MainFeed(): React.JSX.Element {
// Get the feed ID
const { key } = useRoute();
const { key, params } = useRoute<any>();
const postIds = useFeedPostIds(key);
const nextPage = useFeedNextPage(key);

useEffect(() => {
return () => {
cleanupPosts(key);
};
}, []);

const defaultOptions = useMemo(
(): IGetPostOptions => ({
communityName: (params?.name as unknown as string) ?? undefined,
}),
[params?.name],
);

const { isLoading, isRefreshing, isError, append, refresh } = useLoadData(
async () => {
await instance.getPosts(
key,
{
page: 1,
...defaultOptions,
},
true,
);
Expand All @@ -39,6 +55,7 @@ export default function MainFeed(): React.JSX.Element {
const onEndReached = useCallback(() => {
append(async () => {
await instance.getPosts(key, {
...defaultOptions,
page: nextPage,
});
});
Expand All @@ -47,6 +64,7 @@ export default function MainFeed(): React.JSX.Element {
const onRefresh = useCallback(() => {
refresh(async () => {
await instance.getPosts(key, {
...defaultOptions,
page: 1,
refresh: true,
});
Expand All @@ -66,6 +84,7 @@ export default function MainFeed(): React.JSX.Element {
onEndReachedThreshold={0.5}
onEndReached={onEndReached}
removeClippedSubviews={true}
ListHeaderComponent={<CommunityHeader />}
ListFooterComponent={
<FeedLoadingIndicator loading={isLoading} error={isError} />
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/Navigation/MainScreens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { NativeStackNavigatorProps } from 'react-native-screens/lib/typescript/native-stack/types';
import FeedIndexScreen from '@components/Feed/screens/FeedIndexScreen';
import PostScreen from '@components/Post/screens/PostScreen';
import CommunityFeed from '@components/Feed/components/CommunityFeed';
import MainFeed from '@components/Feed/components/MainFeed';

export default function MainScreens(
stack: TypedNavigator<
Expand Down Expand Up @@ -48,7 +48,7 @@ export default function MainScreens(
/>
<stack.Screen
name="Community"
component={CommunityFeed}
component={MainFeed}
options={{
headerShown: true,
}}
Expand Down

0 comments on commit 6294eee

Please sign in to comment.