Skip to content

Commit

Permalink
Merge pull request #2273 from daostack/feature/CW-2269-bg-loading
Browse files Browse the repository at this point in the history
Background Loading next streams and their messages in inbox and spaces #2269
  • Loading branch information
andreymikhadyuk authored Oct 30, 2023
2 parents 6042fa0 + df853d6 commit e61eb74
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/pages/commonFeed/CommonFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ const CommonFeedComponent: FC<CommonFeedProps> = (props) => {
loading: areCommonFeedItemsLoading,
hasMore: hasMoreCommonFeedItems,
fetch: fetchCommonFeedItems,
batchNumber,
} = useCommonFeedItems(commonId, commonFeedItemIdsForNotListening);

const {
Expand Down Expand Up @@ -508,6 +509,7 @@ const CommonFeedComponent: FC<CommonFeedProps> = (props) => {
topFeedItems={topFeedItems}
feedItems={commonFeedItems}
loading={areCommonFeedItemsLoading}
batchNumber={batchNumber}
onFetchNext={fetchMoreCommonFeedItems}
renderFeedItemBaseContent={renderFeedItemBaseContent}
onFeedItemUpdate={handleFeedItemUpdate}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@import "../../../../styles/sizes";

.content {
position: relative;
flex: 1;
padding: 0 1.5rem;
display: flex;
Expand All @@ -17,6 +18,11 @@
justify-content: center;
}

.infiniteScrollMarker {
position: absolute;
bottom: 50rem;
}

.emptyText {
margin: 0;
font-weight: 500;
Expand Down
18 changes: 18 additions & 0 deletions src/pages/commonFeed/components/FeedLayout/FeedLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import {
MobileProfile,
SplitView,
} from "./components";
import { BATCHES_AMOUNT_TO_PRELOAD } from "./constants";
import { useUserForProfile } from "./hooks";
import {
checkShouldAutoOpenPreview,
Expand Down Expand Up @@ -114,6 +115,7 @@ interface FeedLayoutProps {
topFeedItems?: FeedLayoutItem[];
loading: boolean;
shouldHideContent?: boolean;
batchNumber?: number;
onFetchNext: (feedItemId?: string) => void;
renderFeedItemBaseContent: (props: FeedItemBaseContentProps) => ReactNode;
renderChatChannelItem?: (props: ChatChannelFeedLayoutItemProps) => ReactNode;
Expand Down Expand Up @@ -153,6 +155,7 @@ const FeedLayout: ForwardRefRenderFunction<FeedLayoutRef, FeedLayoutProps> = (
topFeedItems = [],
loading,
shouldHideContent = false,
batchNumber,
onFetchNext,
renderFeedItemBaseContent,
renderChatChannelItem,
Expand Down Expand Up @@ -603,6 +606,16 @@ const FeedLayout: ForwardRefRenderFunction<FeedLayoutRef, FeedLayoutProps> = (
}
}, [isTabletView, shouldAutoExpandItem, activeFeedItemId]);

useEffect(() => {
if (
batchNumber &&
batchNumber >= 1 &&
batchNumber <= BATCHES_AMOUNT_TO_PRELOAD
) {
onFetchNext();
}
}, [batchNumber]);

useImperativeHandle(
ref,
() => ({
Expand Down Expand Up @@ -640,6 +653,11 @@ const FeedLayout: ForwardRefRenderFunction<FeedLayoutRef, FeedLayoutProps> = (
{topContent}
{isContentEmpty && <p className={styles.emptyText}>{emptyText}</p>}
<InfiniteScroll
markerClassName={
allFeedItems && allFeedItems.length > 7
? styles.infiniteScrollMarker
: ""
}
onFetchNext={onFetchNext}
isLoading={loading}
loaderDelay={LOADER_APPEARANCE_DELAY}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const BATCHES_AMOUNT_TO_PRELOAD = 2;
2 changes: 2 additions & 0 deletions src/pages/inbox/BaseInbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const InboxPage: FC<InboxPageProps> = (props) => {
loading: areInboxItemsLoading,
hasMore: hasMoreInboxItems,
fetch: fetchInboxItems,
batchNumber,
} = useInboxItems(feedItemIdsForNotListening, {
unread: queryParams.unread === "true",
});
Expand Down Expand Up @@ -257,6 +258,7 @@ const InboxPage: FC<InboxPageProps> = (props) => {
feedItems={inboxItems}
loading={areInboxItemsLoading || !user}
shouldHideContent={!user}
batchNumber={batchNumber}
onFetchNext={fetchMoreInboxItems}
renderFeedItemBaseContent={renderFeedItemBaseContent}
renderChatChannelItem={renderChatChannelItem}
Expand Down
3 changes: 2 additions & 1 deletion src/shared/hooks/useCases/useCommonFeedItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { useDispatch, useSelector } from "react-redux";
import { CommonFeedService } from "@/services";
import { commonActions, FeedItems, selectFeedItems } from "@/store/states";

interface Return extends Pick<FeedItems, "data" | "loading" | "hasMore"> {
interface Return
extends Pick<FeedItems, "data" | "loading" | "hasMore" | "batchNumber"> {
fetch: (feedItemId?: string) => void;
}

Expand Down
3 changes: 2 additions & 1 deletion src/shared/hooks/useCases/useInboxItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { FeedLayoutItemWithFollowData } from "@/shared/interfaces";
import { FeedItemFollow, FeedItemFollowWithMetadata } from "@/shared/models";
import { inboxActions, InboxItems, selectInboxItems } from "@/store/states";

interface Return extends Pick<InboxItems, "data" | "loading" | "hasMore"> {
interface Return
extends Pick<InboxItems, "data" | "loading" | "hasMore" | "batchNumber"> {
fetch: () => void;
}

Expand Down
6 changes: 4 additions & 2 deletions src/shared/ui-kit/InfiniteScroll/InfiniteScroll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import styles from "./InfiniteScroll.module.scss";
interface InfiniteScrollProps {
isLoading: boolean;
loaderDelay?: number;
markerClassName?: string;
onFetchNext: () => void;
}

const InfiniteScroll: FC<InfiniteScrollProps> = (props) => {
const { isLoading, loaderDelay, onFetchNext, children } = props;
const { isLoading, loaderDelay, markerClassName, onFetchNext, children } =
props;
const [isInnerLoading, setIsInnerLoading] = useState(isLoading);
const markerRef = useRef<HTMLDivElement>(null);
const isMarkerOnScreen = useIntersectionObserver(markerRef.current);
Expand Down Expand Up @@ -39,7 +41,7 @@ const InfiniteScroll: FC<InfiniteScrollProps> = (props) => {
return (
<>
{children}
<div ref={markerRef} />
<div className={markerClassName} ref={markerRef} />
{isLoading && (
<div className={styles.loaderWrapper}>
<Loader delay={loaderDelay} />
Expand Down
2 changes: 1 addition & 1 deletion src/store/states/common/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const getFeedItems = createAsyncAction(
feedItemId?: string;
limit?: number;
},
Omit<FeedItems, "loading">,
Omit<FeedItems, "loading" | "batchNumber">,
Error,
string
>();
Expand Down
2 changes: 2 additions & 0 deletions src/store/states/common/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const initialFeedItems: FeedItems = {
hasMore: false,
firstDocTimestamp: null,
lastDocTimestamp: null,
batchNumber: 0,
};

const initialPinnedFeedItems: PinnedFeedItems = {
Expand Down Expand Up @@ -428,6 +429,7 @@ export const reducer = createReducer<CommonState, Action>(initialState)
data:
payloadData && (nextState.feedItems.data || []).concat(payloadData),
loading: false,
batchNumber: nextState.feedItems.batchNumber + 1,
};
}),
)
Expand Down
1 change: 1 addition & 0 deletions src/store/states/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface FeedItems {
hasMore: boolean;
firstDocTimestamp: Timestamp | null;
lastDocTimestamp: Timestamp | null;
batchNumber: number;
}

export interface PinnedFeedItems {
Expand Down
2 changes: 1 addition & 1 deletion src/store/states/inbox/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const getInboxItems = createAsyncAction(
limit?: number;
unread?: boolean;
},
Omit<InboxItems, "loading">,
Omit<InboxItems, "loading" | "batchNumber">,
Error,
string
>();
Expand Down
2 changes: 2 additions & 0 deletions src/store/states/inbox/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const initialInboxItems: InboxItems = {
hasMore: false,
firstDocTimestamp: null,
lastDocTimestamp: null,
batchNumber: 0,
};

const initialState: InboxState = {
Expand Down Expand Up @@ -432,6 +433,7 @@ export const reducer = createReducer<InboxState, Action>(initialState)
...payload,
data: payloadData && (nextState.items.data || []).concat(payloadData),
loading: false,
batchNumber: nextState.items.batchNumber + 1,
};
}),
)
Expand Down
1 change: 1 addition & 0 deletions src/store/states/inbox/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface InboxItems {
hasMore: boolean;
firstDocTimestamp: Timestamp | null;
lastDocTimestamp: Timestamp | null;
batchNumber: number;
}

export interface InboxState {
Expand Down

0 comments on commit e61eb74

Please sign in to comment.