Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 122 additions & 41 deletions packages/shared/src/components/Feed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import useFeedInfiniteScroll, {
InfiniteScrollScreenOffset,
} from '../hooks/feed/useFeedInfiniteScroll';
import FeedItemComponent, { getFeedItemKey } from './FeedItemComponent';
import { computeColSpans } from '../lib/feedHighlightColSpan';
import type { FeaturedWideColSpan } from './cards/article/ArticleFeaturedWideGridCard';
import { useLogContext } from '../contexts/LogContext';
import { feedLogExtra, postLogEvent } from '../lib/feed';
import { usePostModalNavigation } from '../hooks/usePostModalNavigation';
Expand Down Expand Up @@ -67,6 +69,7 @@ import {
briefCardFeedFeature,
briefFeedEntrypointPage,
featureFeedAdTemplate,
featurePostHighlightCards,
} from '../lib/featureManagement';
import { useNewD1ExperienceFeature } from '../hooks/useNewD1ExperienceFeature';
import type { AwardProps } from '../graphql/njord';
Expand Down Expand Up @@ -379,6 +382,60 @@ export default function Feed<T>({
firstSlotOffset: Number(showProfileCompletionCard || showBriefCard),
});

const isMobileViewport = !isTabletViewport;
const isListContext = useList || shouldUseListFeedLayout;
const canRenderHighlightCards =
!isMobileViewport && !isListContext && virtualizedNumCards > 1;
const { value: isPostHighlightCardsEnabled } = useConditionalFeature({
feature: featurePostHighlightCards,
shouldEvaluate: canRenderHighlightCards,
});
const isHighlightCardLayoutEnabled =
canRenderHighlightCards && isPostHighlightCardsEnabled;
const earlyCurrentPageSize = pageSize ?? currentSettings.pageSize;
const earlyShowPromoBanner = !!briefBannerPage;
const earlyShowFirstSlotCard = showProfileCompletionCard || showBriefCard;
const earlyColumnsDiffWithPage = earlyCurrentPageSize % virtualizedNumCards;
const earlyIndexWhenShowingPromoBanner =
earlyCurrentPageSize * Number(briefBannerPage) -
earlyColumnsDiffWithPage * Number(briefBannerPage) -
Number(earlyShowFirstSlotCard);

const fullRowInsertionBeforeIndex = useMemo(() => {
const set = new Set<number>();
if (earlyShowPromoBanner) {
set.add(earlyIndexWhenShowingPromoBanner);
}
if (shouldShowInFeedHero) {
set.add(adjustedHeroInsertIndex);
}
return set;
}, [
earlyShowPromoBanner,
earlyIndexWhenShowingPromoBanner,
shouldShowInFeedHero,
adjustedHeroInsertIndex,
]);

const itemColSpans = useMemo(
() =>
computeColSpans(items, {
numCards: virtualizedNumCards,
isMobile: isMobileViewport,
isList: isListContext,
isEnabled: isHighlightCardLayoutEnabled,
fullRowInsertionBeforeIndex,
}),
[
items,
virtualizedNumCards,
isMobileViewport,
isListContext,
isHighlightCardLayoutEnabled,
fullRowInsertionBeforeIndex,
],
);

