Skip to content

Commit 5cb335b

Browse files
authored
Revert "chore: support for sticky params in URL and intent ops (#7429)" (#7489)
This reverts commit 8bc721b.
1 parent de03c57 commit 5cb335b

File tree

6 files changed

+12
-173
lines changed

6 files changed

+12
-173
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import {describe, expect, it, jest} from '@jest/globals'
1+
import {describe, expect, it} from '@jest/globals'
22
import {render} from '@testing-library/react'
3-
import {noop} from 'lodash'
43

54
import {IntentLink} from './IntentLink'
65
import {route} from './route'
76
import {RouterProvider} from './RouterProvider'
87

9-
jest.mock('./stickyParams', () => ({
10-
STICKY_PARAMS: ['aTestStickyParam'],
11-
}))
12-
138
describe('IntentLink', () => {
149
it('should resolve intent link with query params', () => {
1510
const router = route.create('/test', [route.intents('/intent')])
@@ -20,83 +15,19 @@ describe('IntentLink', () => {
2015
id: 'document-id-123',
2116
type: 'document-type',
2217
}}
23-
searchParams={[['aTestStickyParam', `aStickyParam.value`]]}
24-
/>,
25-
{
26-
wrapper: ({children}) => (
27-
<RouterProvider onNavigate={noop} router={router} state={{}}>
28-
{children}
29-
</RouterProvider>
30-
),
31-
},
32-
)
33-
// Component should render the query param in the href
34-
expect(component.container.querySelector('a')?.href).toContain(
35-
'/test/intent/edit/id=document-id-123;type=document-type/?aTestStickyParam=aStickyParam.value',
36-
)
37-
})
38-
39-
it('should preserve sticky parameters when resolving intent link', () => {
40-
const router = route.create('/test', [route.intents('/intent')])
41-
const component = render(
42-
<IntentLink
43-
intent="edit"
44-
params={{
45-
id: 'document-id-123',
46-
type: 'document-type',
47-
}}
18+
searchParams={[['perspective', `bundle.summer-drop`]]}
4819
/>,
4920
{
5021
wrapper: ({children}) => (
51-
<RouterProvider
52-
onNavigate={noop}
53-
router={router}
54-
state={{
55-
_searchParams: [['aTestStickyParam', 'aStickyParam.value']],
56-
}}
57-
>
22+
<RouterProvider onNavigate={() => null} router={router} state={{}}>
5823
{children}
5924
</RouterProvider>
6025
),
6126
},
6227
)
6328
// Component should render the query param in the href
6429
expect(component.container.querySelector('a')?.href).toContain(
65-
'/test/intent/edit/id=document-id-123;type=document-type/?aTestStickyParam=aStickyParam.value',
66-
)
67-
})
68-
69-
it('should allow sticky parameters to be overridden when resolving intent link', () => {
70-
const router = route.create('/test', [route.intents('/intent')])
71-
const component = render(
72-
<IntentLink
73-
intent="edit"
74-
params={{
75-
id: 'document-id-123',
76-
type: 'document-type',
77-
}}
78-
searchParams={[['aTestStickyParam', `aStickyParam.value.to-be-defined`]]}
79-
/>,
80-
{
81-
wrapper: ({children}) => (
82-
<RouterProvider
83-
onNavigate={noop}
84-
router={router}
85-
state={{
86-
_searchParams: [['aTestStickyParam', 'aStickyParam.value.to-be-overridden']],
87-
}}
88-
>
89-
{children}
90-
</RouterProvider>
91-
),
92-
},
93-
)
94-
// Component should render the query param in the href
95-
expect(component.container.querySelector('a')?.href).toContain(
96-
'/test/intent/edit/id=document-id-123;type=document-type/?aTestStickyParam=aStickyParam.value.to-be-defined',
97-
)
98-
expect(component.container.querySelector('a')?.href).not.toContain(
99-
'aTestStickyParam=aStickyParam.value.to-be-overridden',
30+
'/test/intent/edit/id=document-id-123;type=document-type/?perspective=bundle.summer-drop',
10031
)
10132
})
10233
})

packages/sanity/src/router/RouterProvider.tsx

+7-78
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import {fromPairs, partition, toPairs} from 'lodash'
21
import {type ReactElement, type ReactNode, useCallback, useMemo} from 'react'
32
import {RouterContext} from 'sanity/_singletons'
43

5-
import {STICKY_PARAMS} from './stickyParams'
64
import {
75
type IntentParameters,
86
type NavigateOptions,
@@ -89,49 +87,17 @@ export function RouterProvider(props: RouterProviderProps): ReactElement {
8987
intent: intentName,
9088
params,
9189
payload,
92-
_searchParams: toPairs({
93-
...fromPairs((state._searchParams ?? []).filter(([key]) => STICKY_PARAMS.includes(key))),
94-
...fromPairs(_searchParams ?? []),
95-
}),
90+
_searchParams,
9691
})
9792
},
98-
[routerProp, state._searchParams],
93+
[routerProp],
9994
)
10095

