diff --git a/surfsense_web/components/providers/runtime-config.server.tsx b/surfsense_web/components/providers/runtime-config.server.tsx new file mode 100644 index 000000000..bbe242286 --- /dev/null +++ b/surfsense_web/components/providers/runtime-config.server.tsx @@ -0,0 +1,15 @@ +import { connection } from "next/server"; +import { RuntimeConfigProvider } from "@/components/providers/runtime-config"; +import { AUTH_TYPE, DEPLOYMENT_MODE, ETL_SERVICE } from "@/lib/env-config"; + +export async function RuntimeConfig({ children }: { children: React.ReactNode }) { + await connection(); + + const value = { + authType: process.env.AUTH_TYPE ?? AUTH_TYPE, + etlService: process.env.ETL_SERVICE ?? ETL_SERVICE, + deploymentMode: process.env.DEPLOYMENT_MODE ?? DEPLOYMENT_MODE, + }; + + return {children}; +} diff --git a/surfsense_web/components/providers/runtime-config.tsx b/surfsense_web/components/providers/runtime-config.tsx new file mode 100644 index 000000000..560acd597 --- /dev/null +++ b/surfsense_web/components/providers/runtime-config.tsx @@ -0,0 +1,48 @@ +"use client"; + +import { createContext, useContext } from "react"; + +export type AuthType = "LOCAL" | "GOOGLE" | string; +export type DeploymentMode = "self-hosted" | "cloud" | string; + +export interface RuntimeConfigValue { + authType: AuthType; + etlService: string; + deploymentMode: DeploymentMode; +} + +const RuntimeConfigContext = createContext(null); + +export function RuntimeConfigProvider({ + value, + children, +}: { + value: RuntimeConfigValue; + children: React.ReactNode; +}) { + return {children}; +} + +export function useRuntimeConfig() { + const context = useContext(RuntimeConfigContext); + if (!context) { + throw new Error("useRuntimeConfig must be used within RuntimeConfigProvider"); + } + return context; +} + +export function useIsLocalAuth() { + return useRuntimeConfig().authType === "LOCAL"; +} + +export function useIsGoogleAuth() { + return useRuntimeConfig().authType === "GOOGLE"; +} + +export function useIsSelfHosted() { + return useRuntimeConfig().deploymentMode === "self-hosted"; +} + +export function useIsCloud() { + return useRuntimeConfig().deploymentMode === "cloud"; +}