Skip to content

Commit

Permalink
Address nits and add comments to routing utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ckniffen committed Jul 7, 2023
1 parent 413827c commit 4685adb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/containers/Accounts/AccountAssetTab/AccountAssetTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ interface Props {
account: any
}

const assetTypes = ['issued', 'nft']

const AccountAssetTabDisconnected = ({ account }: Props) => {
const assetTypes = ['issued', 'nft']
const { id: accountId = '', assetType = assetTypes[0] } =
useRouteParams(ACCOUNT_ROUTE)
const navigate = useNavigate()
Expand Down
3 changes: 2 additions & 1 deletion src/containers/Header/NavigationMenu/NavigationMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const NavigationMenu = ({
<li className="nav-item nav-search">
<Search />
</li>
{routes.map((nav): any => {
{routes.map((nav) => {
const title = t(nav.title)

if ('children' in nav) {
Expand All @@ -98,6 +98,7 @@ export const NavigationMenu = ({
href={buildPath(child.route, {})}
data-title={title}
className="nav-link"
key={child.title}
>
{t(child.title)}
</DropdownItem>
Expand Down
54 changes: 41 additions & 13 deletions src/containers/shared/routing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@ import {
useParams as useRouterParams,
} from 'react-router-dom'

/**
* A definition for a react-router route that allows for typed routes
*
* @example
* export const ACCOUNT_ROUTE: RouteDefinition<{
* id?: string
* tab?: 'assets' | 'transactions'
* assetType?: 'issued' | 'nfts'
* }> = {
* path: '/accounts/:id?/:tab?/:assetType?',
* }
*/
export interface RouteDefinition<T = {}> {
path: string
sampleParams?: T
}

export interface LinkProps<
T extends RouteDefinition,
K extends T['sampleParams'] = T['sampleParams'],
> {
children?: ReactNode
to: T
params?: K
innerRef?: Ref<HTMLAnchorElement>
[key: string]: any
path: string // react-router style path with replacements ex. "/ledgers/:identifier"
sampleParams?: T // A phantom field used for typing the parameters on `NavigationLink` and `buildPath`.
}

/**
* Produce a link path. In `custom` network mode it will prepend the rippled entrypoint
* @param route
* @param params
*/
export function buildPath<T>(route: RouteDefinition<T>, params: T) {
const path =
(process.env.VITE_ENVIRONMENT === 'custom'
Expand All @@ -32,10 +38,32 @@ export function buildPath<T>(route: RouteDefinition<T>, params: T) {
)
}

/**
* A wrapper for `useRouterParams` that returns a typed object of the routes params
* @param route
*/
export function useRouteParams<T extends RouteDefinition>(route: T) {
return useRouterParams<NonNullable<(typeof route)['sampleParams']>>()
}

export interface LinkProps<
T extends RouteDefinition,
K extends T['sampleParams'] = T['sampleParams'],
> {
children?: ReactNode
to: T
params?: K
innerRef?: Ref<HTMLAnchorElement>
[key: string]: any
}

/**
* A wrapper for `Link` that provides a typed interface for params and uses `buildPath` to build the url/
* @param to
* @param children
* @param params
* @param rest All other parameters pass through
*/
export function ExplorerLink<T extends {} = {}>({
to,
children,
Expand Down

0 comments on commit 4685adb

Please sign in to comment.