mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-06-18 21:15:16 +02:00
feat(web): add runtime config provider for server-injected flags
This commit is contained in:
parent
6b31997599
commit
5613908d0d
2 changed files with 63 additions and 0 deletions
15
surfsense_web/components/providers/runtime-config.server.tsx
Normal file
15
surfsense_web/components/providers/runtime-config.server.tsx
Normal file
|
|
@ -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 <RuntimeConfigProvider value={value}>{children}</RuntimeConfigProvider>;
|
||||
}
|
||||
48
surfsense_web/components/providers/runtime-config.tsx
Normal file
48
surfsense_web/components/providers/runtime-config.tsx
Normal file
|
|
@ -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<RuntimeConfigValue | null>(null);
|
||||
|
||||
export function RuntimeConfigProvider({
|
||||
value,
|
||||
children,
|
||||
}: {
|
||||
value: RuntimeConfigValue;
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
return <RuntimeConfigContext.Provider value={value}>{children}</RuntimeConfigContext.Provider>;
|
||||
}
|
||||
|
||||
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";
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue