Skip to content

Commit

Permalink
Simplify checking type for to
Browse files Browse the repository at this point in the history
  • Loading branch information
kkosiorowska committed Sep 4, 2024
1 parent 3d69111 commit a4c7ebe
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
7 changes: 2 additions & 5 deletions dapp/src/components/Header/Navigation/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react"
import { Box, BoxProps, HStack, List } from "@chakra-ui/react"
import { EXTERNAL_HREF } from "#/constants"
import { routerPath } from "#/router/path"
import { isString } from "#/utils"
import { router } from "#/utils"
import NavigationItem, { NavigationItemProps } from "./NavigationItem"

const NAVIGATION_ITEMS: NavigationItemProps[] = [
Expand All @@ -20,10 +20,7 @@ function Navigation(props: BoxProps) {
<Box as="nav" {...props}>
<HStack as={List} spacing={5} ml={12}>
{NAVIGATION_ITEMS.map((item) => (
<NavigationItem
key={isString(item.to) ? item.to : item.to.pathname}
{...item}
/>
<NavigationItem key={router.getURLPath(item.to)} {...item} />
))}
</HStack>
</Box>
Expand Down
4 changes: 2 additions & 2 deletions dapp/src/components/Header/Navigation/NavigationItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react"
import { ListItem, useMultiStyleConfig } from "@chakra-ui/react"
import { To, useSearchParams } from "react-router-dom"
import { useModal } from "#/hooks"
import { isString } from "#/utils"
import { router } from "#/utils"
import { NavLink, NavLinkProps } from "../../shared/NavLink"

export type NavigationItemProps = NavLinkProps
Expand All @@ -20,7 +20,7 @@ function NavigationItem(props: NavigationItemProps) {
const to: To = isExternal
? defaultTo
: {
pathname: isString(defaultTo) ? defaultTo : defaultTo.pathname,
pathname: router.getURLPath(defaultTo),
search: searchParams.toString(),
}

Expand Down
1 change: 1 addition & 0 deletions dapp/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export { default as orangeKit } from "./orangeKit"
export { default as userAgent } from "./userAgent"
export { default as referralProgram } from "./referralProgram"
export { default as mezoPortalAPI } from "./mezoPortalApi"
export { default as router } from "./router"
6 changes: 6 additions & 0 deletions dapp/src/utils/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { To } from "react-router-dom"
import { isString } from "./type-check"

const getURLPath = (to: To) => (isString(to) ? to : to.pathname)

export default { getURLPath }

0 comments on commit a4c7ebe

Please sign in to comment.