Prefetch profile links on web (#2634)

zio/stable
dan 2024-01-26 05:23:56 +00:00 committed by GitHub
parent 157404132f
commit 23a4bbf608
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 1 deletions

View File

@ -57,6 +57,23 @@ export function useProfilesQuery({handles}: {handles: string[]}) {
}) })
} }
export function usePrefetchProfileQuery() {
const queryClient = useQueryClient()
const prefetchProfileQuery = useCallback(
(did: string) => {
queryClient.prefetchQuery({
queryKey: RQKEY(did),
queryFn: async () => {
const res = await getAgent().getProfile({actor: did || ''})
return res.data
},
})
},
[queryClient],
)
return prefetchProfileQuery
}
interface ProfileUpdateParams { interface ProfileUpdateParams {
profile: AppBskyActorDefs.ProfileView profile: AppBskyActorDefs.ProfileView
updates: updates:

View File

@ -47,6 +47,7 @@ interface Props extends ComponentProps<typeof TouchableOpacity> {
asAnchor?: boolean asAnchor?: boolean
anchorNoUnderline?: boolean anchorNoUnderline?: boolean
navigationAction?: 'push' | 'replace' | 'navigate' navigationAction?: 'push' | 'replace' | 'navigate'
onPointerEnter?: () => void
} }
export const Link = memo(function Link({ export const Link = memo(function Link({
@ -264,6 +265,7 @@ interface TextLinkOnWebOnlyProps extends TextProps {
accessibilityHint?: string accessibilityHint?: string
title?: string title?: string
navigationAction?: 'push' | 'replace' | 'navigate' navigationAction?: 'push' | 'replace' | 'navigate'
onPointerEnter?: () => void
} }
export const TextLinkOnWebOnly = memo(function DesktopWebTextLink({ export const TextLinkOnWebOnly = memo(function DesktopWebTextLink({
testID, testID,

View File

@ -8,10 +8,11 @@ import {TypographyVariant} from 'lib/ThemeContext'
import {UserAvatar} from './UserAvatar' import {UserAvatar} from './UserAvatar'
import {sanitizeDisplayName} from 'lib/strings/display-names' import {sanitizeDisplayName} from 'lib/strings/display-names'
import {sanitizeHandle} from 'lib/strings/handles' import {sanitizeHandle} from 'lib/strings/handles'
import {isAndroid} from 'platform/detection' import {isAndroid, isWeb} from 'platform/detection'
import {TimeElapsed} from './TimeElapsed' import {TimeElapsed} from './TimeElapsed'
import {makeProfileLink} from 'lib/routes/links' import {makeProfileLink} from 'lib/routes/links'
import {ModerationUI} from '@atproto/api' import {ModerationUI} from '@atproto/api'
import {usePrefetchProfileQuery} from '#/state/queries/profile'
interface PostMetaOpts { interface PostMetaOpts {
author: { author: {
@ -35,6 +36,7 @@ let PostMeta = (opts: PostMetaOpts): React.ReactNode => {
const pal = usePalette('default') const pal = usePalette('default')
const displayName = opts.author.displayName || opts.author.handle const displayName = opts.author.displayName || opts.author.handle
const handle = opts.author.handle const handle = opts.author.handle
const prefetchProfileQuery = usePrefetchProfileQuery()
return ( return (
<View style={[styles.container, opts.style]}> <View style={[styles.container, opts.style]}>
@ -66,6 +68,11 @@ let PostMeta = (opts: PostMetaOpts): React.ReactNode => {
</> </>
} }
href={makeProfileLink(opts.author)} href={makeProfileLink(opts.author)}
onPointerEnter={() => {
if (isWeb) {
prefetchProfileQuery(opts.author.did)
}
}}
/> />
</View> </View>
{!isAndroid && ( {!isAndroid && (

View File

@ -4,6 +4,7 @@ import {Link} from './Link'
import {isAndroid, isWeb} from 'platform/detection' import {isAndroid, isWeb} from 'platform/detection'
import {makeProfileLink} from 'lib/routes/links' import {makeProfileLink} from 'lib/routes/links'
import {useModalControls} from '#/state/modals' import {useModalControls} from '#/state/modals'
import {usePrefetchProfileQuery} from '#/state/queries/profile'
interface UserPreviewLinkProps { interface UserPreviewLinkProps {
did: string did: string
@ -14,10 +15,16 @@ export function UserPreviewLink(
props: React.PropsWithChildren<UserPreviewLinkProps>, props: React.PropsWithChildren<UserPreviewLinkProps>,
) { ) {
const {openModal} = useModalControls() const {openModal} = useModalControls()
const prefetchProfileQuery = usePrefetchProfileQuery()
if (isWeb || isAndroid) { if (isWeb || isAndroid) {
return ( return (
<Link <Link
onPointerEnter={() => {
if (isWeb) {
prefetchProfileQuery(props.did)
}
}}
href={makeProfileLink(props)} href={makeProfileLink(props)}
title={props.handle} title={props.handle}
asAnchor asAnchor