Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/web/ui/src/contexts/PathPrefixContext.tsx
5304 views
1
import React from 'react';
2
3
/**
4
* PathPrefixContext propagates the base URL throughout the component tree where
5
* the application is hosted.
6
*/
7
const PathPrefixContext = React.createContext('');
8
9
/**
10
* usePathPrefix retrieves the current base URL where the application is
11
* hosted. Links and API calls should be all relative to this path. Returns
12
* `/` if there is no path prefix.
13
*
14
* The returned path prefix will always end in a `/`.
15
*/
16
function usePathPrefix(): string {
17
const prefix = React.useContext(PathPrefixContext);
18
if (prefix === '') {
19
return '/';
20
}
21
22
if (prefix.endsWith('/')) {
23
return prefix;
24
}
25
return prefix + '/';
26
}
27
28
export { PathPrefixContext, usePathPrefix };
29
30