useMutationSubscription({
matcher: ({ mutation }) => {
const [requestKey] = Array.isArray(mutation.options.mutationKey)
Expand Down Expand Up @@ -716,45 +773,14 @@ export default function Feed<T>({
}}
/>
)}
{items.map((item, index) => (
<FeedCardContext.Provider
key={getFeedItemKey(item, index)}
value={{
boostedBy: isBoostedPostAd(item)
? item.ad.data?.post?.author || item.ad.data?.post?.scout
: undefined,
}}
>
{showPromoBanner && index === indexWhenShowingPromoBanner && (
<BriefBannerFeed
style={{
gridColumn: !shouldUseListFeedLayout
? `span ${virtualizedNumCards}`
: undefined,
}}
/>
)}
{shouldShowInFeedHero && index === adjustedHeroInsertIndex && (
<div
style={{
gridColumn: !shouldUseListFeedLayout
? `span ${virtualizedNumCards}`
: undefined,
}}
>
<TopHero
className="pt-0"
title={readingReminderTitle}
subtitle={readingReminderSubtitle}
onCtaClick={() =>
onEnableHero(NotificationCtaPlacement.InFeedHero)
}
onClose={() =>
onDismissHero(NotificationCtaPlacement.InFeedHero)
}
/>
</div>
)}
{items.map((item, index) => {
const colSpan = itemColSpans[index] ?? 1;
const isWidened = colSpan > 1;
const wideColSpan =
isWidened && (colSpan === 2 || colSpan === 3 || colSpan === 4)
? (colSpan as FeaturedWideColSpan)
: undefined;
const itemNode = (
<FeedItemComponent
item={item}
index={index}
Expand All @@ -777,9 +803,64 @@ export default function Feed<T>({
onReadArticleClick={onReadArticleClick}
virtualizedNumCards={virtualizedNumCards}
disableAdRefresh={disableAdRefresh}
wideColSpan={wideColSpan}
/>
</FeedCardContext.Provider>
))}
);

return (
<FeedCardContext.Provider
key={getFeedItemKey(item, index)}
value={{
boostedBy: isBoostedPostAd(item)
? item.ad.data?.post?.author || item.ad.data?.post?.scout
: undefined,
}}
>
{showPromoBanner && index === indexWhenShowingPromoBanner && (
<BriefBannerFeed
style={{
gridColumn: !shouldUseListFeedLayout
? `span ${virtualizedNumCards}`
: undefined,
}}
/>
)}
{shouldShowInFeedHero &&
index === adjustedHeroInsertIndex && (
<div
style={{
gridColumn: !shouldUseListFeedLayout
? `span ${virtualizedNumCards}`
: undefined,
}}
>
<TopHero
className="pt-0"
title={readingReminderTitle}
subtitle={readingReminderSubtitle}
onCtaClick={() =>
onEnableHero(NotificationCtaPlacement.InFeedHero)
}
onClose={() =>
onDismissHero(NotificationCtaPlacement.InFeedHero)
}
/>
</div>
)}
{isWidened ? (
<div
className="flex h-full w-full [&>*]:h-full [&>*]:w-full"
style={{ gridColumn: `span ${colSpan}` }}
data-testid="feedItemColSpanWrapper"
>
{itemNode}
</div>
) : (
itemNode
)}
</FeedCardContext.Provider>
);
})}
{!isFetching && !isInitialLoading && !isHorizontal && (
<InfiniteScrollScreenOffset ref={infiniteScrollRef} />
)}
Expand Down
134 changes: 68 additions & 66 deletions packages/shared/src/components/FeedItemComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import { FreeformList } from './cards/Freeform/FreeformList';
import type { PostClick } from '../lib/click';
import { ArticleList } from './cards/article/ArticleList';
import { ArticleGrid } from './cards/article/ArticleGrid';
import { ArticleFeaturedWideGridCard } from './cards/article/ArticleFeaturedWideGridCard';
import type { FeaturedWideColSpan } from './cards/article/ArticleFeaturedWideGridCard';
import { ShareGrid } from './cards/share/ShareGrid';
import { ShareList } from './cards/share/ShareList';
import { CollectionGrid } from './cards/collection';
Expand Down Expand Up @@ -98,6 +100,12 @@ export type FeedItemComponentProps = {
) => unknown;
virtualizedNumCards: number;
disableAdRefresh?: boolean;
/**
* When set, render the post as a wide featured highlight card spanning
* the given number of grid columns. Only used for article-like post
* types with an active `postHighlight`.
*/
wideColSpan?: FeaturedWideColSpan;
} & Pick<UseVotePost, 'toggleUpvote' | 'toggleDownvote'> &
Pick<UseBookmarkPost, 'toggleBookmark'>;

Expand Down Expand Up @@ -280,6 +288,7 @@ function FeedItemComponent({
onCommentClick,
onReadArticleClick,
virtualizedNumCards,
wideColSpan,
}: FeedItemComponentProps): ReactElement | null {
const { logEvent } = useLogContext();
const inViewRef = useLogImpression(
Expand Down Expand Up @@ -395,74 +404,67 @@ function FeedItemComponent({
return null;
}

const postCardProps = {
enableSourceHeader:
feedName !== 'squad' && isSourceSquadOrMachine(itemPost.source),
ref: inViewRef,
post: { ...itemPost },
'data-testid': 'postItem',
onUpvoteClick: (post: Post, origin = Origin.Feed) => {
toggleUpvote({
payload: post,
origin,
opts: { columns, column, row },
});
},
onDownvoteClick: (post: Post, origin = Origin.Feed) => {
toggleDownvote({
payload: post,
origin,
opts: { columns, column, row },
});
},
onPostClick: (post: Post, event?: React.MouseEvent) =>
onPostClick(post, index, row, column, false, event),
onPostAuxClick: (post: Post) =>
onPostClick(post, index, row, column, true),
onReadArticleClick: () =>
onReadArticleClick(itemPost, index, row, column),
onShare: (post: Post) => onShare(post, row, column),
onBookmarkClick: (post: Post, origin = Origin.Feed) => {
toggleBookmark({
post,
origin,
opts: { columns, column, row },
});
},
openNewTab,
enableMenu: !!user,
onMenuClick: (event: React.MouseEvent) =>
onMenuClick(event, index, row, column),
onCopyLinkClick: (event: React.MouseEvent, post: Post) =>
onCopyLinkClick(event, post, index, row, column),
menuOpened: postMenuIndex === index,
onCommentClick: (post: Post) =>
onCommentClick(post, index, row, column, !!boostedBy),
eagerLoadImage: row === 0 && column === 0,
};

const isWidenedFeaturedPost =
item.type === FeedItemType.Post && !!wideColSpan && wideColSpan > 1;

return (
<ActivePostContextProvider post={itemPost}>
<PostTag
enableSourceHeader={
feedName !== 'squad' && isSourceSquadOrMachine(itemPost.source)
}
ref={inViewRef}
post={{ ...itemPost }}
data-testid="postItem"
onUpvoteClick={(post: Post, origin = Origin.Feed) => {
toggleUpvote({
payload: post,
origin,
opts: {
columns,
column,
row,
},
});
}}
onDownvoteClick={(post: Post, origin = Origin.Feed) => {
toggleDownvote({
payload: post,
origin,
opts: {
columns,
column,
row,
},
});
}}
onPostClick={(post: Post, event) =>
onPostClick(post, index, row, column, false, event)
}
onPostAuxClick={(post: Post) =>
onPostClick(post, index, row, column, true)
}
onReadArticleClick={() =>
onReadArticleClick(itemPost, index, row, column)
}
onShare={(post: Post) => onShare(post, row, column)}
onBookmarkClick={(post: Post, origin = Origin.Feed) => {
toggleBookmark({
post,
origin,
opts: {
columns,
column,
row,
},
});
}}
openNewTab={openNewTab}
enableMenu={!!user}
onMenuClick={(event: React.MouseEvent) =>
onMenuClick(event, index, row, column)
}
onCopyLinkClick={(event: React.MouseEvent, post: Post) =>
onCopyLinkClick(event, post, index, row, column)
}
menuOpened={postMenuIndex === index}
onCommentClick={(post: Post) =>
onCommentClick(post, index, row, column, !!boostedBy)
}
eagerLoadImage={row === 0 && column === 0}
>
{item.type === FeedItemType.Ad && <AdPixel pixel={item.ad.pixel} />}
</PostTag>
{isWidenedFeaturedPost ? (
<ArticleFeaturedWideGridCard
{...postCardProps}
wideColSpan={wideColSpan}
/>
) : (
<PostTag {...postCardProps}>
{item.type === FeedItemType.Ad && <AdPixel pixel={item.ad.pixel} />}
</PostTag>
)}
</ActivePostContextProvider>
);
}
Expand Down
Loading
Loading