Skip to content

Commit

Permalink
optimize useMakeLinkWithPreservedPath
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Sep 13, 2024
1 parent 561844b commit 2d4f46c
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 124 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-geese-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rescript-relay-router": patch
---

Optimize useMakeLinkWithPreservedPath

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,7 @@ let makeLinkFromQueryParams = (`->addToStr

`
@live
let useMakeLinkWithPreservedPath = () => {
let location = RelayRouter.Utils.useLocation()
React.useMemo(() => {
(makeNewQueryParams: queryParams => queryParams) => {
let newQueryParams = location.search->parseQueryParams->makeNewQueryParams
open RelayRouter.Bindings
let queryParams = location.search->QueryParams.parse
queryParams->applyQueryParams(~newParams=newQueryParams)
location.pathname ++ queryParams->QueryParams.toString
}
}, [location.search])
}
let useMakeLinkWithPreservedPath = (): ((queryParams => queryParams) => string) => RelayRouter__Internal.useMakeLinkWithPreservedPath(~parseQueryParams, ~applyQueryParams)
`->addToStr
}

Expand Down
35 changes: 33 additions & 2 deletions packages/rescript-relay-router/src/RelayRouter__Internal.res
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ type setQueryParamsFn<'queryParams> = (
~shallow: bool=?,
) => unit

type parseQueryParamsFn<'queryParams> = string => 'queryParams
type applyQueryParamsFn<'queryParams> = (
RelayRouter__Bindings.QueryParams.t,
~newParams: 'queryParams,
) => unit

let useSetQueryParams = (
~parseQueryParams: string => 'queryParams,
~applyQueryParams: (RelayRouter__Bindings.QueryParams.t, ~newParams: 'queryParams) => unit,
~parseQueryParams: parseQueryParamsFn<'queryParams>,
~applyQueryParams: applyQueryParamsFn<'queryParams>,
): setQueryParamsFn<'queryParams> => {
let router = RelayRouter__Context.useRouterContext()

Expand Down Expand Up @@ -75,6 +81,31 @@ let useSetQueryParams = (
}, (parseQueryParams, applyQueryParams, router))
}

type makeNewQueryParamsMakerFn<'queryParams> = 'queryParams => 'queryParams

type makeNewQueryParamsFn<'queryParams> = makeNewQueryParamsMakerFn<'queryParams> => string

let useMakeLinkWithPreservedPath = (
~parseQueryParams: parseQueryParamsFn<'queryParams>,
~applyQueryParams: applyQueryParamsFn<'queryParams>,
): makeNewQueryParamsFn<'queryParams> => {
let router = RelayRouter__Context.useRouterContext()
React.useMemo3(() => {
(makeNewQueryParams: 'queryParams => 'queryParams) => {
let location = router.history->RelayRouter__History.getLocation
let newQueryParams =
location.search
->parseQueryParams
->makeNewQueryParams

open RelayRouter__Bindings
let queryParams = location.search->QueryParams.parse
queryParams->applyQueryParams(~newParams=newQueryParams)
location.pathname ++ queryParams->QueryParams.toString
}
}, (router, parseQueryParams, applyQueryParams))
}

type pathMatch = {params: Js.Dict.t<string>}

@module("./vendor/react-router.js") @return(nullable)
Expand Down
19 changes: 17 additions & 2 deletions packages/rescript-relay-router/src/RelayRouter__Internal.resi
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,26 @@ type setQueryParamsFn<'queryParams> = (
~shallow: bool=?,
) => unit

type parseQueryParamsFn<'queryParams> = string => 'queryParams
type applyQueryParamsFn<'queryParams> = (
RelayRouter__Bindings.QueryParams.t,
~newParams: 'queryParams,
) => unit

let useSetQueryParams: (
~parseQueryParams: string => 'queryParams,
~applyQueryParams: (RelayRouter__Bindings.QueryParams.t, ~newParams: 'queryParams) => unit,
~parseQueryParams: parseQueryParamsFn<'queryParams>,
~applyQueryParams: applyQueryParamsFn<'queryParams>,
) => setQueryParamsFn<'queryParams>

type makeNewQueryParamsMakerFn<'queryParams> = 'queryParams => 'queryParams

type makeNewQueryParamsFn<'queryParams> = makeNewQueryParamsMakerFn<'queryParams> => string

let useMakeLinkWithPreservedPath: (
~parseQueryParams: parseQueryParamsFn<'queryParams>,
~applyQueryParams: applyQueryParamsFn<'queryParams>,
) => makeNewQueryParamsFn<'queryParams>

type pathMatch = {params: Js.Dict.t<string>}

@module("./vendor/react-router.js") @return(nullable)
Expand Down

0 comments on commit 2d4f46c

Please sign in to comment.