mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-31 23:28:34 -04:00
Refactor handling of environment variables (#2303)
* Refactor environment variables Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
e412115a78
commit
39a4125cb0
85 changed files with 624 additions and 461 deletions
33
src/hooks/common/use-base-url.tsx
Normal file
33
src/hooks/common/use-base-url.tsx
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { useContext, useMemo } from 'react'
|
||||
import { useRouter } from 'next/router'
|
||||
import { baseUrlContext } from '../../components/common/base-url/base-url-context-provider'
|
||||
|
||||
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])
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { useRouter } from 'next/router'
|
||||
import { useMemo } from 'react'
|
||||
|
||||
/**
|
||||
* Retrieves the frontend base url either from an environment variable or from the window location itself.
|
||||
*
|
||||
* @return The base url of the frontend.
|
||||
*/
|
||||
export const useFrontendBaseUrl = (): string => {
|
||||
const { asPath } = useRouter()
|
||||
return useMemo(() => {
|
||||
return process.env.NEXT_PUBLIC_FRONTEND_ASSETS_URL || window.location.toString().replace(asPath, '') + '/'
|
||||
}, [asPath])
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue