Fix flashes when replacing For You (#3967)

* Fix flashes when replacing For You

* Switch to Discover if pinned after removing
zio/stable
dan 2024-05-11 20:35:08 +01:00 committed by GitHub
parent 51b4b22dec
commit 08462375ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 54 additions and 34 deletions

View File

@ -6,6 +6,7 @@ import {
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query' import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
import {track} from '#/lib/analytics/analytics' import {track} from '#/lib/analytics/analytics'
import {PROD_DEFAULT_FEED} from '#/lib/constants'
import {replaceEqualDeep} from '#/lib/functions' import {replaceEqualDeep} from '#/lib/functions'
import {getAge} from '#/lib/strings/time' import {getAge} from '#/lib/strings/time'
import {STALE} from '#/state/queries' import {STALE} from '#/state/queries'
@ -244,6 +245,45 @@ export function useRemoveFeedMutation() {
}) })
} }
export function useReplaceForYouWithDiscoverFeedMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation({
mutationFn: async ({
forYouFeedConfig,
discoverFeedConfig,
}: {
forYouFeedConfig: AppBskyActorDefs.SavedFeed | undefined
discoverFeedConfig: AppBskyActorDefs.SavedFeed | undefined
}) => {
if (forYouFeedConfig) {
await getAgent().removeSavedFeeds([forYouFeedConfig.id])
}
if (!discoverFeedConfig) {
await getAgent().addSavedFeeds([
{
type: 'feed',
value: PROD_DEFAULT_FEED('whats-hot'),
pinned: true,
},
])
} else {
await getAgent().updateSavedFeeds([
{
...discoverFeedConfig,
pinned: true,
},
])
}
// triggers a refetch
await queryClient.invalidateQueries({
queryKey: preferencesQueryKey,
})
},
})
}
export function useUpdateSavedFeedsMutation() { export function useUpdateSavedFeedsMutation() {
const queryClient = useQueryClient() const queryClient = useQueryClient()
const {getAgent} = useAgent() const {getAgent} = useAgent()

View File

@ -6,10 +6,9 @@ import {useLingui} from '@lingui/react'
import {PROD_DEFAULT_FEED} from '#/lib/constants' import {PROD_DEFAULT_FEED} from '#/lib/constants'
import {logger} from '#/logger' import {logger} from '#/logger'
import { import {
useAddSavedFeedsMutation,
usePreferencesQuery, usePreferencesQuery,
useRemoveFeedMutation, useRemoveFeedMutation,
useUpdateSavedFeedsMutation, useReplaceForYouWithDiscoverFeedMutation,
} from '#/state/queries/preferences' } from '#/state/queries/preferences'
import {useSetSelectedFeed} from '#/state/shell/selected-feed' import {useSetSelectedFeed} from '#/state/shell/selected-feed'
import * as Toast from '#/view/com/util/Toast' import * as Toast from '#/view/com/util/Toast'
@ -24,12 +23,10 @@ export function FeedShutdownMsg({feedUri}: {feedUri: string}) {
const {_} = useLingui() const {_} = useLingui()
const setSelectedFeed = useSetSelectedFeed() const setSelectedFeed = useSetSelectedFeed()
const {data: preferences} = usePreferencesQuery() const {data: preferences} = usePreferencesQuery()
const {mutateAsync: addSavedFeeds, isPending: isAddSavedFeedPending} =
useAddSavedFeedsMutation()
const {mutateAsync: removeFeed, isPending: isRemovePending} = const {mutateAsync: removeFeed, isPending: isRemovePending} =
useRemoveFeedMutation() useRemoveFeedMutation()
const {mutateAsync: updateSavedFeeds, isPending: isUpdateFeedPending} = const {mutateAsync: replaceFeedWithDiscover, isPending: isReplacePending} =
useUpdateSavedFeedsMutation() useReplaceForYouWithDiscoverFeedMutation()
const feedConfig = preferences?.savedFeeds?.find( const feedConfig = preferences?.savedFeeds?.find(
f => f.value === feedUri && f.pinned, f => f.value === feedUri && f.pinned,
@ -46,6 +43,9 @@ export function FeedShutdownMsg({feedUri}: {feedUri: string}) {
await removeFeed(feedConfig) await removeFeed(feedConfig)
Toast.show(_(msg`Removed from your feeds`)) Toast.show(_(msg`Removed from your feeds`))
} }
if (hasDiscoverPinned) {
setSelectedFeed(`feedgen|${PROD_DEFAULT_FEED('whats-hot')}`)
}
} catch (err: any) { } catch (err: any) {
Toast.show( Toast.show(
_( _(
@ -54,30 +54,15 @@ export function FeedShutdownMsg({feedUri}: {feedUri: string}) {
) )
logger.error('Failed up update feeds', {message: err}) logger.error('Failed up update feeds', {message: err})
} }
}, [removeFeed, feedConfig, _]) }, [removeFeed, feedConfig, _, hasDiscoverPinned, setSelectedFeed])
const onReplaceFeed = React.useCallback(async () => { const onReplaceFeed = React.useCallback(async () => {
try { try {
if (!discoverFeedConfig) { await replaceFeedWithDiscover({
await addSavedFeeds([ forYouFeedConfig: feedConfig,
{ discoverFeedConfig,
type: 'feed', })
value: PROD_DEFAULT_FEED('whats-hot'),
pinned: true,
},
])
} else {
await updateSavedFeeds([
{
...discoverFeedConfig,
pinned: true,
},
])
}
setSelectedFeed(`feedgen|${PROD_DEFAULT_FEED('whats-hot')}`) setSelectedFeed(`feedgen|${PROD_DEFAULT_FEED('whats-hot')}`)
if (feedConfig) {
await removeFeed(feedConfig)
}
Toast.show(_(msg`The feed has been replaced with Discover.`)) Toast.show(_(msg`The feed has been replaced with Discover.`))
} catch (err: any) { } catch (err: any) {
Toast.show( Toast.show(
@ -88,17 +73,14 @@ export function FeedShutdownMsg({feedUri}: {feedUri: string}) {
logger.error('Failed up update feeds', {message: err}) logger.error('Failed up update feeds', {message: err})
} }
}, [ }, [
addSavedFeeds, replaceFeedWithDiscover,
updateSavedFeeds,
removeFeed,
discoverFeedConfig, discoverFeedConfig,
feedConfig, feedConfig,
setSelectedFeed, setSelectedFeed,
_, _,
]) ])
const isProcessing = const isProcessing = isReplacePending || isRemovePending
isAddSavedFeedPending || isUpdateFeedPending || isRemovePending
return ( return (
<View <View
style={[ style={[
@ -147,9 +129,7 @@ export function FeedShutdownMsg({feedUri}: {feedUri: string}) {
<ButtonText> <ButtonText>
<Trans>Replace with Discover</Trans> <Trans>Replace with Discover</Trans>
</ButtonText> </ButtonText>
{(isAddSavedFeedPending || isUpdateFeedPending) && ( {isReplacePending && <ButtonIcon icon={Loader} />}
<ButtonIcon icon={Loader} />
)}
</Button> </Button>
)} )}
</View> </View>