Fix self mention, resolve handle (#1903)
* Fix self mention, resolve handle * Use queryClient * Fix type * Remove staleTimezio/stable
parent
8e4a3ad5b6
commit
ab6e3f2c5d
|
@ -0,0 +1,25 @@
|
||||||
|
import React from 'react'
|
||||||
|
import {useQueryClient} from '@tanstack/react-query'
|
||||||
|
|
||||||
|
import {useSession} from '#/state/session'
|
||||||
|
|
||||||
|
const fetchHandleQueryKey = (handleOrDid: string) => ['handle', handleOrDid]
|
||||||
|
|
||||||
|
export function useFetchHandle() {
|
||||||
|
const {agent} = useSession()
|
||||||
|
const queryClient = useQueryClient()
|
||||||
|
|
||||||
|
return React.useCallback(
|
||||||
|
async (handleOrDid: string) => {
|
||||||
|
if (handleOrDid.startsWith('did:')) {
|
||||||
|
const res = await queryClient.fetchQuery({
|
||||||
|
queryKey: fetchHandleQueryKey(handleOrDid),
|
||||||
|
queryFn: () => agent.getProfile({actor: handleOrDid}),
|
||||||
|
})
|
||||||
|
return res.data.handle
|
||||||
|
}
|
||||||
|
return handleOrDid
|
||||||
|
},
|
||||||
|
[agent, queryClient],
|
||||||
|
)
|
||||||
|
}
|
|
@ -45,6 +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 {useFetchHandle} from '#/state/queries/handle'
|
||||||
|
|
||||||
const ProfileCard = observer(function ProfileCardImpl() {
|
const ProfileCard = observer(function ProfileCardImpl() {
|
||||||
const {currentAccount} = useSession()
|
const {currentAccount} = useSession()
|
||||||
|
@ -124,6 +125,7 @@ const NavItem = observer(function NavItemImpl({
|
||||||
label,
|
label,
|
||||||
}: NavItemProps) {
|
}: NavItemProps) {
|
||||||
const pal = usePalette('default')
|
const pal = usePalette('default')
|
||||||
|
const {currentAccount} = useSession()
|
||||||
const store = useStores()
|
const store = useStores()
|
||||||
const {isDesktop, isTablet} = useWebMediaQueries()
|
const {isDesktop, isTablet} = useWebMediaQueries()
|
||||||
const [pathName] = React.useMemo(() => router.matchPath(href), [href])
|
const [pathName] = React.useMemo(() => router.matchPath(href), [href])
|
||||||
|
@ -137,7 +139,7 @@ const NavItem = observer(function NavItemImpl({
|
||||||
currentRouteInfo.name === 'Profile'
|
currentRouteInfo.name === 'Profile'
|
||||||
? isTab(currentRouteInfo.name, pathName) &&
|
? isTab(currentRouteInfo.name, pathName) &&
|
||||||
(currentRouteInfo.params as CommonNavigatorParams['Profile']).name ===
|
(currentRouteInfo.params as CommonNavigatorParams['Profile']).name ===
|
||||||
store.me.handle
|
currentAccount?.handle
|
||||||
: isTab(currentRouteInfo.name, pathName)
|
: isTab(currentRouteInfo.name, pathName)
|
||||||
const {onPress} = useLinkProps({to: href})
|
const {onPress} = useLinkProps({to: href})
|
||||||
const onPressWrapped = React.useCallback(
|
const onPressWrapped = React.useCallback(
|
||||||
|
@ -194,11 +196,13 @@ const NavItem = observer(function NavItemImpl({
|
||||||
})
|
})
|
||||||
|
|
||||||
function ComposeBtn() {
|
function ComposeBtn() {
|
||||||
const store = useStores()
|
const {currentAccount} = useSession()
|
||||||
const {getState} = useNavigation()
|
const {getState} = useNavigation()
|
||||||
const {openComposer} = useComposerControls()
|
const {openComposer} = useComposerControls()
|
||||||
const {_} = useLingui()
|
const {_} = useLingui()
|
||||||
const {isTablet} = useWebMediaQueries()
|
const {isTablet} = useWebMediaQueries()
|
||||||
|
const [isFetchingHandle, setIsFetchingHandle] = React.useState(false)
|
||||||
|
const fetchHandle = useFetchHandle()
|
||||||
|
|
||||||
const getProfileHandle = async () => {
|
const getProfileHandle = async () => {
|
||||||
const {routes} = getState()
|
const {routes} = getState()
|
||||||
|
@ -210,13 +214,21 @@ function ComposeBtn() {
|
||||||
).name
|
).name
|
||||||
|
|
||||||
if (handle.startsWith('did:')) {
|
if (handle.startsWith('did:')) {
|
||||||
const cached = await store.profiles.cache.get(handle)
|
try {
|
||||||
const profile = cached ? cached.data : undefined
|
setIsFetchingHandle(true)
|
||||||
// if we can't resolve handle, set to undefined
|
handle = await fetchHandle(handle)
|
||||||
handle = profile?.handle || undefined
|
} catch (e) {
|
||||||
|
handle = undefined
|
||||||
|
} finally {
|
||||||
|
setIsFetchingHandle(false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!handle || handle === store.me.handle || handle === 'handle.invalid')
|
if (
|
||||||
|
!handle ||
|
||||||
|
handle === currentAccount?.handle ||
|
||||||
|
handle === 'handle.invalid'
|
||||||
|
)
|
||||||
return undefined
|
return undefined
|
||||||
|
|
||||||
return handle
|
return handle
|
||||||
|
@ -233,6 +245,7 @@ function ComposeBtn() {
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
|
disabled={isFetchingHandle}
|
||||||
style={[styles.newPostBtn]}
|
style={[styles.newPostBtn]}
|
||||||
onPress={onPressCompose}
|
onPress={onPressCompose}
|
||||||
accessibilityRole="button"
|
accessibilityRole="button"
|
||||||
|
@ -372,7 +385,7 @@ export const DesktopLeftNav = observer(function DesktopLeftNav() {
|
||||||
label="Moderation"
|
label="Moderation"
|
||||||
/>
|
/>
|
||||||
<NavItem
|
<NavItem
|
||||||
href={makeProfileLink(currentAccount)}
|
href={currentAccount ? makeProfileLink(currentAccount) : '/'}
|
||||||
icon={
|
icon={
|
||||||
<UserIcon
|
<UserIcon
|
||||||
strokeWidth={1.75}
|
strokeWidth={1.75}
|
||||||
|
|
Loading…
Reference in New Issue