11import type { ReactElement } from 'react' ;
22import React , { useCallback , useEffect , useMemo } from 'react' ;
3- import { useQuery } from '@tanstack/react-query' ;
4- import type { Post } from '../../../graphql/posts' ;
5- import { gqlClient } from '../../../graphql/common' ;
6- import type { FeedHeroData } from '../../../graphql/feed' ;
7- import { FEED_HERO_QUERY , baseFeedSupportedTypes } from '../../../graphql/feed' ;
83import type { PostHighlight } from '../../../graphql/highlights' ;
94import type { ViewabilityData } from '../../../features/monetization/viewability' ;
105import { viewabilityLogExtra } from '../../../features/monetization/viewability' ;
11- import { useAuthContext } from '../../../contexts/AuthContext' ;
126import { useLogContext } from '../../../contexts/LogContext' ;
13- import { useCopyLink } from '../../../hooks/useCopy' ;
147import { ImpressionStatus } from '../../../hooks/feed/useLogImpression' ;
158import {
169 adLogEvent ,
@@ -19,11 +12,10 @@ import {
1912} from '../../../lib/feed' ;
2013import { AdActions } from '../../../lib/ads' ;
2114import { LogEvent , Origin } from '../../../lib/log' ;
22- import { generateQueryKey , RequestKey , StaleTime } from '../../../lib/query' ;
2315import { FeedHeroSection } from './FeedHeroSection' ;
2416import { useFeedHeroAd } from './useFeedHeroAd' ;
25- import { useFeedHeroPostActions } from './useFeedHeroPostActions ' ;
26- import { useCachedTokenRecovery } from '../../../hooks/useCachedTokenRecovery ' ;
17+ import { useFeedHeroData } from './useFeedHeroData ' ;
18+ import { useFeedHeroCardProps } from './useFeedHeroCardProps ' ;
2719
2820/**
2921 * The carousel and the Happening Now list are two lists, not one: `feedHero`
@@ -48,42 +40,13 @@ export const FeedHero = ({
4840 */
4941 onRenderedChange ?: ( isRendered : boolean ) => void ;
5042} ) : ReactElement | null => {
51- const { user, isTokenValid } = useAuthContext ( ) ;
5243 const { logEvent } = useLogContext ( ) ;
5344 const postLogEvent = usePostLogEvent ( ) ;
54- const [ , copyLink ] = useCopyLink ( ) ;
5545
5646 const { ad, placement, shape } = useFeedHeroAd ( ) ;
5747
58- const queryKey = useMemo (
59- ( ) => generateQueryKey ( RequestKey . FeedHero , user ) ,
60- [ user ] ,
61- ) ;
62- // Vote and bookmark patch the hero's own query: the bare hooks only write the
63- // single-post key, which these cards never read.
64- const { toggleUpvote, toggleDownvote, toggleBookmark } =
65- useFeedHeroPostActions ( {
66- queryKey,
67- feedName,
68- origin : Origin . FeedHero ,
69- } ) ;
70-
71- const { data : hero } = useQuery ( {
72- queryKey,
73- queryFn : ( ) =>
74- gqlClient . request < FeedHeroData > ( FEED_HERO_QUERY , {
75- loggedIn : ! ! user ,
76- supportedTypes : baseFeedSupportedTypes ,
77- } ) ,
78- enabled : isTokenValid ,
79- // The same window `majorHeadlinesQueryOptions` gives the in-feed card, so
80- // the two surfaces the experiment compares are equally fresh.
81- staleTime : StaleTime . OneMinute ,
82- } ) ;
83- useCachedTokenRecovery ( { queryKey, enabled : isTokenValid } ) ;
84-
85- const highlights = useMemo ( ( ) => hero ?. feedHero ?. highlights ?? [ ] , [ hero ] ) ;
86- const posts : Post [ ] = useMemo ( ( ) => hero ?. feedHero ?. posts ?? [ ] , [ hero ] ) ;
48+ const { queryKey, posts, highlights } = useFeedHeroData ( ) ;
49+ const cardProps = useFeedHeroCardProps ( { queryKey, feedName } ) ;
8750
8851 const isRendered = posts . length > 0 ;
8952 const adPlacement = isRendered ? placement : 'none' ;
@@ -153,33 +116,6 @@ export const FeedHero = ({
153116 ad . impressionStatus = ImpressionStatus . LOGGED ;
154117 } , [ ad , isAdShown , onAdAction ] ) ;
155118
156- const cardProps = useMemo (
157- ( ) => ( {
158- onPostClick : ( post : Post ) =>
159- logEvent (
160- postLogEvent ( LogEvent . Click , post , {
161- extra : { origin : Origin . FeedHero } ,
162- } ) ,
163- ) ,
164- onUpvoteClick : ( post : Post , origin = Origin . FeedHero ) =>
165- toggleUpvote ( { payload : post , origin } ) ,
166- onDownvoteClick : ( post : Post , origin = Origin . FeedHero ) =>
167- toggleDownvote ( { payload : post , origin } ) ,
168- onBookmarkClick : ( post : Post , origin = Origin . FeedHero ) =>
169- toggleBookmark ( { post, origin } ) ,
170- onCopyLinkClick : ( _ : React . MouseEvent , post : Post ) =>
171- copyLink ( { link : post . commentsPermalink } ) ,
172- } ) ,
173- [
174- copyLink ,
175- logEvent ,
176- postLogEvent ,
177- toggleBookmark ,
178- toggleDownvote ,
179- toggleUpvote ,
180- ] ,
181- ) ;
182-
183119 if ( ! isRendered ) {
184120 return null ;
185121 }
0 commit comments