Skip to content

Commit

Permalink
Add ember nav
Browse files Browse the repository at this point in the history
  • Loading branch information
jho406 committed Feb 11, 2025
1 parent 3ef5a94 commit d62b4fa
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 15 deletions.
32 changes: 18 additions & 14 deletions superglue/lib/components/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import React, {
useImperativeHandle,
ForwardedRef,
} from 'react'
import { urlToPageKey, pathWithoutBZParams } from '../utils'
import { removePage, setActivePage } from '../actions'
import { urlToPageKey, pathWithoutBZParams, mergeQuery } from '../utils'
import { removePage, setActivePage, copyPage } from '../actions'
import {
HistoryState,
RootState,
Expand Down Expand Up @@ -164,26 +164,30 @@ const NavigationProvider = forwardRef(function NavigationProvider(
}
}

const navigateTo: NavigateTo = (
path,
{ action } = {
action: 'push',
}
) => {
const navigateTo: NavigateTo = (path, { action, search } = {}) => {
action ||= 'push'
search ||= {}

if (action === 'none') {
return false
}

path = pathWithoutBZParams(path)
const nextPageKey = urlToPageKey(path)
const hasPage = Object.prototype.hasOwnProperty.call(
store.getState().pages,
nextPageKey
)
let nextPageKey = urlToPageKey(path)
// store is untyped?
const page = store.getState().pages[nextPageKey]

if (page) {
if (Object.keys(search).length > 0) {
const originalKey = nextPageKey
nextPageKey = mergeQuery(nextPageKey, search)
dispatch(copyPage({ from: originalKey, to: nextPageKey }))
path = mergeQuery(path, search)
}

if (hasPage) {
const location = history.location
const state = location.state as HistoryState

const historyArgs = [
path,
{
Expand Down
3 changes: 2 additions & 1 deletion superglue/lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,8 @@ export interface BasicRequestInit extends RequestInit {
export type NavigateTo = (
path: Keypath,
options: {
action: NavigationAction
action?: NavigationAction
search?: Record<string, string>
}
) => boolean

Expand Down
10 changes: 10 additions & 0 deletions superglue/lib/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,13 @@ export function parsePageKey(pageKey: PageKey) {
search: query,
}
}

export function mergeQuery(pageKey: PageKey, search: Record<string, string>) {
const parsed = new parse(pageKey, {}, true)

Object.keys(search).forEach((key) => {
parsed.query[key] = search[key]
})

return parsed.toString()
}
54 changes: 54 additions & 0 deletions superglue/spec/lib/NavComponent.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,60 @@ describe('Nav', () => {

expect(instance.navigateTo('/blah', { action: 'none' })).toEqual(false)
})

it('navigates optimistically to a copied page with new params', () => {
const history = createMemoryHistory({})
history.replace('/home', {
superglue: true,
pageKey: '/home',
posX: 5,
posY: 5,
})

const store = buildStore({
pages: {
'/home': {
componentIdentifier: 'home',
restoreStrategy: 'fromCacheOnly',
},
'/about': {
componentIdentifier: 'about',
restoreStrategy: 'fromCacheOnly',
},
},
superglue: {
csrfToken: 'abc',
currentPageKey: '/home',
},
})

let instance

render(
<Provider store={store}>
<NavigationProvider
store={store}
ref={(node) => (instance = node)}
mapping={{ home: Home, about: About }}
history={history}
/>
</Provider>
)

instance.navigateTo('/home', { search: {hello: "world" }})

const pages = store.getState().pages
expect(pages['/home?hello=world']).toMatchObject(
pages['/home']
)
expect(pages['/home?hello=world']).not.toBe(
pages['/home']
)

expect(store.getState().superglue.currentPageKey).toEqual("/home?hello=world")
expect(history.location.pathname).toEqual('/home')
expect(history.location.search).toEqual('?hello=world')
})
})

describe('history pop', () => {
Expand Down

0 comments on commit d62b4fa

Please sign in to comment.