-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
297 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
import { EnhancedCastObject } from '@app/api/channelFeed.api'; | ||
import { getEnhancedForYouFeed } from '@app/api/forYouFeed.api'; | ||
import { Cast } from '@app/components/apps/cast/Cast'; | ||
import { AdvertFeed } from '@app/components/apps/channelFeed/AdvertFeed/AdvertFeed'; | ||
import { BaseEmpty } from '@app/components/common/BaseEmpty/BaseEmpty'; | ||
import { BaseFeed } from '@app/components/common/BaseFeed/BaseFeed'; | ||
import { useAppSelector } from '@app/hooks/reduxHooks'; | ||
import { allChannelsQuery, allPowerBadgeUsersQuery, followingByFidQuery } from '@app/queries/queries'; | ||
import { useZustand } from '@app/store/zustand'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { useEffect, useMemo, useState } from 'react'; | ||
|
||
interface ForYouFeedProps { | ||
fid: number; | ||
} | ||
export const ForYouFeed: React.FC<ForYouFeedProps> = ({ fid }) => { | ||
const [allPowerBadgeUsers, setAllPowerBadgeUsers] = useState<number[]>([]); | ||
const [casts, setCasts] = useState<EnhancedCastObject[]>([]); | ||
const [nextCursor, setNextCursor] = useState<string | undefined>(); | ||
const [hasMore, setHasMore] = useState<boolean>(true); | ||
const [loaded, setLoaded] = useState<boolean>(false); | ||
const { | ||
setNumCasts, | ||
setNumCuratedChannelsCasts, | ||
setNumFarcaptchas, | ||
setNumUpvotes, | ||
setNumDownvotes, | ||
setNumCastsWithUpvotes, | ||
setNumCastsWithDownvotes, | ||
setNumCastsAboveThreshold, | ||
setNumCastsAfterFiltering, | ||
selectedLabels, | ||
} = useZustand(); | ||
|
||
const signalToNoiseState = useAppSelector((state) => state.signalToNoise); | ||
const showOnlyCuratedChannels = signalToNoiseState.showOnlyCuratedChannels; | ||
const showOnlyFarcaptcha = signalToNoiseState.showOnlyFarcaptcha; | ||
const onlyShowUpvoted = signalToNoiseState.onlyShowUpvoted; | ||
const hideDownvoted = signalToNoiseState.hideDownvoted; | ||
const onlyShowRatioAboveThreshold = signalToNoiseState.onlyShowRatioAboveThreshold; | ||
const ratioThreshold = signalToNoiseState.ratioThreshold; | ||
|
||
const chQuery = useQuery(allChannelsQuery()); | ||
const memodChannelData = useMemo(() => { | ||
if (chQuery.isLoading || chQuery.error) return null; | ||
return chQuery?.data?.result?.channels ?? []; | ||
}, [chQuery.isLoading, chQuery.error, chQuery.data]); | ||
|
||
const ffQuery = useQuery(followingByFidQuery(fid)); | ||
const memodFfData = useMemo(() => { | ||
if (ffQuery.isLoading || ffQuery.error) return null; | ||
return (ffQuery.data?.result?.users ?? []).map((u) => Number(u.fid)); | ||
}, [ffQuery.isLoading, ffQuery.error, ffQuery.data]); | ||
|
||
const pbQuery = useQuery(allPowerBadgeUsersQuery()); | ||
const memodPbData = useMemo(() => { | ||
if (pbQuery.isLoading || pbQuery.error) return null; | ||
return pbQuery.data; | ||
}, [pbQuery.isLoading, pbQuery.error, pbQuery.data]); | ||
|
||
useEffect(() => { | ||
const allPowerBadgeUsers = memodPbData?.result.fids ?? []; | ||
setAllPowerBadgeUsers(allPowerBadgeUsers); | ||
}, [memodPbData]); | ||
|
||
useEffect(() => { | ||
setCasts([]); | ||
|
||
getEnhancedForYouFeed({ | ||
fid: fid, | ||
powerBadgeUsers: allPowerBadgeUsers, | ||
allChannels: memodChannelData ?? [], | ||
}) | ||
.then((res) => { | ||
setCasts(res.casts); | ||
setNextCursor(res.next?.cursor); | ||
setHasMore(!!res.next?.cursor); | ||
}) | ||
.finally(() => { | ||
setLoaded(true); | ||
}); | ||
}, [fid, allPowerBadgeUsers, memodChannelData, memodFfData]); | ||
|
||
useEffect(() => { | ||
setNumCasts(casts.length); | ||
setNumCuratedChannelsCasts(casts.filter((c) => c.tags.length > 1).length); | ||
setNumFarcaptchas(casts.filter((c) => c.botOrNotResult.farcaptcha).length); | ||
setNumUpvotes(casts.reduce((acc, c) => acc + c.curation.upvotes.length, 0)); | ||
setNumDownvotes(casts.reduce((acc, c) => acc + c.curation.downvotes.length, 0)); | ||
setNumCastsWithUpvotes(casts.filter((c) => c.curation.upvotes.length > 0).length); | ||
setNumCastsWithDownvotes(casts.filter((c) => c.curation.downvotes.length > 0).length); | ||
setNumCastsAboveThreshold( | ||
casts.filter((c) => c.curation.upvotes.length / (c.curation.downvotes.length + 0.00001) > ratioThreshold).length, | ||
); | ||
}, [ | ||
casts, | ||
ratioThreshold, | ||
setNumCasts, | ||
setNumCuratedChannelsCasts, | ||
setNumCastsAboveThreshold, | ||
setNumCastsWithDownvotes, | ||
setNumCastsWithUpvotes, | ||
setNumDownvotes, | ||
setNumFarcaptchas, | ||
setNumUpvotes, | ||
]); | ||
|
||
const next = () => | ||
getEnhancedForYouFeed({ | ||
fid: fid, | ||
cursor: nextCursor, | ||
powerBadgeUsers: allPowerBadgeUsers, | ||
allChannels: memodChannelData ?? [], | ||
}).then((newCasts) => { | ||
setNextCursor(newCasts.next?.cursor); | ||
setCasts(casts.concat(newCasts.casts)); | ||
}); | ||
|
||
const filteredCastsList = useMemo(() => { | ||
const filteredCasts = casts | ||
.filter((c) => !showOnlyCuratedChannels || c.tags.length > 1) | ||
.filter((c) => !showOnlyFarcaptcha || c.botOrNotResult.farcaptcha) | ||
.filter((c) => !onlyShowUpvoted || c.curation.upvotes.length > 0) | ||
.filter((c) => (hideDownvoted ? c.curation.downvotes.length < 1 : true)) | ||
.filter((c) => | ||
onlyShowRatioAboveThreshold | ||
? c.curation.upvotes.length / (c.curation.downvotes.length + 0.00001) > ratioThreshold | ||
: true, | ||
) | ||
.filter((c) => | ||
selectedLabels.length === 0 ? true : selectedLabels.includes(c.botOrNotResult.label ?? 'missing-label'), | ||
) | ||
.map((post, index) => ( | ||
<Cast | ||
level={0} | ||
key={`${post.hash}-top-${index}`} | ||
castHash={post.hash} | ||
title={' '} | ||
description={post.text} | ||
date={post.timestamp} | ||
embeds={post.embeds ?? []} | ||
displayName={post.author.display_name} | ||
fid={post.author.fid} | ||
fname={post.author.username ?? post.author.display_name ?? post.author.fid.toString() ?? '<unknown>'} | ||
avatar={post.author.pfp_url} | ||
parentHash={post.parent_hash} | ||
threadHash={post.thread_hash} | ||
parentUrl={post.parent_url} | ||
replies={post.replies.count} | ||
recasts={post.reactions.recasts_count} | ||
recastooors={post.reactions.recasts.map((r) => r.fid)} | ||
likes={post.reactions.likes_count} | ||
likooors={post.reactions.likes.map((l) => l.fid)} | ||
tags={post.tags} | ||
curation={post.curation} | ||
hasPowerBadge={post.authorHasPowerBadge} | ||
botOrNotResult={post.botOrNotResult} | ||
/> | ||
)); | ||
setNumCastsAfterFiltering(filteredCasts.length); | ||
return filteredCasts; | ||
}, [ | ||
casts, | ||
setNumCastsAfterFiltering, | ||
showOnlyCuratedChannels, | ||
showOnlyFarcaptcha, | ||
onlyShowUpvoted, | ||
hideDownvoted, | ||
onlyShowRatioAboveThreshold, | ||
ratioThreshold, | ||
selectedLabels, | ||
]); | ||
|
||
return ( | ||
<AdvertFeed casts={casts}> | ||
{({ castList }) => | ||
castList?.length || !loaded ? ( | ||
<BaseFeed next={next} hasMore={hasMore}> | ||
{filteredCastsList} | ||
</BaseFeed> | ||
) : ( | ||
<BaseEmpty /> | ||
) | ||
} | ||
</AdvertFeed> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.