hedgedoc/frontend/src/hooks/common/use-base-url.tsx
Tilman Vatteroth e390c0dd15 fix(frontend): reformat source files
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
2022-12-04 20:59:46 +01:00

32 lines
947 B
TypeScript

/*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { baseUrlContext } from '../../components/common/base-url/base-url-context-provider'
import { useRouter } from 'next/router'
import { useContext, useMemo } from 'react'
export enum ORIGIN {
EDITOR,
RENDERER,
CURRENT_PAGE
}
/**
* Provides the base urls for the editor and the renderer.
*/
export const useBaseUrl = (origin = ORIGIN.CURRENT_PAGE): string => {
const baseUrls = useContext(baseUrlContext)
if (!baseUrls) {
throw new Error('No base url context received. Did you forget to use the provider component?')
}
const router = useRouter()
return useMemo(() => {
return (router.route === '/render' && origin === ORIGIN.CURRENT_PAGE) || origin === ORIGIN.RENDERER
? baseUrls.renderer
: baseUrls.editor
}, [origin, baseUrls.renderer, baseUrls.editor, router.route])
}