Skip to content

Commit

Permalink
fix(corel): update intent link resolve function to persist search params
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin committed Jul 8, 2024
1 parent 47e6dda commit 5c698f5
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ export function DocumentRow(props: {
params={{
id: getPublishedId(documentId, true),
type: documentTypeName,
perspective: `bundle.${release.name}`,
}}
searchParams={[['perspective', `bundle.${release.name}`]]}
ref={ref}
/>
)
Expand Down
1 change: 1 addition & 0 deletions packages/sanity/src/core/studio/router/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export function resolveIntentState(

const nextUrlState: Record<string, unknown> = {
...prevState,
_searchParams: nextState._searchParams,
tool: matchingTool.name,
[matchingTool.name]: toolState,
}
Expand Down
10 changes: 8 additions & 2 deletions packages/sanity/src/router/IntentLink.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {type ForwardedRef, forwardRef, type HTMLProps} from 'react'

import {type IntentParameters} from './types'
import {type IntentParameters, type SearchParam} from './types'
import {useIntentLink} from './useIntentLink'

/**
Expand All @@ -24,6 +24,11 @@ export interface IntentLinkProps {
* Whether to replace the current URL in the browser history instead of adding a new entry.
*/
replace?: boolean

/**
* search params to include in the intent.
*/
searchParams?: SearchParam[]
}

/**
Expand All @@ -43,12 +48,13 @@ export const IntentLink = forwardRef(function IntentLink(
props: IntentLinkProps & HTMLProps<HTMLAnchorElement>,
ref: ForwardedRef<HTMLAnchorElement>,
) {
const {intent, params, target, ...restProps} = props
const {intent, params, target, searchParams, ...restProps} = props
const {onClick, href} = useIntentLink({
intent,
params,
target,
onClick: props.onClick,
searchParams,
})

return <a {...restProps} href={href} onClick={onClick} ref={ref} target={target} />
Expand Down
19 changes: 9 additions & 10 deletions packages/sanity/src/router/RouterProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,14 @@ export function RouterProvider(props: RouterProviderProps): ReactElement {
const {onNavigate, router: routerProp, state} = props

const resolveIntentLink = useCallback(
(intentName: string, parameters?: IntentParameters): string => {
(intentName: string, parameters?: IntentParameters, searchParams?: SearchParam[]): string => {
const [params, payload] = Array.isArray(parameters) ? parameters : [parameters]
// TODO: Is there a better way to do this?
const {perspective, ...restParams} = params || {}
let path = routerProp.encode({intent: intentName, params: restParams, payload})
if (perspective) {
path += `?perspective=${perspective}`
}

return path
return routerProp.encode({
intent: intentName,
params: params,
payload,
_searchParams: searchParams,
})
},
[routerProp],
)
Expand Down Expand Up @@ -134,7 +132,8 @@ export function RouterProvider(props: RouterProviderProps): ReactElement {

const navigate = useCallback(
(nextState: Record<string, unknown>, options: NavigateOptions = {}) => {
onNavigate({path: resolvePathFromState(nextState), replace: options.replace})
const path = resolvePathFromState(nextState)
onNavigate({path, replace: options.replace})
},
[onNavigate, resolvePathFromState],
)
Expand Down
6 changes: 5 additions & 1 deletion packages/sanity/src/router/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,11 @@ export interface RouterContextValue {
* Resolves the intent link for the given intent name and parameters.
* See {@link IntentParameters}
*/
resolveIntentLink: (intentName: string, params?: IntentParameters) => string
resolveIntentLink: (
intentName: string,
params?: IntentParameters,
searchParams?: SearchParam[],
) => string

/**
* Navigates to the given URL.
Expand Down
10 changes: 7 additions & 3 deletions packages/sanity/src/router/useIntentLink.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {useMemo} from 'react'

import {type IntentParameters} from './types'
import {type IntentParameters, type SearchParam} from './types'
import {useLink} from './useLink'
import {useRouter} from './useRouter'

Expand Down Expand Up @@ -32,6 +32,7 @@ export interface UseIntentLinkOptions {
* The target window or frame to open the link in.
*/
target?: string
searchParams?: SearchParam[]
}

/**
Expand Down Expand Up @@ -59,9 +60,12 @@ export function useIntentLink(options: UseIntentLinkOptions): {
onClick: React.MouseEventHandler<HTMLElement>
href: string
} {
const {intent, onClick: onClickProp, params, replace, target} = options
const {intent, onClick: onClickProp, params, replace, target, searchParams} = options
const {resolveIntentLink} = useRouter()
const href = useMemo(() => resolveIntentLink(intent, params), [intent, params, resolveIntentLink])
const href = useMemo(
() => resolveIntentLink(intent, params, searchParams),
[intent, params, searchParams, resolveIntentLink],
)
const {onClick} = useLink({href, onClick: onClickProp, replace, target})

return {onClick, href}
Expand Down

0 comments on commit 5c698f5

Please sign in to comment.