Path: blob/main/web/ui/src/contexts/PathPrefixContext.tsx
5304 views
import React from 'react';12/**3* PathPrefixContext propagates the base URL throughout the component tree where4* the application is hosted.5*/6const PathPrefixContext = React.createContext('');78/**9* usePathPrefix retrieves the current base URL where the application is10* hosted. Links and API calls should be all relative to this path. Returns11* `/` if there is no path prefix.12*13* The returned path prefix will always end in a `/`.14*/15function usePathPrefix(): string {16const prefix = React.useContext(PathPrefixContext);17if (prefix === '') {18return '/';19}2021if (prefix.endsWith('/')) {22return prefix;23}24return prefix + '/';25}2627export { PathPrefixContext, usePathPrefix };282930