Skip to content

Commit

Permalink
feat(ONYX-1486): Section title prefetching and refactoring (#11405)
Browse files Browse the repository at this point in the history
* feat: Section title prefetching and refactoring

* code

* more

* fixes

* fix more

* simplify passProps

* section path ID

* RightButtonContent fix

* tests
  • Loading branch information
olerichter00 authored Jan 20, 2025
1 parent 93ca920 commit c6a6224
Show file tree
Hide file tree
Showing 37 changed files with 364 additions and 480 deletions.
9 changes: 3 additions & 6 deletions src/app/Components/EmbeddedCarousel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Spacer, Flex, Touchable } from "@artsy/palette-mobile"
import { Flex, Spacer, Touchable } from "@artsy/palette-mobile"
import { SectionTitle } from "app/Components/SectionTitle"
import React from "react"
import { FlatList, FlatListProps } from "react-native"
Expand All @@ -20,11 +20,8 @@ export const EmbeddedCarousel: React.FC<EmbeddedCarouselProps & FlatListProps<an

return (
<Flex>
{!!title && (
<Flex px={2}>
<SectionTitle title={title} />
</Flex>
)}
{!!title && <SectionTitle title={title} mx={2} />}

<FlatList
testID={testID}
horizontal
Expand Down
92 changes: 54 additions & 38 deletions src/app/Components/SectionTitle.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,37 @@
import { ArrowRightIcon, Flex, SpacingUnit, Text, TextProps, useTheme } from "@artsy/palette-mobile"
import {
ArrowRightIcon,
Flex,
FlexProps,
SpacingUnit,
Text,
TextProps,
useTheme,
} from "@artsy/palette-mobile"
import { toTitleCase } from "@artsy/to-title-case"
import { RouterLink } from "app/system/navigation/RouterLink"

const Wrapper: React.FC<{ onPress?(): void; href?: string | null }> = ({
children,
href,
onPress,
}) => {
if (onPress) {
return (
<RouterLink
onPress={onPress}
to={href}
testID="touchable-wrapper"
hitSlop={{ top: 10, bottom: 10 }}
activeOpacity={0.65}
>
{children}
</RouterLink>
)
} else {
return <>{children}</>
}
}

export const SectionTitle: React.FC<{
href?: string | null
title: React.ReactNode
titleVariant?: TextProps["variant"]
subtitle?: React.ReactNode
onPress?: () => any
RightButtonContent?: React.FC
mb?: SpacingUnit
capitalized?: boolean
}> = ({
export const SectionTitle: React.FC<
{
href?: string | null
title: React.ReactNode
titleVariant?: TextProps["variant"]
subtitle?: React.ReactNode
navigationProps?: object
onPress?: () => any
RightButtonContent?: React.FC
mb?: SpacingUnit
capitalized?: boolean
} & FlexProps
> = ({
href,
navigationProps,
title,
titleVariant = "sm-display",
subtitle,
onPress,
RightButtonContent = RightButton,
mb = 2,
capitalized = true,
...flexProps
}) => {
const { color } = useTheme()
let titleText
Expand All @@ -51,19 +41,21 @@ export const SectionTitle: React.FC<{
}

return (
<Wrapper onPress={onPress} href={href}>
<Flex mb={mb} flexDirection="row" alignItems="flex-start">
<Wrapper onPress={onPress} href={href} navigationProps={navigationProps}>
<Flex mb={2} flexDirection="row" alignItems="flex-start" {...flexProps}>
<Flex flex={1}>
<Text variant={titleVariant} ellipsizeMode="tail" numberOfLines={1} testID="title">
{typeof title === "string" ? titleText : title}
</Text>
{Boolean(subtitle) && (

{!!subtitle && (
<Text variant="sm" color={color("black60")} lineHeight="20px" testID="subtitle">
{subtitle}
</Text>
)}
</Flex>
{!!onPress && (

{(!!href || !!onPress) && (
<Flex flexShrink={0} pl={1}>
<RightButtonContent />
</Flex>
Expand All @@ -73,6 +65,30 @@ export const SectionTitle: React.FC<{
)
}

const Wrapper: React.FC<{ onPress?(): void; href?: string | null; navigationProps?: object }> = ({
children,
href,
navigationProps,
onPress,
}) => {
if (onPress) {
return (
<RouterLink
onPress={onPress}
navigationProps={navigationProps}
to={href}
testID="touchable-wrapper"
hitSlop={{ top: 10, bottom: 10 }}
activeOpacity={0.65}
>
{children}
</RouterLink>
)
} else {
return <>{children}</>
}
}

const RightButton = () => (
<Flex flexDirection="row" flex={1}>
<Flex my="auto">
Expand Down
8 changes: 6 additions & 2 deletions src/app/Navigation/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ import { FeatureQueryRenderer } from "app/Scenes/Feature/Feature"
import { GalleriesForYouScreen } from "app/Scenes/GalleriesForYou/GalleriesForYouScreen"
import { GeneQueryRenderer } from "app/Scenes/Gene/Gene"
import { HomeViewScreen, homeViewScreenQuery } from "app/Scenes/HomeView/HomeView"
import { HomeViewSectionScreenQueryRenderer } from "app/Scenes/HomeViewSectionScreen/HomeViewSectionScreen"
import {
HOME_SECTION_SCREEN_QUERY,
HomeViewSectionScreenQueryRenderer,
} from "app/Scenes/HomeViewSectionScreen/HomeViewSectionScreen"
import { MakeOfferModalQueryRenderer } from "app/Scenes/Inbox/Components/Conversations/MakeOfferModal"
import { PurchaseModalQueryRenderer } from "app/Scenes/Inbox/Components/Conversations/PurchaseModal"
import { ConversationQueryRenderer } from "app/Scenes/Inbox/Screens/Conversation"
Expand Down Expand Up @@ -785,14 +788,15 @@ export const artsyDotNetRoutes = defineRoutes([
},
},
{
path: "/home-view/sections/:sectionID",
path: "/home-view/sections/:id",
name: "HomeViewSectionScreen",
Component: HomeViewSectionScreenQueryRenderer,
options: {
screenOptions: {
headerShown: false,
},
},
queries: [HOME_SECTION_SCREEN_QUERY],
},
{
path: "/inbox",
Expand Down
6 changes: 1 addition & 5 deletions src/app/Scenes/HomeView/Components/ActivityRail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ export const ActivityRail: React.FC<ActivityRailProps> = ({ title, viewer }) =>

const handleHeaderPress = () => {
trackEvent(HomeAnalytics.activityHeaderTapEvent())

navigate("/notifications")
}

const handleMorePress = () => {
Expand All @@ -43,9 +41,7 @@ export const ActivityRail: React.FC<ActivityRailProps> = ({ title, viewer }) =>

return (
<Flex pt={2}>
<Flex px={2}>
<SectionTitle title={title} onPress={handleHeaderPress} />
</Flex>
<SectionTitle title={title} onPress={handleHeaderPress} href="/notifications" mx={2} />

<FlatList
horizontal
Expand Down
126 changes: 51 additions & 75 deletions src/app/Scenes/HomeView/Components/ArticlesRail.tsx
Original file line number Diff line number Diff line change
@@ -1,100 +1,76 @@
import { Flex, Spacer } from "@artsy/palette-mobile"
import { ArticlesRail_articlesConnection$data } from "__generated__/ArticlesRail_articlesConnection.graphql"
import {
ArticlesRail_articlesConnection$data,
ArticlesRail_articlesConnection$key,
} from "__generated__/ArticlesRail_articlesConnection.graphql"
import { ArticleCardContainer } from "app/Components/ArticleCard"
import { SectionTitle } from "app/Components/SectionTitle"
import {
HORIZONTAL_FLATLIST_INTIAL_NUMBER_TO_RENDER_DEFAULT,
HORIZONTAL_FLATLIST_WINDOW_SIZE,
} from "app/Scenes/HomeView/helpers/constants"
import HomeAnalytics from "app/Scenes/HomeView/helpers/homeAnalytics"
import { navigate } from "app/system/navigation/navigate"
import { extractNodes } from "app/utils/extractNodes"
import { ExtractNodeType } from "app/utils/relayHelpers"
import { memo } from "react"
import { FlatList } from "react-native"
import { createFragmentContainer, graphql } from "react-relay"
import { useTracking } from "react-tracking"
import { graphql, useFragment } from "react-relay"

interface ArticlesRailProps {
articlesConnection: ArticlesRail_articlesConnection$data
onTrack?: (article: ExtractNodeType<ArticlesRail_articlesConnection$data>, index: number) => void
onSectionTitlePress?: () => void
articlesConnection: ArticlesRail_articlesConnection$key
onTitlePress?: () => void
onPress?: (article: ExtractNodeType<ArticlesRail_articlesConnection$data>, index: number) => void
title: string
titleHref?: string | null
}

export const ArticlesRail: React.FC<ArticlesRailProps> = ({
articlesConnection,
onTrack,
onSectionTitlePress,
title,
}) => {
const articles = extractNodes(articlesConnection)
const tracking = useTracking()
export const ArticlesRail: React.FC<ArticlesRailProps> = memo(
({ onTitlePress, onPress, title, titleHref, ...restProps }) => {
const articlesConnection = useFragment(articlesConnectionFragment, restProps.articlesConnection)

if (!articles.length) {
return null
}
const articles = extractNodes(articlesConnection)

const onTitlePress = () => {
if (onSectionTitlePress) {
onSectionTitlePress()
} else {
tracking.trackEvent(HomeAnalytics.articlesHeaderTapEvent())
navigate("/articles")
if (!articles.length) {
return null
}
}

return (
<Flex>
<Flex mx={2}>
<SectionTitle title={title} onPress={onTitlePress} />
</Flex>
return (
<Flex>
<FlatList
horizontal
showsHorizontalScrollIndicator={false}
ListHeaderComponent={() => <Spacer x={2} />}
ListFooterComponent={() => <Spacer x={2} />}
ItemSeparatorComponent={() => <Spacer x={2} />}
initialNumToRender={HORIZONTAL_FLATLIST_INTIAL_NUMBER_TO_RENDER_DEFAULT}
windowSize={HORIZONTAL_FLATLIST_WINDOW_SIZE}
data={articles}
keyExtractor={(item) => `${item.internalID}`}
renderItem={({ item, index }) => (
<ArticleCardContainer
onPress={() => {
if (onTrack) {
return onTrack(item, index)
}
<SectionTitle href={titleHref} mx={2} onPress={onTitlePress} title={title} />

const tapEvent = HomeAnalytics.articleThumbnailTapEvent(
item.internalID,
item.slug || "",
index
)
tracking.trackEvent(tapEvent)
}}
article={item}
/>
)}
/>
<Flex>
<FlatList
horizontal
showsHorizontalScrollIndicator={false}
ListHeaderComponent={() => <Spacer x={2} />}
ListFooterComponent={() => <Spacer x={2} />}
ItemSeparatorComponent={() => <Spacer x={2} />}
initialNumToRender={HORIZONTAL_FLATLIST_INTIAL_NUMBER_TO_RENDER_DEFAULT}
windowSize={HORIZONTAL_FLATLIST_WINDOW_SIZE}
data={articles}
keyExtractor={(item) => `${item.internalID}`}
renderItem={({ item, index }) => (
<ArticleCardContainer
onPress={() => {
onPress?.(item, index)
}}
article={item}
/>
)}
/>
</Flex>
</Flex>
</Flex>
)
}
)
}
)

export const ArticlesRailFragmentContainer = memo(
createFragmentContainer(ArticlesRail, {
articlesConnection: graphql`
fragment ArticlesRail_articlesConnection on ArticleConnection {
edges {
node {
internalID
slug
...ArticleCard_article
}
}
const articlesConnectionFragment = graphql`
fragment ArticlesRail_articlesConnection on ArticleConnection {
edges {
node {
internalID
slug
...ArticleCard_article
}
`,
})
)
}
}
`
5 changes: 2 additions & 3 deletions src/app/Scenes/HomeView/Components/ArtistRails/ArtistRail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ const ArtistRail: React.FC<Props & RailScrollProps> = (props) => {

return (
<Flex>
<Flex pl={2} pr={2}>
<SectionTitle title={props.title} subtitle={props.subtitle} />
</Flex>
<SectionTitle title={props.title} subtitle={props.subtitle} mx={2} />

<CardRailFlatList<SuggestedArtist>
listRef={listRef}
data={artists}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ export const RecommendedArtistsRail: React.FC<RecommendedArtistsRailProps & Rail

return (
<Flex>
<Flex pl={2} pr={2}>
<SectionTitle title={title} subtitle={subtitle} />
</Flex>
<SectionTitle title={title} subtitle={subtitle} mx={2} />

<CardRailFlatList<ArtistCard_artist$data>
listRef={listRef}
data={artists as any}
Expand Down
Loading

0 comments on commit c6a6224

Please sign in to comment.