Skip to content

Commit 4d9f2d6

Browse files
authored
Merge pull request #1898 from codeforboston/revert-1766-ctoon/1753/upgrade-instantsearch-router
Revert "[1753] Replace custom routing with react-instantsearch-router-nextjs"
2 parents 7e1b9bc + 8a85105 commit 4d9f2d6

File tree

6 files changed

+62
-119
lines changed

6 files changed

+62
-119
lines changed

components/search/bills/BillSearch.tsx

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,21 @@ import {
77
SearchBox,
88
useInstantSearch
99
} from "react-instantsearch"
10-
import { createInstantSearchRouterNext } from "react-instantsearch-router-nextjs"
11-
import singletonRouter from "next/router"
10+
import { currentGeneralCourt } from "functions/src/shared"
1211
import styled from "styled-components"
1312
import TypesenseInstantSearchAdapter from "typesense-instantsearch-adapter"
14-
import { currentGeneralCourt } from "functions/src/shared"
1513
import { Col, Container, Row, Spinner } from "../../bootstrap"
1614
import { NoResults } from "../NoResults"
1715
import { ResultCount } from "../ResultCount"
1816
import { SearchContainer } from "../SearchContainer"
1917
import { SearchErrorBoundary } from "../SearchErrorBoundary"
18+
import { useRouting } from "../useRouting"
2019
import { BillHit } from "./BillHit"
2120
import { useBillRefinements } from "./useBillRefinements"
2221
import { SortBy, SortByWithConfigurationItem } from "../SortBy"
2322
import { getServerConfig, VirtualFilters } from "../common"
2423
import { useBillSort } from "./useBillSort"
2524
import { FC, useState } from "react"
26-
import { pathToSearchState, searchStateToUrl } from "../routingHelpers"
2725

2826
const searchClient = new TypesenseInstantSearchAdapter({
2927
server: getServerConfig(),
@@ -72,16 +70,8 @@ export const BillSearch = () => {
7270
}
7371
}}
7472
searchClient={searchClient}
75-
routing={{
76-
router: createInstantSearchRouterNext({
77-
singletonRouter,
78-
routerOptions: {
79-
cleanUrlOnDispose: false,
80-
createURL: args => searchStateToUrl(args),
81-
parseURL: args => pathToSearchState(args)
82-
}
83-
})
84-
}}
73+
routing={useRouting()}
74+
future={{ preserveSharedStateOnUnmount: true }}
8575
>
8676
<VirtualFilters type="bill" />
8777
<Layout items={items} />

components/search/routingHelpers.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.

