- Added support for mock data mode in the frontend by introducing a new environment variable in the Dockerfile and workflow configuration. - Updated the settings page to conditionally display options based on the availability of mock data, improving user experience. - Refactored data source logic to include a check for mock data availability, ensuring proper mode handling in the application.
38 lines
1.3 KiB
TypeScript
38 lines
1.3 KiB
TypeScript
export const LOCAL_DEFAULT_BACKEND_URL = "http://localhost:8000"
|
|
|
|
export type ConfiguredBackendUrl =
|
|
| { kind: "local" }
|
|
| { kind: "fixed"; url: string }
|
|
| { kind: "same-origin" }
|
|
|
|
export function configuredBackendUrl(): ConfiguredBackendUrl {
|
|
const raw = process.env.NEXT_PUBLIC_BACKEND_URL
|
|
if (raw === undefined || raw === "local") return { kind: "local" }
|
|
if (raw === "" || raw === "same-origin") return { kind: "same-origin" }
|
|
return { kind: "fixed", url: raw.replace(/\/$/, "") }
|
|
}
|
|
|
|
export function defaultDataSourceMode(): "mock" | "live" {
|
|
if (!isMockDataSourceAvailable()) return "live"
|
|
return process.env.NEXT_PUBLIC_DEFAULT_DATA_SOURCE === "live" ? "live" : "mock"
|
|
}
|
|
|
|
export function isMockDataSourceAvailable(): boolean {
|
|
return process.env.NEXT_PUBLIC_ALLOW_MOCK_DATA !== "false"
|
|
}
|
|
|
|
export function isBackendUrlLocked(): boolean {
|
|
const cfg = configuredBackendUrl()
|
|
return cfg.kind === "same-origin" || cfg.kind === "fixed"
|
|
}
|
|
|
|
export function resolveStoredBackendUrl(stored: string | null): string {
|
|
const cfg = configuredBackendUrl()
|
|
if (cfg.kind === "fixed") return cfg.url
|
|
if (cfg.kind === "same-origin" && typeof window !== "undefined") {
|
|
return window.location.origin
|
|
}
|
|
const trimmed = stored?.trim().replace(/\/$/, "")
|
|
return trimmed || LOCAL_DEFAULT_BACKEND_URL
|
|
}
|