Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: support for sticky params in URL and intent ops #7429

Merged
merged 8 commits into from
Sep 2, 2024
78 changes: 73 additions & 5 deletions packages/sanity/src/router/RouterProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {partition} from 'lodash'
import {type ReactElement, type ReactNode, useCallback, useMemo} from 'react'
import {RouterContext} from 'sanity/_singletons'

Expand Down Expand Up @@ -94,10 +95,39 @@ export function RouterProvider(props: RouterProviderProps): ReactElement {
)

const resolvePathFromState = useCallback(
(nextState: Record<string, unknown>): string => {
return routerProp.encode(nextState)
(nextState: RouterState): string => {
const currentStateParams = state._searchParams || []
const nextStateParams = nextState._searchParams || []
const nextParams = STICKY.reduce((acc, param) => {
return replaceStickyParam(
acc,
param,
findParam(nextStateParams, param) ?? findParam(currentStateParams, param),
)
}, nextStateParams || [])

return routerProp.encode({
...nextState,
_searchParams: nextParams,
})
},
[routerProp],
[routerProp, state],
)

const handleNavigateStickyParam = useCallback(
(param: string, value: string | undefined, options: NavigateOptions = {}) => {
if (!STICKY.includes(param)) {
throw new Error('Parameter is not sticky')
}
onNavigate({
path: resolvePathFromState({
...state,
_searchParams: [[param, value || '']],
}),
replace: options.replace,
})
},
[onNavigate, resolvePathFromState, state],
)

const navigate = useCallback(
Expand All @@ -114,17 +144,55 @@ export function RouterProvider(props: RouterProviderProps): ReactElement {
[onNavigate, resolveIntentLink],
)

const [routerState, stickyParams] = useMemo(() => {
if (!state._searchParams) {
return [state, null]
}
const {_searchParams, ...rest} = state
const [sticky, restParams] = partition(_searchParams, ([key]) => STICKY.includes(key))
if (sticky.length === 0) {
return [state, null]
}
return [{...rest, _searchParams: restParams}, sticky]
}, [state])

const router: RouterContextValue = useMemo(
() => ({
navigate,
navigateIntent,
navigateStickyParam: handleNavigateStickyParam,
navigateUrl: onNavigate,
resolveIntentLink,
resolvePathFromState,
state,
state: routerState,
stickyParams: Object.fromEntries(stickyParams || []),
}),
[navigate, navigateIntent, onNavigate, resolveIntentLink, resolvePathFromState, state],
[
handleNavigateStickyParam,
navigate,
navigateIntent,
onNavigate,
resolveIntentLink,
resolvePathFromState,
routerState,
stickyParams,
],
)

return <RouterContext.Provider value={router}>{props.children}</RouterContext.Provider>
}
const STICKY: string[] = []
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently there are no sticky params defined, however in content releases, the perspective key will be added here (the first current use of sticky params), but they can be used in other instances also


function replaceStickyParam(
current: SearchParam[],
param: string,
value: string | undefined,
): SearchParam[] {
const filtered = current.filter(([key]) => key !== param)
return value === undefined || value == '' ? filtered : [...filtered, [param, value]]
}

function findParam(searchParams: SearchParam[], key: string): string | undefined {
const entry = searchParams.find(([k]) => k === key)
return entry ? entry[1] : undefined
}
10 changes: 10 additions & 0 deletions packages/sanity/src/router/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ export interface RouterContextValue {
*/
navigateUrl: (opts: {path: string; replace?: boolean}) => void

/**
* Navigates to the current URL with the sticky url search param set to the given value
*/
navigateStickyParam: (param: string, value: string, options?: NavigateOptions) => void

/**
* Navigates to the given router state.
* See {@link RouterState} and {@link NavigateOptions}
Expand All @@ -280,4 +285,9 @@ export interface RouterContextValue {
* The current router state. See {@link RouterState}
*/
state: RouterState

/**
* The current router state. See {@link RouterState}
*/
stickyParams: Record<string, string | undefined>
}
8 changes: 7 additions & 1 deletion packages/sanity/src/structure/components/IntentButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ export const IntentButton = forwardRef(function IntentButton(
linkRef: ForwardedRef<HTMLAnchorElement>,
) {
return (
<IntentLink {...linkProps} intent={intent.type} params={intent.params} ref={linkRef} />
<IntentLink
{...linkProps}
intent={intent.type}
params={intent.params}
ref={linkRef}
searchParams={intent.searchParams}
/>
)
}),
[intent],
Expand Down
4 changes: 4 additions & 0 deletions packages/sanity/src/structure/structureBuilder/Intent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {type SearchParam} from 'sanity/router'

import {getTypeNamesFromFilter, type PartialDocumentList} from './DocumentList'
import {type StructureNode} from './StructureNodes'

Expand Down Expand Up @@ -75,6 +77,8 @@ export interface Intent {
/** Intent parameters. See {@link IntentParams}
*/
params?: IntentParams

searchParams?: SearchParam[]
}

/**
Expand Down
Loading