From 5cb335b1a2dcdf5cc967e88e22d2e8c082bb9c2e Mon Sep 17 00:00:00 2001 From: Jordan Lawrence Date: Tue, 10 Sep 2024 12:53:41 +0100 Subject: [PATCH] Revert "chore: support for sticky params in URL and intent ops (#7429)" (#7489) This reverts commit 8bc721bc73f9579a6cab1440c673f8148a8e2a4d. --- .../sanity/src/router/IntentLink.test.tsx | 77 +---------------- packages/sanity/src/router/RouterProvider.tsx | 85 ++----------------- packages/sanity/src/router/stickyParams.ts | 1 - packages/sanity/src/router/types.ts | 10 --- .../src/structure/components/IntentButton.tsx | 8 +- .../src/structure/structureBuilder/Intent.ts | 4 - 6 files changed, 12 insertions(+), 173 deletions(-) delete mode 100644 packages/sanity/src/router/stickyParams.ts diff --git a/packages/sanity/src/router/IntentLink.test.tsx b/packages/sanity/src/router/IntentLink.test.tsx index 008ca2f0042..a08cbd9b344 100644 --- a/packages/sanity/src/router/IntentLink.test.tsx +++ b/packages/sanity/src/router/IntentLink.test.tsx @@ -1,15 +1,10 @@ -import {describe, expect, it, jest} from '@jest/globals' +import {describe, expect, it} from '@jest/globals' import {render} from '@testing-library/react' -import {noop} from 'lodash' import {IntentLink} from './IntentLink' import {route} from './route' import {RouterProvider} from './RouterProvider' -jest.mock('./stickyParams', () => ({ - STICKY_PARAMS: ['aTestStickyParam'], -})) - describe('IntentLink', () => { it('should resolve intent link with query params', () => { const router = route.create('/test', [route.intents('/intent')]) @@ -20,41 +15,11 @@ describe('IntentLink', () => { id: 'document-id-123', type: 'document-type', }} - searchParams={[['aTestStickyParam', `aStickyParam.value`]]} - />, - { - wrapper: ({children}) => ( - - {children} - - ), - }, - ) - // Component should render the query param in the href - expect(component.container.querySelector('a')?.href).toContain( - '/test/intent/edit/id=document-id-123;type=document-type/?aTestStickyParam=aStickyParam.value', - ) - }) - - it('should preserve sticky parameters when resolving intent link', () => { - const router = route.create('/test', [route.intents('/intent')]) - const component = render( - , { wrapper: ({children}) => ( - + null} router={router} state={{}}> {children} ), @@ -62,41 +27,7 @@ describe('IntentLink', () => { ) // Component should render the query param in the href expect(component.container.querySelector('a')?.href).toContain( - '/test/intent/edit/id=document-id-123;type=document-type/?aTestStickyParam=aStickyParam.value', - ) - }) - - it('should allow sticky parameters to be overridden when resolving intent link', () => { - const router = route.create('/test', [route.intents('/intent')]) - const component = render( - , - { - wrapper: ({children}) => ( - - {children} - - ), - }, - ) - // Component should render the query param in the href - expect(component.container.querySelector('a')?.href).toContain( - '/test/intent/edit/id=document-id-123;type=document-type/?aTestStickyParam=aStickyParam.value.to-be-defined', - ) - expect(component.container.querySelector('a')?.href).not.toContain( - 'aTestStickyParam=aStickyParam.value.to-be-overridden', + '/test/intent/edit/id=document-id-123;type=document-type/?perspective=bundle.summer-drop', ) }) }) diff --git a/packages/sanity/src/router/RouterProvider.tsx b/packages/sanity/src/router/RouterProvider.tsx index 4b819bff1a4..d0f8ada9523 100644 --- a/packages/sanity/src/router/RouterProvider.tsx +++ b/packages/sanity/src/router/RouterProvider.tsx @@ -1,8 +1,6 @@ -import {fromPairs, partition, toPairs} from 'lodash' import {type ReactElement, type ReactNode, useCallback, useMemo} from 'react' import {RouterContext} from 'sanity/_singletons' -import {STICKY_PARAMS} from './stickyParams' import { type IntentParameters, type NavigateOptions, @@ -89,49 +87,17 @@ export function RouterProvider(props: RouterProviderProps): ReactElement { intent: intentName, params, payload, - _searchParams: toPairs({ - ...fromPairs((state._searchParams ?? []).filter(([key]) => STICKY_PARAMS.includes(key))), - ...fromPairs(_searchParams ?? []), - }), + _searchParams, }) }, - [routerProp, state._searchParams], + [routerProp], ) const resolvePathFromState = useCallback( - (nextState: RouterState): string => { - const currentStateParams = state._searchParams || [] - const nextStateParams = nextState._searchParams || [] - const nextParams = STICKY_PARAMS.reduce((acc, param) => { - return replaceStickyParam( - acc, - param, - findParam(nextStateParams, param) ?? findParam(currentStateParams, param), - ) - }, nextStateParams || []) - - return routerProp.encode({ - ...nextState, - _searchParams: nextParams, - }) - }, - [routerProp, state], - ) - - const handleNavigateStickyParam = useCallback( - (param: string, value: string | undefined, options: NavigateOptions = {}) => { - if (!STICKY_PARAMS.includes(param)) { - throw new Error('Parameter is not sticky') - } - onNavigate({ - path: resolvePathFromState({ - ...state, - _searchParams: [[param, value || '']], - }), - replace: options.replace, - }) + (nextState: Record): string => { + return routerProp.encode(nextState) }, - [onNavigate, resolvePathFromState, state], + [routerProp], ) const navigate = useCallback( @@ -148,54 +114,17 @@ 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_PARAMS.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: routerState, - stickyParams: Object.fromEntries(stickyParams || []), + state, }), - [ - handleNavigateStickyParam, - navigate, - navigateIntent, - onNavigate, - resolveIntentLink, - resolvePathFromState, - routerState, - stickyParams, - ], + [navigate, navigateIntent, onNavigate, resolveIntentLink, resolvePathFromState, state], ) return {props.children} } - -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 -} diff --git a/packages/sanity/src/router/stickyParams.ts b/packages/sanity/src/router/stickyParams.ts deleted file mode 100644 index 577e1874654..00000000000 --- a/packages/sanity/src/router/stickyParams.ts +++ /dev/null @@ -1 +0,0 @@ -export const STICKY_PARAMS: string[] = [] diff --git a/packages/sanity/src/router/types.ts b/packages/sanity/src/router/types.ts index 69ba8dd4d9f..7b219ae811c 100644 --- a/packages/sanity/src/router/types.ts +++ b/packages/sanity/src/router/types.ts @@ -264,11 +264,6 @@ 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} @@ -285,9 +280,4 @@ export interface RouterContextValue { * The current router state. See {@link RouterState} */ state: RouterState - - /** - * The current router state. See {@link RouterState} - */ - stickyParams: Record } diff --git a/packages/sanity/src/structure/components/IntentButton.tsx b/packages/sanity/src/structure/components/IntentButton.tsx index 6a82570072e..fbba8eed29b 100644 --- a/packages/sanity/src/structure/components/IntentButton.tsx +++ b/packages/sanity/src/structure/components/IntentButton.tsx @@ -23,13 +23,7 @@ export const IntentButton = forwardRef(function IntentButton( linkRef: ForwardedRef, ) { return ( - + ) }), [intent], diff --git a/packages/sanity/src/structure/structureBuilder/Intent.ts b/packages/sanity/src/structure/structureBuilder/Intent.ts index 956da0c021f..ad0810f6379 100644 --- a/packages/sanity/src/structure/structureBuilder/Intent.ts +++ b/packages/sanity/src/structure/structureBuilder/Intent.ts @@ -1,5 +1,3 @@ -import {type SearchParam} from 'sanity/router' - import {getTypeNamesFromFilter, type PartialDocumentList} from './DocumentList' import {type StructureNode} from './StructureNodes' @@ -77,8 +75,6 @@ export interface Intent { /** Intent parameters. See {@link IntentParams} */ params?: IntentParams - - searchParams?: SearchParam[] } /**