components/search/testimony/TestimonySearch.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import {
66
SearchBox,
77
useInstantSearch
88
} from "react-instantsearch"
9-
import { createInstantSearchRouterNext } from "react-instantsearch-router-nextjs"
10-
import singletonRouter from "next/router"
119
import {
1210
StyledTabContent,
1311
StyledTabNav
@@ -25,10 +23,10 @@ import { SearchContainer } from "../SearchContainer"
2523
import { SearchErrorBoundary } from "../SearchErrorBoundary"
2624
import { SortBy } from "../SortBy"
2725
import { getServerConfig, VirtualFilters } from "../common"
26+
import { useRouting } from "../useRouting"
2827
import { TestimonyHit } from "./TestimonyHit"
2928
import { useTestimonyRefinements } from "./useTestimonyRefinements"
3029
import { FollowContext, OrgFollowStatus } from "components/shared/FollowContext"
31-
import { pathToSearchState, searchStateToUrl } from "../routingHelpers"
3230

3331
const searchClient = new TypesenseInstantSearchAdapter({
3432
server: getServerConfig(),
@@ -65,16 +63,8 @@ export const TestimonySearch = () => (
6563
}
6664
}}
6765
searchClient={searchClient}
68-
routing={{
69-
router: createInstantSearchRouterNext({
70-
singletonRouter,
71-
routerOptions: {
72-
cleanUrlOnDispose: false,
73-
createURL: args => searchStateToUrl(args),
74-
parseURL: args => pathToSearchState(args)
75-
}
76-
})
77-
}}
66+
routing={useRouting()}
67+
future={{ preserveSharedStateOnUnmount: true }}
7868
>
7969
<VirtualFilters type="testimony" />
8070
<Layout />

components/search/useRouting.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { UiState } from "instantsearch.js"
2+
import { RouterProps } from "instantsearch.js/es/middlewares"
3+
import Router from "next/router"
4+
import qs from "qs"
5+
import { useMemo } from "react"
6+
7+
const pathToSearchState = (path: string) =>
8+
(path.includes("?")
9+
? qs.parse(path.substring(path.indexOf("?") + 1))
10+
: {}) as UiState
11+
12+
const searchStateToUrl = (searchState: UiState) => {
13+
const base = window.location.pathname
14+
15+
const flagQueries = Object.fromEntries(
16+
Object.entries(qs.parse(window.location.search.slice(1))).filter(
17+
([key]) => !Object.keys(searchState).includes(key)
18+
)
19+
)
20+
21+
const query = qs.stringify({
22+
...searchState,
23+
...flagQueries
24+
})
25+
26+
return query ? `${base}?${query}` : base
27+
}
28+
29+
export function useRouting(): RouterProps<UiState, UiState> {
30+
return useMemo(() => {
31+
let disposed = false
32+
return {
33+
router: {
34+
createURL: searchStateToUrl,
35+
dispose() {
36+
disposed = true
37+
// Clear back listener
38+
Router.beforePopState(() => true)
39+
},
40+
onUpdate(callback) {
41+
Router.beforePopState(({ url }) => {
42+
callback(pathToSearchState(url))
43+
return true
44+
})
45+
},
46+
read: () => pathToSearchState(window.location.href),
47+
write(route) {
48+
if (disposed) return
49+
const url = searchStateToUrl(route)
50+
Router.push(url, url, { shallow: true })
51+
}
52+
}
53+
}
54+
}, [])
55+
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@
118118
"react-i18next": "^13.2.2",
119119
"react-inlinesvg": "^3.0.1",
120120
"react-instantsearch": "^7.12.4",
121-
"react-instantsearch-router-nextjs": "^7.15.5",
122121
"react-is": "^18.2.0",
123122
"react-markdown": "^8.0.4",
124123
"react-overlays": "^5.1.1",

yarn.lock

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5291,13 +5291,6 @@ [email protected]:
52915291
dependencies:
52925292
"@algolia/events" "^4.0.1"
52935293

5294-
5295-
version "3.24.3"
5296-
resolved "https://registry.yarnpkg.com/algoliasearch-helper/-/algoliasearch-helper-3.24.3.tgz#9a358c3110bcd912e79ef606a6e7bdd7725d22ee"
5297-
integrity sha512-3QKg5lzSfUiPN8Hn1ViHEGv6PjK7i4SFEDLzwlSzPO/4mVOsyos7B7/AsEtFQW5KHHPiCq6DyJl+mzg7CYlEgw==
5298-
dependencies:
5299-
"@algolia/events" "^4.0.1"
5300-
53015294
all-contributors-cli@^6.20.5:
53025295
version "6.26.1"
53035296
resolved "https://registry.yarnpkg.com/all-contributors-cli/-/all-contributors-cli-6.26.1.tgz#9f3358c9b9d0a7e66c8f84ffebf5a6432a859cae"
@@ -10408,13 +10401,6 @@ install-artifact-from-github@^1.3.5:
1040810401
resolved "https://registry.yarnpkg.com/install-artifact-from-github/-/install-artifact-from-github-1.3.5.tgz#88c96fe40e5eb21d45586d564208c648a1dbf38d"
1040910402
integrity sha512-gZHC7f/cJgXz7MXlHFBxPVMsvIbev1OQN1uKQYKVJDydGNm9oYf9JstbU4Atnh/eSvk41WtEovoRm+8IF686xg==
1041010403

10411-
10412-
version "0.11.1"
10413-
resolved "https://registry.yarnpkg.com/instantsearch-ui-components/-/instantsearch-ui-components-0.11.1.tgz#664ca03f657079946e459af72fa8d2674799c466"
10414-
integrity sha512-ZqUbJYYgObQ47J08ftXV1KNC1vdEoiD4/49qrkCdW46kRzLxLgYXJGuEuk48DQwK4aBtIoccgTyfbMGfcqNjxg==
10415-
dependencies:
10416-
"@babel/runtime" "^7.1.2"
10417-
1041810404
1041910405
version "0.9.0"
1042010406
resolved "https://registry.yarnpkg.com/instantsearch-ui-components/-/instantsearch-ui-components-0.9.0.tgz#f7ae71fe623d18eff32b73071749f31826cb7b89"
@@ -10445,24 +10431,6 @@ [email protected]:
1044510431
qs "^6.5.1 < 6.10"
1044610432
search-insights "^2.15.0"
1044710433

10448-
10449-
version "4.78.1"
10450-
resolved "https://registry.yarnpkg.com/instantsearch.js/-/instantsearch.js-4.78.1.tgz#cee799b920ba08c7c4e5af5ba591b86a1d80af1d"
10451-
integrity sha512-nDTWQ6DUxYzBZfkSxb/QJsYMZPPU8SGlGurn9147ABvA5Eumtxmk3Qy55EBMl0VxKVltGy3axAYMRB/gKIIHkg==
10452-
dependencies:
10453-
"@algolia/events" "^4.0.1"
10454-
"@types/dom-speech-recognition" "^0.0.1"
10455-
"@types/google.maps" "^3.55.12"
10456-
"@types/hogan.js" "^3.0.0"
10457-
"@types/qs" "^6.5.3"
10458-
algoliasearch-helper "3.24.3"
10459-
hogan.js "^3.0.2"
10460-
htm "^3.0.0"
10461-
instantsearch-ui-components "0.11.1"
10462-
preact "^10.10.0"
10463-
qs "^6.5.1 < 6.10"
10464-
search-insights "^2.17.2"
10465-
1046610434
instantsearch.js@^4.43.0:
1046710435
version "4.62.0"
1046810436
resolved "https://registry.yarnpkg.com/instantsearch.js/-/instantsearch.js-4.62.0.tgz#68577f4f04866728f22441cbc7464c544678d342"
@@ -14837,24 +14805,6 @@ [email protected]:
1483714805
instantsearch.js "4.74.2"
1483814806
use-sync-external-store "^1.0.0"
1483914807

14840-
14841-
version "7.15.5"
14842-
resolved "https://registry.yarnpkg.com/react-instantsearch-core/-/react-instantsearch-core-7.15.5.tgz#65d1edc440de8dc73d55230d13af8cbcf1724221"
14843-
integrity sha512-SFxiwwMf0f5F/8U0Y4ullvQ7bZtbYE516UOJbxaHhjV8yY0i8c22K4lrBFrYbxVRT7QAcp2wLGHiB7r/lD7eRA==
14844-
dependencies:
14845-
"@babel/runtime" "^7.1.2"
14846-
algoliasearch-helper "3.24.3"
14847-
instantsearch.js "4.78.1"
14848-
use-sync-external-store "^1.0.0"
14849-
14850-
react-instantsearch-router-nextjs@^7.15.5:
14851-
version "7.15.5"
14852-
resolved "https://registry.yarnpkg.com/react-instantsearch-router-nextjs/-/react-instantsearch-router-nextjs-7.15.5.tgz#a8b13bc5ad9bd8c5a689d48f2714eab6bed2514f"
14853-
integrity sha512-kn325Nl6QkZlkSuOXwKUOb56QkSsKes9XQdBLythKt2oZzzAfcaXSYzYsFEyj96cMYDLkRHhHvLhdyq4C8Xezg==
14854-
dependencies:
14855-
instantsearch.js "4.78.1"
14856-
react-instantsearch-core "7.15.5"
14857-
1485814808
react-instantsearch@^7.12.4:
1485914809
version "7.13.2"
1486014810
resolved "https://registry.yarnpkg.com/react-instantsearch/-/react-instantsearch-7.13.2.tgz#db84d04bd399596fb0078625bc75a6abc65e4bc6"
@@ -15657,11 +15607,6 @@ search-insights@^2.15.0:
1565715607
resolved "https://registry.yarnpkg.com/search-insights/-/search-insights-2.17.2.tgz#d13b2cabd44e15ade8f85f1c3b65c8c02138629a"
1565815608
integrity sha512-zFNpOpUO+tY2D85KrxJ+aqwnIfdEGi06UH2+xEb+Bp9Mwznmauqc9djbnBibJO5mpfUPPa8st6Sx65+vbeO45g==
1565915609

15660-
search-insights@^2.17.2:
15661-
version "2.17.3"
15662-
resolved "https://registry.yarnpkg.com/search-insights/-/search-insights-2.17.3.tgz#8faea5d20507bf348caba0724e5386862847b661"
15663-
integrity sha512-RQPdCYTa8A68uM2jwxoY842xDhvx3E5LFL1LxvxCNMev4o5mLuokczhzjAgGwUZBAmOKZknArSxLKmXtIi2AxQ==
15664-
1566515610
search-insights@^2.6.0:
1566615611
version "2.11.0"
1566715612
resolved "https://registry.yarnpkg.com/search-insights/-/search-insights-2.11.0.tgz#0512ae3b801fed5ff3a2ae82840bf20ba29d82e5"

0 commit comments

Comments
 (0)