10196
const resolvePathFromState = useCallback(
102-
(nextState: RouterState): string => {
103-
const currentStateParams = state._searchParams || []
104-
const nextStateParams = nextState._searchParams || []
105-
const nextParams = STICKY_PARAMS.reduce((acc, param) => {
106-
return replaceStickyParam(
107-
acc,
108-
param,
109-
findParam(nextStateParams, param) ?? findParam(currentStateParams, param),
110-
)
111-
}, nextStateParams || [])
112-
113-
return routerProp.encode({
114-
...nextState,
115-
_searchParams: nextParams,
116-
})
117-
},
118-
[routerProp, state],
119-
)
120-
121-
const handleNavigateStickyParam = useCallback(
122-
(param: string, value: string | undefined, options: NavigateOptions = {}) => {
123-
if (!STICKY_PARAMS.includes(param)) {
124-
throw new Error('Parameter is not sticky')
125-
}
126-
onNavigate({
127-
path: resolvePathFromState({
128-
...state,
129-
_searchParams: [[param, value || '']],
130-
}),
131-
replace: options.replace,
132-
})
97+
(nextState: Record<string, unknown>): string => {
98+
return routerProp.encode(nextState)
13399
},
134-
[onNavigate, resolvePathFromState, state],
100+
[routerProp],
135101
)
136102

137103
const navigate = useCallback(
@@ -148,54 +114,17 @@ export function RouterProvider(props: RouterProviderProps): ReactElement {
148114
[onNavigate, resolveIntentLink],
149115
)
150116

151-
const [routerState, stickyParams] = useMemo(() => {
152-
if (!state._searchParams) {
153-
return [state, null]
154-
}
155-
const {_searchParams, ...rest} = state
156-
const [sticky, restParams] = partition(_searchParams, ([key]) => STICKY_PARAMS.includes(key))
157-
if (sticky.length === 0) {
158-
return [state, null]
159-
}
160-
return [{...rest, _searchParams: restParams}, sticky]
161-
}, [state])
162-
163117
const router: RouterContextValue = useMemo(
164118
() => ({
165119
navigate,
166120
navigateIntent,
167-
navigateStickyParam: handleNavigateStickyParam,
168121
navigateUrl: onNavigate,
169122
resolveIntentLink,
170123
resolvePathFromState,
171-
state: routerState,
172-
stickyParams: Object.fromEntries(stickyParams || []),
124+
state,
173125
}),
174-
[
175-
handleNavigateStickyParam,
176-
navigate,
177-
navigateIntent,
178-
onNavigate,
179-
resolveIntentLink,
180-
resolvePathFromState,
181-
routerState,
182-
stickyParams,
183-
],
126+
[navigate, navigateIntent, onNavigate, resolveIntentLink, resolvePathFromState, state],
184127
)
185128

186129
return <RouterContext.Provider value={router}>{props.children}</RouterContext.Provider>
187130
}
188-
189-
function replaceStickyParam(
190-
current: SearchParam[],
191-
param: string,
192-
value: string | undefined,
193-
): SearchParam[] {
194-
const filtered = current.filter(([key]) => key !== param)
195-
return value === undefined || value == '' ? filtered : [...filtered, [param, value]]
196-
}
197-
198-
function findParam(searchParams: SearchParam[], key: string): string | undefined {
199-
const entry = searchParams.find(([k]) => k === key)
200-
return entry ? entry[1] : undefined
201-
}

packages/sanity/src/router/stickyParams.ts

-1
This file was deleted.

packages/sanity/src/router/types.ts

-10
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,6 @@ export interface RouterContextValue {
264264
*/
265265
navigateUrl: (opts: {path: string; replace?: boolean}) => void
266266

267-
/**
268-
* Navigates to the current URL with the sticky url search param set to the given value
269-
*/
270-
navigateStickyParam: (param: string, value: string, options?: NavigateOptions) => void
271-
272267
/**
273268
* Navigates to the given router state.
274269
* See {@link RouterState} and {@link NavigateOptions}
@@ -285,9 +280,4 @@ export interface RouterContextValue {
285280
* The current router state. See {@link RouterState}
286281
*/
287282
state: RouterState
288-
289-
/**
290-
* The current router state. See {@link RouterState}
291-
*/
292-
stickyParams: Record<string, string | undefined>
293283
}

packages/sanity/src/structure/components/IntentButton.tsx

+1-7
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,7 @@ export const IntentButton = forwardRef(function IntentButton(
2323
linkRef: ForwardedRef<HTMLAnchorElement>,
2424
) {
2525
return (
26-
<IntentLink
27-
{...linkProps}
28-
intent={intent.type}
29-
params={intent.params}
30-
ref={linkRef}
31-
searchParams={intent.searchParams}
32-
/>
26+
<IntentLink {...linkProps} intent={intent.type} params={intent.params} ref={linkRef} />
3327
)
3428
}),
3529
[intent],

packages/sanity/src/structure/structureBuilder/Intent.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import {type SearchParam} from 'sanity/router'
2-
31
import {getTypeNamesFromFilter, type PartialDocumentList} from './DocumentList'
42
import {type StructureNode} from './StructureNodes'
53

@@ -77,8 +75,6 @@ export interface Intent {
7775
/** Intent parameters. See {@link IntentParams}
7876
*/
7977
params?: IntentParams
80-
81-
searchParams?: SearchParam[]
8278
}
8379

8480
/**

0 commit comments

Comments
 (0)