Fix linking to specific search results (#8520)
* fix linking to bsky.app/search?q=xyz * add name to comment * skip if notfound
This commit is contained in:
+36
-7
@@ -9,7 +9,7 @@ import {
|
|||||||
import {BSKY_DOWNLOAD_URL} from '#/lib/constants'
|
import {BSKY_DOWNLOAD_URL} from '#/lib/constants'
|
||||||
import {useNavigationDeduped} from '#/lib/hooks/useNavigationDeduped'
|
import {useNavigationDeduped} from '#/lib/hooks/useNavigationDeduped'
|
||||||
import {useOpenLink} from '#/lib/hooks/useOpenLink'
|
import {useOpenLink} from '#/lib/hooks/useOpenLink'
|
||||||
import {type AllNavigatorParams} from '#/lib/routes/types'
|
import {type AllNavigatorParams, type RouteParams} from '#/lib/routes/types'
|
||||||
import {shareUrl} from '#/lib/sharing'
|
import {shareUrl} from '#/lib/sharing'
|
||||||
import {
|
import {
|
||||||
convertBskyAppUrlIfNeeded,
|
convertBskyAppUrlIfNeeded,
|
||||||
@@ -155,15 +155,44 @@ export function useLink({
|
|||||||
} else {
|
} else {
|
||||||
closeModal() // close any active modals
|
closeModal() // close any active modals
|
||||||
|
|
||||||
|
const [screen, params] = router.matchPath(href) as [
|
||||||
|
screen: keyof AllNavigatorParams,
|
||||||
|
params?: RouteParams,
|
||||||
|
]
|
||||||
|
|
||||||
|
// does not apply to web's flat navigator
|
||||||
|
if (isNative && screen !== 'NotFound') {
|
||||||
|
const state = navigation.getState()
|
||||||
|
// if screen is not in the current navigator, it means it's
|
||||||
|
// most likely a tab screen
|
||||||
|
if (!state.routeNames.includes(screen)) {
|
||||||
|
const parent = navigation.getParent()
|
||||||
|
if (
|
||||||
|
parent &&
|
||||||
|
parent.getState().routeNames.includes(`${screen}Tab`)
|
||||||
|
) {
|
||||||
|
// yep, it's a tab screen. i.e. SearchTab
|
||||||
|
// thus we need to navigate to the child screen
|
||||||
|
// via the parent navigator
|
||||||
|
// see https://reactnavigation.org/docs/upgrading-from-6.x/#changes-to-the-navigate-action
|
||||||
|
// TODO: can we support the other kinds of actions? push/replace -sfn
|
||||||
|
|
||||||
|
// @ts-expect-error include does not narrow the type unfortunately
|
||||||
|
parent.navigate(`${screen}Tab`, {screen, params})
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
// will probably fail, but let's try anyway
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (action === 'push') {
|
if (action === 'push') {
|
||||||
navigation.dispatch(StackActions.push(...router.matchPath(href)))
|
navigation.dispatch(StackActions.push(screen, params))
|
||||||
} else if (action === 'replace') {
|
} else if (action === 'replace') {
|
||||||
navigation.dispatch(
|
navigation.dispatch(StackActions.replace(screen, params))
|
||||||
StackActions.replace(...router.matchPath(href)),
|
|
||||||
)
|
|
||||||
} else if (action === 'navigate') {
|
} else if (action === 'navigate') {
|
||||||
// @ts-ignore
|
// @ts-expect-error not typed
|
||||||
navigation.navigate(...router.matchPath(href))
|
navigation.navigate(screen, params)
|
||||||
} else {
|
} else {
|
||||||
throw Error('Unsupported navigator action.')
|
throw Error('Unsupported navigator action.')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ export type DebouncedNavigationProp = Pick<
|
|||||||
| 'dispatch'
|
| 'dispatch'
|
||||||
| 'goBack'
|
| 'goBack'
|
||||||
| 'getState'
|
| 'getState'
|
||||||
|
| 'getParent'
|
||||||
>
|
>
|
||||||
|
|
||||||
export function useNavigationDeduped() {
|
export function useNavigationDeduped() {
|
||||||
@@ -46,6 +47,9 @@ export function useNavigationDeduped() {
|
|||||||
getState: () => {
|
getState: () => {
|
||||||
return navigation.getState()
|
return navigation.getState()
|
||||||
},
|
},
|
||||||
|
getParent: (...args: Parameters<typeof navigation.getParent>) => {
|
||||||
|
return navigation.getParent(...args)
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
[dedupe, navigation],
|
[dedupe, navigation],
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -193,6 +193,11 @@ export function convertBskyAppUrlIfNeeded(url: string): string {
|
|||||||
return startUriToStarterPackUri(urlp.pathname)
|
return startUriToStarterPackUri(urlp.pathname)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// special-case search links
|
||||||
|
if (urlp.pathname === '/search') {
|
||||||
|
return `/search?q=${urlp.searchParams.get('q')}`
|
||||||
|
}
|
||||||
|
|
||||||
return urlp.pathname
|
return urlp.pathname
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Unexpected error in convertBskyAppUrlIfNeeded()', e)
|
console.error('Unexpected error in convertBskyAppUrlIfNeeded()', e)
|
||||||
|
|||||||
Reference in New Issue
Block a user