Use queryClient
This commit is contained in:
+12
-19
@@ -1,34 +1,27 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import {useMutation} from '@tanstack/react-query'
|
import {useQueryClient} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {useSession} from '#/state/session'
|
import {useSession} from '#/state/session'
|
||||||
|
|
||||||
export function useGetHandle() {
|
const fetchHandleQueryKey = (handleOrDid: string) => ['handle', handleOrDid]
|
||||||
|
|
||||||
|
export function useFetchHandle() {
|
||||||
const {agent} = useSession()
|
const {agent} = useSession()
|
||||||
|
const queryClient = useQueryClient()
|
||||||
|
|
||||||
return React.useCallback(
|
return React.useCallback(
|
||||||
async (handleOrDid: string) => {
|
async (handleOrDid: string) => {
|
||||||
if (handleOrDid.startsWith('did:')) {
|
if (handleOrDid.startsWith('did:')) {
|
||||||
const res = await agent.getProfile({actor: handleOrDid})
|
const res = await queryClient.fetchQuery({
|
||||||
|
// cache in memory forever, page reload clears
|
||||||
|
staleTime: Infinity,
|
||||||
|
queryKey: fetchHandleQueryKey(handleOrDid),
|
||||||
|
queryFn: () => agent.getProfile({actor: handleOrDid}),
|
||||||
|
})
|
||||||
return res.data.handle
|
return res.data.handle
|
||||||
}
|
}
|
||||||
return handleOrDid
|
return handleOrDid
|
||||||
},
|
},
|
||||||
[agent],
|
[agent, queryClient],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useGetHandleMutation() {
|
|
||||||
const {agent} = useSession()
|
|
||||||
|
|
||||||
return useMutation({
|
|
||||||
mutationFn: async (handleOrDid: string) => {
|
|
||||||
if (handleOrDid.startsWith('did:')) {
|
|
||||||
// TODO would be nice to do this without all the other fetched data
|
|
||||||
const res = await agent.getProfile({actor: handleOrDid})
|
|
||||||
return res.data.handle
|
|
||||||
}
|
|
||||||
return handleOrDid
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ import {useProfileQuery} from '#/state/queries/profile'
|
|||||||
import {useSession} from '#/state/session'
|
import {useSession} from '#/state/session'
|
||||||
import {useUnreadNotifications} from '#/state/queries/notifications/unread'
|
import {useUnreadNotifications} from '#/state/queries/notifications/unread'
|
||||||
import {useComposerControls} from '#/state/shell/composer'
|
import {useComposerControls} from '#/state/shell/composer'
|
||||||
import {useGetHandleMutation} from '#/state/queries/handle'
|
import {useFetchHandle} from '#/state/queries/handle'
|
||||||
|
|
||||||
const ProfileCard = observer(function ProfileCardImpl() {
|
const ProfileCard = observer(function ProfileCardImpl() {
|
||||||
const {currentAccount} = useSession()
|
const {currentAccount} = useSession()
|
||||||
@@ -201,8 +201,8 @@ function ComposeBtn() {
|
|||||||
const {openComposer} = useComposerControls()
|
const {openComposer} = useComposerControls()
|
||||||
const {_} = useLingui()
|
const {_} = useLingui()
|
||||||
const {isTablet} = useWebMediaQueries()
|
const {isTablet} = useWebMediaQueries()
|
||||||
const {mutateAsync: getHandle, isPending: isGetHandlePending} =
|
const [isFetchingHandle, setIsFetchingHandle] = React.useState(false)
|
||||||
useGetHandleMutation()
|
const fetchHandle = useFetchHandle()
|
||||||
|
|
||||||
const getProfileHandle = async () => {
|
const getProfileHandle = async () => {
|
||||||
const {routes} = getState()
|
const {routes} = getState()
|
||||||
@@ -215,9 +215,12 @@ function ComposeBtn() {
|
|||||||
|
|
||||||
if (handle.startsWith('did:')) {
|
if (handle.startsWith('did:')) {
|
||||||
try {
|
try {
|
||||||
handle = await getHandle(handle)
|
setIsFetchingHandle(true)
|
||||||
|
handle = await fetchHandle(handle)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
handle = undefined
|
handle = undefined
|
||||||
|
} finally {
|
||||||
|
setIsFetchingHandle(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,7 +245,7 @@ function ComposeBtn() {
|
|||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
disabled={isGetHandlePending}
|
disabled={isFetchingHandle}
|
||||||
style={[styles.newPostBtn]}
|
style={[styles.newPostBtn]}
|
||||||
onPress={onPressCompose}
|
onPress={onPressCompose}
|
||||||
accessibilityRole="button"
|
accessibilityRole="button"
|
||||||
|
|||||||
Reference in New Issue
Block a user