Skip to content

Commit 65e51d6

Browse files
authored
Revert 1898 adding state on u mount (#1928)
* Revert "Revert "[1753] Replace custom routing with react-instantsearch-router-nextjs"" This reverts commit 8a85105. * Adding preserveSharedStateOnUnmount * Merge with main and sloved conflict
1 parent 6e75996 commit 65e51d6

File tree

6 files changed

+119
-60
lines changed

6 files changed

+119
-60
lines changed

components/search/bills/BillSearch.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@ import {
77
SearchBox,
88
useInstantSearch
99
} from "react-instantsearch"
10-
import { currentGeneralCourt } from "functions/src/shared"
10+
import { createInstantSearchRouterNext } from "react-instantsearch-router-nextjs"
11+
import singletonRouter from "next/router"
1112
import styled from "styled-components"
1213
import TypesenseInstantSearchAdapter from "typesense-instantsearch-adapter"
14+
import { currentGeneralCourt } from "functions/src/shared"
1315
import { Col, Container, Row, Spinner } from "../../bootstrap"
1416
import { NoResults } from "../NoResults"
1517
import { ResultCount } from "../ResultCount"
1618
import { SearchContainer } from "../SearchContainer"
1719
import { SearchErrorBoundary } from "../SearchErrorBoundary"
18-
import { useRouting } from "../useRouting"
1920
import { BillHit } from "./BillHit"
2021
import { useBillRefinements } from "./useBillRefinements"
2122
import { SortBy, SortByWithConfigurationItem } from "../SortBy"
2223
import { getServerConfig, VirtualFilters } from "../common"
2324
import { useBillSort } from "./useBillSort"
2425
import { FC, useState } from "react"
26+
import { pathToSearchState, searchStateToUrl } from "../routingHelpers"
2527

2628
const searchClient = new TypesenseInstantSearchAdapter({
2729
server: getServerConfig(),
@@ -70,7 +72,16 @@ export const BillSearch = () => {
7072
}
7173
}}
7274
searchClient={searchClient}
73-
routing={useRouting()}
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+
}}
7485
future={{ preserveSharedStateOnUnmount: true }}
7586
>
7687
<VirtualFilters type="bill" />
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { UiState } from "instantsearch.js"
2+
import QueryString from "qs"
3+
4+
export const searchStateToUrl = (createUrlArgs: {
5+
location: Location
6+
qsModule: typeof QueryString
7+
routeState: UiState
8+
}) => {
9+
const { location, qsModule: qs, routeState: searchState } = createUrlArgs
10+
const base = location.origin + location.pathname
11+
12+
const flagQueries = Object.fromEntries(
13+
Object.entries(qs.parse(window.location.search.slice(1))).filter(
14+
([key]) => !Object.keys(searchState).includes(key)
15+
)
16+
)
17+
18+
const query = qs.stringify({
19+
...searchState,
20+
...flagQueries
21+
})
22+
23+
return query ? `${base}?${query}` : base
24+
}
25+
26+
export const pathToSearchState = (parseUrlArgs: {
27+
location: Location
28+
qsModule: typeof QueryString
29+
}) => {
30+
const { location, qsModule: qs } = parseUrlArgs
31+
const path = location.href
32+
33+
return (
34+
path.includes("?") ? qs.parse(path.substring(path.indexOf("?") + 1)) : {}
35+
) as UiState
36+
}

