-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
66 additions
and
32 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
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 |
---|---|---|
@@ -1,6 +1,30 @@ | ||
import React from "react"; | ||
import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux"; | ||
import { AppDispatch, RootState } from "./store"; | ||
import { PrefContext } from "./preferences"; | ||
|
||
// Use throughout your app instead of plain `useDispatch` and `useSelector` | ||
export const useAppDispatch: () => AppDispatch = useDispatch; | ||
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector; | ||
|
||
/** | ||
* React Hook to prefix API routes with the base API URL | ||
* | ||
* @param {string} route - A route in the API | ||
* @return {string} | ||
* | ||
* @example | ||
* apiUrl = "/conda-store" | ||
* useApiUrl("api/v1/namespace") | ||
* "/conda-store/api/v1/namespace" | ||
*/ | ||
export const useApiUrl = (route: string): string => { | ||
const { apiUrl } = React.useContext(PrefContext); | ||
return ( | ||
// remove slash from end (if there is one) | ||
apiUrl.replace(/\/$/, "") + | ||
"/" + | ||
// remove slash from start (if there is one) | ||
route.replace(/^\//, "") | ||
); | ||
}; |
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