-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor filter ULR implementation along with app.tsx
- Loading branch information
Showing
7 changed files
with
175 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { MEZZO_API_PATH } from '@caribou-crew/mezzo-constants'; | ||
import { RouteItemType, VariantCategory } from '@caribou-crew/mezzo-interfaces'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
export interface URLHashCallback { | ||
routes: RouteItemType[]; | ||
initialText: string; | ||
} | ||
|
||
/** | ||
* Fetches array of routes via api | ||
* @returns | ||
*/ | ||
export default function useFetchRoutes() { | ||
const [routes, setRoutes] = useState<RouteItemType[]>([]); | ||
const [version, setVersion] = useState<string>(''); | ||
const [variantCategories, setVariantCategories] = useState<VariantCategory[]>( | ||
[] | ||
); | ||
|
||
useEffect(() => { | ||
const fetchData = async () => { | ||
const response = await fetch(`${MEZZO_API_PATH}/routes`); | ||
const data = await response.json(); | ||
setRoutes(data.routes); | ||
setVersion(data.appVersion); | ||
setVariantCategories( | ||
(data.variantCategories || []).sort( | ||
(a: VariantCategory, b: VariantCategory) => | ||
(a?.order ?? 0) - (b?.order ?? 0) | ||
) | ||
); | ||
}; | ||
|
||
fetchData().catch(console.error); | ||
}, []); | ||
|
||
return { routes, version, variantCategories }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { RouteItemType } from '@caribou-crew/mezzo-interfaces'; | ||
import { useEffect } from 'react'; | ||
|
||
export interface URLHashCallback { | ||
routes: RouteItemType[]; | ||
initialText: string; | ||
} | ||
|
||
export type SetFilter = (arg0: string) => void; | ||
const filterPrefix = '#label'; | ||
|
||
/** | ||
* If #label hash is set in URL on load, then set that value as the initial filter | ||
* @param setFilter | ||
*/ | ||
export default function useFilterFromURL(setFilter: SetFilter) { | ||
useEffect(() => { | ||
const hash = getURLHash(); | ||
if (hash.key === filterPrefix) { | ||
setFilter(hash.value); | ||
} | ||
}, [setFilter]); | ||
} | ||
|
||
export const getURLHash = () => { | ||
const [key, hashValue] = window.location.hash.substring(1).split('/'); | ||
return { key, value: decodeURIComponent(hashValue) }; | ||
}; | ||
|
||
export const setURLHash = (value: string) => { | ||
if (value.length > 0) { | ||
window.history.replaceState( | ||
null, | ||
'', | ||
`${filterPrefix}/${encodeURIComponent(value)}` | ||
); | ||
} else { | ||
window.history.replaceState(null, '', ''); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { RouteItemType } from '@caribou-crew/mezzo-interfaces'; | ||
import { useState } from 'react'; | ||
|
||
export interface URLHashCallback { | ||
routes: RouteItemType[]; | ||
initialText: string; | ||
} | ||
|
||
/** | ||
* Filters array of routes based on the filterValue | ||
* @param routes | ||
* @returns | ||
*/ | ||
export default function useRouteFilter(routes: RouteItemType[]) { | ||
const [filterValue, setFilterValue] = useState(''); | ||
|
||
return { | ||
routes: getFilteredRoutes(routes, filterValue), | ||
filterValue, | ||
setFilterValue, | ||
}; | ||
} | ||
|
||
const getFilteredRoutes = (routes: RouteItemType[], value: string) => { | ||
const filteredRoutes = routes.filter((route) => { | ||
const searchLower = value?.toLowerCase(); | ||
return ( | ||
route?.id?.toLowerCase()?.includes(searchLower) || | ||
route?.label?.toLowerCase()?.includes(searchLower) || | ||
route?.method?.toLowerCase()?.includes(searchLower) || | ||
route?.path?.toString()?.toLowerCase()?.includes(searchLower) | ||
); | ||
}); | ||
return filteredRoutes; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { RouteItemType } from '@caribou-crew/mezzo-interfaces'; | ||
import { useState } from 'react'; | ||
|
||
export interface URLHashCallback { | ||
routes: RouteItemType[]; | ||
initialText: string; | ||
} | ||
|
||
/** | ||
* Sorts routes based on selected property and direction | ||
* @param routes | ||
* @returns | ||
*/ | ||
export default function useRouteSort(routes: RouteItemType[]) { | ||
const sortAsc = 1; | ||
const [sortedByProperty, setSortedBy] = useState(''); | ||
const [sortDirection, setSortDirection] = useState(sortAsc); | ||
|
||
const setSort = (property: string) => { | ||
let mySortDirection = sortDirection; | ||
if (sortedByProperty === property) { | ||
mySortDirection *= -1; | ||
setSortDirection(mySortDirection); | ||
} else { | ||
setSortedBy(property); | ||
} | ||
}; | ||
|
||
const outRoutes = [...routes].sort((a: any, b: any) => | ||
a?.[sortedByProperty] < b?.[sortedByProperty] | ||
? -1 * sortDirection | ||
: sortDirection | ||
); | ||
return { routes: outRoutes, setSort, sortDirection, sortedByProperty }; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.