components/search/testimony/TestimonySearch.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
SearchBox,
77
useInstantSearch
88
} from "react-instantsearch"
9+
import { createInstantSearchRouterNext } from "react-instantsearch-router-nextjs"
10+
import singletonRouter from "next/router"
911
import {
1012
StyledTabContent,
1113
StyledTabNav
@@ -23,10 +25,10 @@ import { SearchContainer } from "../SearchContainer"
2325
import { SearchErrorBoundary } from "../SearchErrorBoundary"
2426
import { SortBy } from "../SortBy"
2527
import { getServerConfig, VirtualFilters } from "../common"
26-
import { useRouting } from "../useRouting"
2728
import { TestimonyHit } from "./TestimonyHit"
2829
import { useTestimonyRefinements } from "./useTestimonyRefinements"
2930
import { FollowContext, OrgFollowStatus } from "components/shared/FollowContext"
31+
import { pathToSearchState, searchStateToUrl } from "../routingHelpers"
3032
import { useTranslation } from "next-i18next"
3133

3234
const searchClient = new TypesenseInstantSearchAdapter({
@@ -71,7 +73,16 @@ export const TestimonySearch = () => {
7173
}
7274
}}
7375
searchClient={searchClient}
74-
routing={useRouting()}
76+
routing={{
77+
router: createInstantSearchRouterNext({
78+
singletonRouter,
79+
routerOptions: {
80+
cleanUrlOnDispose: false,
81+
createURL: args => searchStateToUrl(args),
82+
parseURL: args => pathToSearchState(args)
83+
}
84+
})
85+
}}
7586
future={{ preserveSharedStateOnUnmount: true }}
7687
>
7788
<VirtualFilters type="testimony" />

components/search/useRouting.tsx

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
"react-i18next": "^13.2.2",
120120
"react-inlinesvg": "^3.0.1",
121121
"react-instantsearch": "^7.12.4",
122+
"react-instantsearch-router-nextjs": "^7.15.5",
122123
"react-is": "^18.2.0",
123124
"react-markdown": "^8.0.4",
124125
"react-overlays": "^5.1.1",

yarn.lock

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5369,6 +5369,13 @@ algoliasearch-helper@3.22.5:
53695369
dependencies:
53705370
"@algolia/events" "^4.0.1"
53715371

5372+
algoliasearch-helper@3.24.3:
5373+
version "3.24.3"
5374+
resolved "https://registry.yarnpkg.com/algoliasearch-helper/-/algoliasearch-helper-3.24.3.tgz#9a358c3110bcd912e79ef606a6e7bdd7725d22ee"
5375+
integrity sha512-3QKg5lzSfUiPN8Hn1ViHEGv6PjK7i4SFEDLzwlSzPO/4mVOsyos7B7/AsEtFQW5KHHPiCq6DyJl+mzg7CYlEgw==
5376+
dependencies:
5377+
"@algolia/events" "^4.0.1"
5378+
53725379
all-contributors-cli@^6.20.5:
53735380
version "6.26.1"
53745381
resolved "https://registry.yarnpkg.com/all-contributors-cli/-/all-contributors-cli-6.26.1.tgz#9f3358c9b9d0a7e66c8f84ffebf5a6432a859cae"
@@ -10535,6 +10542,13 @@ install-artifact-from-github@^1.3.5:
1053510542
resolved "https://registry.yarnpkg.com/install-artifact-from-github/-/install-artifact-from-github-1.3.5.tgz#88c96fe40e5eb21d45586d564208c648a1dbf38d"
1053610543
integrity sha512-gZHC7f/cJgXz7MXlHFBxPVMsvIbev1OQN1uKQYKVJDydGNm9oYf9JstbU4Atnh/eSvk41WtEovoRm+8IF686xg==
1053710544

10545+
instantsearch-ui-components@0.11.1:
10546+
version "0.11.1"
10547+
resolved "https://registry.yarnpkg.com/instantsearch-ui-components/-/instantsearch-ui-components-0.11.1.tgz#664ca03f657079946e459af72fa8d2674799c466"
10548+
integrity sha512-ZqUbJYYgObQ47J08ftXV1KNC1vdEoiD4/49qrkCdW46kRzLxLgYXJGuEuk48DQwK4aBtIoccgTyfbMGfcqNjxg==
10549+
dependencies:
10550+
"@babel/runtime" "^7.1.2"
10551+
1053810552
instantsearch-ui-components@0.9.0:
1053910553
version "0.9.0"
1054010554
resolved "https://registry.yarnpkg.com/instantsearch-ui-components/-/instantsearch-ui-components-0.9.0.tgz#f7ae71fe623d18eff32b73071749f31826cb7b89"
@@ -10565,6 +10579,24 @@ instantsearch.js@4.74.2:
1056510579
qs "^6.5.1 < 6.10"
1056610580
search-insights "^2.15.0"
1056710581

10582+
instantsearch.js@4.78.1:
10583+
version "4.78.1"
10584+
resolved "https://registry.yarnpkg.com/instantsearch.js/-/instantsearch.js-4.78.1.tgz#cee799b920ba08c7c4e5af5ba591b86a1d80af1d"
10585+
integrity sha512-nDTWQ6DUxYzBZfkSxb/QJsYMZPPU8SGlGurn9147ABvA5Eumtxmk3Qy55EBMl0VxKVltGy3axAYMRB/gKIIHkg==
10586+
dependencies:
10587+
"@algolia/events" "^4.0.1"
10588+
"@types/dom-speech-recognition" "^0.0.1"
10589+
"@types/google.maps" "^3.55.12"
10590+
"@types/hogan.js" "^3.0.0"
10591+
"@types/qs" "^6.5.3"
10592+
algoliasearch-helper "3.24.3"
10593+
hogan.js "^3.0.2"
10594+
htm "^3.0.0"
10595+
instantsearch-ui-components "0.11.1"
10596+
preact "^10.10.0"
10597+
qs "^6.5.1 < 6.10"
10598+
search-insights "^2.17.2"
10599+
1056810600
instantsearch.js@^4.43.0:
1056910601
version "4.62.0"
1057010602
resolved "https://registry.yarnpkg.com/instantsearch.js/-/instantsearch.js-4.62.0.tgz#68577f4f04866728f22441cbc7464c544678d342"
@@ -15051,6 +15083,24 @@ react-instantsearch-core@7.13.2:
1505115083
instantsearch.js "4.74.2"
1505215084
use-sync-external-store "^1.0.0"
1505315085

15086+
react-instantsearch-core@7.15.5:
15087+
version "7.15.5"
15088+
resolved "https://registry.yarnpkg.com/react-instantsearch-core/-/react-instantsearch-core-7.15.5.tgz#65d1edc440de8dc73d55230d13af8cbcf1724221"
15089+
integrity sha512-SFxiwwMf0f5F/8U0Y4ullvQ7bZtbYE516UOJbxaHhjV8yY0i8c22K4lrBFrYbxVRT7QAcp2wLGHiB7r/lD7eRA==
15090+
dependencies:
15091+
"@babel/runtime" "^7.1.2"
15092+
algoliasearch-helper "3.24.3"
15093+
instantsearch.js "4.78.1"
15094+
use-sync-external-store "^1.0.0"
15095+
15096+
react-instantsearch-router-nextjs@^7.15.5:
15097+
version "7.15.5"
15098+
resolved "https://registry.yarnpkg.com/react-instantsearch-router-nextjs/-/react-instantsearch-router-nextjs-7.15.5.tgz#a8b13bc5ad9bd8c5a689d48f2714eab6bed2514f"
15099+
integrity sha512-kn325Nl6QkZlkSuOXwKUOb56QkSsKes9XQdBLythKt2oZzzAfcaXSYzYsFEyj96cMYDLkRHhHvLhdyq4C8Xezg==
15100+
dependencies:
15101+
instantsearch.js "4.78.1"
15102+
react-instantsearch-core "7.15.5"
15103+
1505415104
react-instantsearch@^7.12.4:
1505515105
version "7.13.2"
1505615106
resolved "https://registry.yarnpkg.com/react-instantsearch/-/react-instantsearch-7.13.2.tgz#db84d04bd399596fb0078625bc75a6abc65e4bc6"
@@ -15869,6 +15919,11 @@ search-insights@^2.15.0:
1586915919
resolved "https://registry.yarnpkg.com/search-insights/-/search-insights-2.17.2.tgz#d13b2cabd44e15ade8f85f1c3b65c8c02138629a"
1587015920
integrity sha512-zFNpOpUO+tY2D85KrxJ+aqwnIfdEGi06UH2+xEb+Bp9Mwznmauqc9djbnBibJO5mpfUPPa8st6Sx65+vbeO45g==
1587115921

15922+
search-insights@^2.17.2:
15923+
version "2.17.3"
15924+
resolved "https://registry.yarnpkg.com/search-insights/-/search-insights-2.17.3.tgz#8faea5d20507bf348caba0724e5386862847b661"
15925+
integrity sha512-RQPdCYTa8A68uM2jwxoY842xDhvx3E5LFL1LxvxCNMev4o5mLuokczhzjAgGwUZBAmOKZknArSxLKmXtIi2AxQ==
15926+
1587215927
search-insights@^2.6.0:
1587315928
version "2.11.0"
1587415929
resolved "https://registry.yarnpkg.com/search-insights/-/search-insights-2.11.0.tgz#0512ae3b801fed5ff3a2ae82840bf20ba29d82e5"

0 commit comments

Comments
 (0)