Skip to content

Commit

Permalink
fix(navigation): home should not clear search
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Feb 27, 2024
1 parent da7fffa commit dad36d1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
21 changes: 8 additions & 13 deletions src/context/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,25 @@ import { decode, encode, type Navigation } from './encodeNavigation.js'
import type { SearchTerm } from './Search.js'
import { LwM2MObjectID, models } from '@hello.nrfcloud.com/proto-lwm2m'

const Home: Navigation = { panel: 'home' }
const Home: Navigation = { panel: 'world', resources: [], search: [] }

export const NavigationProvider = (props: ParentProps) => {
const [location, setLocation] = createSignal<Navigation>(
const [location, setLocation] = createSignal<Required<Navigation>>(
decode(window.location.hash.slice(1)) ?? Home,
)

const locationHandler = () =>
setLocation(decode(window.location.hash.slice(1)) ?? Home)
window.addEventListener('hashchange', locationHandler)

onCleanup(() => window.removeEventListener('hashchange', locationHandler))
const navigate = (next: Navigation) => {
const navigate = (next: Partial<Navigation>) => {
window.location.hash =
encode({
...location(),
...next,
}) ?? ''
}
const link = (next: Navigation) =>
const link = (next: Partial<Navigation>) =>
new URL(
`${BASE_URL}/#${encode({
...location(),
Expand All @@ -37,7 +36,7 @@ export const NavigationProvider = (props: ParentProps) => {
document.location.href,
).toString()

const linkToHome = () => link(Home)
const linkToHome = () => link({ panel: 'world' })

const hasResource = (resource: Resource) =>
(location().resources ?? []).find(
Expand All @@ -47,11 +46,7 @@ export const NavigationProvider = (props: ParentProps) => {
return (
<NavigationContext.Provider
value={{
current: () => ({
panel: location().panel,
search: location().search ?? [],
resources: location().resources ?? [],
}),
current: location,
navigate,
navigateHome: () => navigate(Home),
linkToHome,
Expand Down Expand Up @@ -117,10 +112,10 @@ const resourceToString = ({ ObjectID, ResourceID }: Resource): string =>

export const NavigationContext = createContext<{
current: Accessor<Required<Navigation>>
navigate: (next: Navigation) => void
navigate: (next: Partial<Navigation>) => void
navigateHome: () => void
linkToHome: () => string
link: (next: Navigation) => string
link: (next: Partial<Navigation>) => string
linkWithoutSearchTerm: (term: SearchTerm) => string
linkToSearch: (term: SearchTerm) => string
navigateWithSearchTerm: (term: SearchTerm) => void
Expand Down
4 changes: 2 additions & 2 deletions src/context/encodeNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { isLwM2MObjectID } from '../util/lwm2m.js'

export type Navigation = {
panel: string
search?: SearchTerm[]
resources?: Resource[]
search: SearchTerm[]
resources: Resource[]
}

enum FieldKey {
Expand Down

0 comments on commit dad36d1

Please sign in to comment.