Skip to content

Commit

Permalink
Remove indirection, insert between slices (#6645)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon authored Nov 22, 2024
1 parent ba04bb5 commit fee2f5d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 111 deletions.
1 change: 0 additions & 1 deletion src/lib/statsig/gates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ export type Gate =
// Keep this alphabetic please.
| 'debug_show_feedcontext' // DISABLED DUE TO EME
| 'post_feed_lang_window' // DISABLED DUE TO EME
| 'suggested_feeds_interstitial'
153 changes: 43 additions & 110 deletions src/view/com/posts/Feed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {useQueryClient} from '@tanstack/react-query'

import {DISCOVER_FEED_URI, KNOWN_SHUTDOWN_FEEDS} from '#/lib/constants'
import {useInitialNumToRender} from '#/lib/hooks/useInitialNumToRender'
import {logEvent, useGate} from '#/lib/statsig/statsig'
import {logEvent} from '#/lib/statsig/statsig'
import {useTheme} from '#/lib/ThemeContext'
import {logger} from '#/logger'
import {isIOS, isWeb} from '#/platform/detection'
Expand All @@ -32,11 +32,7 @@ import {
usePostFeedQuery,
} from '#/state/queries/post-feed'
import {useSession} from '#/state/session'
import {
ProgressGuide,
SuggestedFeeds,
SuggestedFollows,
} from '#/components/FeedInterstitials'
import {ProgressGuide, SuggestedFollows} from '#/components/FeedInterstitials'
import {List, ListRef} from '../util/List'
import {PostFeedLoadingPlaceholder} from '../util/LoadingPlaceholder'
import {LoadMoreRetryBtn} from '../util/LoadMoreRetryBtn'
Expand Down Expand Up @@ -84,74 +80,15 @@ type FeedRow =
key: string
uri: string
}
| {
type: 'interstitialFeeds'
key: string
params: {
variant: 'default' | string
}
slot: number
}
| {
type: 'interstitialFollows'
key: string
params: {
variant: 'default' | string
}
slot: number
}
| {
type: 'interstitialProgressGuide'
key: string
params: {
variant: 'default' | string
}
slot: number
}

const feedInterstitialType = 'interstitialFeeds'
const followInterstitialType = 'interstitialFollows'
const progressGuideInterstitialType = 'interstitialProgressGuide'
const interstials: Record<
'following' | 'discover' | 'profile',
(FeedRow & {
type:
| 'interstitialFeeds'
| 'interstitialFollows'
| 'interstitialProgressGuide'
})[]
> = {
following: [],
discover: [
{
type: progressGuideInterstitialType,
params: {
variant: 'default',
},
key: progressGuideInterstitialType,
slot: 0,
},
{
type: followInterstitialType,
params: {
variant: 'default',
},
key: followInterstitialType,
slot: 20,
},
],
profile: [
{
type: followInterstitialType,
params: {
variant: 'default',
},
key: followInterstitialType,
slot: 5,
},
],
}

export function getFeedPostSlice(feedRow: FeedRow): FeedPostSlice | null {
if (feedRow.type === 'sliceItem') {
return feedRow.slice
Expand Down Expand Up @@ -217,7 +154,6 @@ let Feed = ({
const checkForNewRef = React.useRef<(() => void) | null>(null)
const lastFetchRef = React.useRef<number>(Date.now())
const [feedType, feedUri, feedTab] = feed.split('|')
const gate = useGate()

const opts = React.useMemo(
() => ({enabled, ignoreFilterFor}),
Expand Down Expand Up @@ -317,6 +253,19 @@ let Feed = ({
}, [pollInterval])

const feedItems: FeedRow[] = React.useMemo(() => {
let feedKind: 'following' | 'discover' | 'profile' | undefined
if (feedType === 'following') {
feedKind = 'following'
} else if (feedUri === DISCOVER_FEED_URI) {
feedKind = 'discover'
} else if (
feedType === 'author' &&
(feedTab === 'posts_and_author_threads' ||
feedTab === 'posts_with_replies')
) {
feedKind = 'profile'
}

let arr: FeedRow[] = []
if (KNOWN_SHUTDOWN_FEEDS.includes(feedUri)) {
arr.push({
Expand All @@ -336,8 +285,34 @@ let Feed = ({
key: 'empty',
})
} else if (data) {
let sliceIndex = -1
for (const page of data?.pages) {
for (const slice of page.slices) {
sliceIndex++

if (hasSession) {
if (feedKind === 'discover') {
if (sliceIndex === 0) {
arr.push({
type: 'interstitialProgressGuide',
key: 'interstitial-' + sliceIndex + '-' + lastFetchedAt,
})
} else if (sliceIndex === 20) {
arr.push({
type: 'interstitialFollows',
key: 'interstitial-' + sliceIndex + '-' + lastFetchedAt,
})
}
} else if (feedKind === 'profile') {
if (sliceIndex === 5) {
arr.push({
type: 'interstitialFollows',
key: 'interstitial-' + sliceIndex + '-' + lastFetchedAt,
})
}
}
}

if (slice.isIncompleteThread && slice.items.length >= 3) {
const beforeLast = slice.items.length - 2
const last = slice.items.length - 1
Expand Down Expand Up @@ -396,45 +371,6 @@ let Feed = ({
})
}

if (hasSession) {
let feedKind: 'following' | 'discover' | 'profile' | undefined
if (feedType === 'following') {
feedKind = 'following'
} else if (feedUri === DISCOVER_FEED_URI) {
feedKind = 'discover'
} else if (
feedType === 'author' &&
(feedTab === 'posts_and_author_threads' ||
feedTab === 'posts_with_replies')
) {
feedKind = 'profile'
}

if (feedKind) {
for (const interstitial of interstials[feedKind]) {
const shouldShow =
(interstitial.type === feedInterstitialType &&
gate('suggested_feeds_interstitial')) ||
interstitial.type === followInterstitialType ||
interstitial.type === progressGuideInterstitialType

if (shouldShow) {
const variant = 'default' // replace with experiment variant
const int = {
...interstitial,
params: {variant},
// overwrite key with unique value
key: [interstitial.type, variant, lastFetchedAt].join(':'),
}

if (arr.length > interstitial.slot) {
arr.splice(interstitial.slot, 0, int)
}
}
}
}
}

return arr
}, [
isFetched,
Expand All @@ -445,7 +381,6 @@ let Feed = ({
feedType,
feedUri,
feedTab,
gate,
hasSession,
])

Expand Down Expand Up @@ -529,11 +464,9 @@ let Feed = ({
return <PostFeedLoadingPlaceholder />
} else if (row.type === 'feedShutdownMsg') {
return <FeedShutdownMsg feedUri={feedUri} />
} else if (row.type === feedInterstitialType) {
return <SuggestedFeeds />
} else if (row.type === followInterstitialType) {
} else if (row.type === 'interstitialFollows') {
return <SuggestedFollows feed={feed} />
} else if (row.type === progressGuideInterstitialType) {
} else if (row.type === 'interstitialProgressGuide') {
return <ProgressGuide />
} else if (row.type === 'sliceItem') {
const slice = row.slice
Expand Down

0 comments on commit fee2f5d

Please sign in to comment.