mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-22 21:29:05 +00:00
fix typings and edge config key issue
This commit is contained in:
@@ -1,14 +1,8 @@
|
||||
"use client";
|
||||
import { createContext, useContext, useEffect, useState } from "react";
|
||||
import { DomainConfig } from "../lib/edgeConfig";
|
||||
|
||||
type DomainContextType = {
|
||||
features: {
|
||||
requireLogin: boolean;
|
||||
privacy: boolean;
|
||||
browse: boolean;
|
||||
};
|
||||
apiUrl: string | null;
|
||||
};
|
||||
type DomainContextType = Omit<DomainConfig, "auth_callback_url">;
|
||||
|
||||
export const DomainContext = createContext<DomainContextType>({
|
||||
features: {
|
||||
@@ -16,22 +10,22 @@ export const DomainContext = createContext<DomainContextType>({
|
||||
privacy: true,
|
||||
browse: false,
|
||||
},
|
||||
apiUrl: null,
|
||||
api_url: "",
|
||||
});
|
||||
|
||||
export const DomainContextProvider = ({ config, children }) => {
|
||||
export const DomainContextProvider = ({
|
||||
config,
|
||||
children,
|
||||
}: {
|
||||
config: DomainConfig;
|
||||
children: any;
|
||||
}) => {
|
||||
const [context, setContext] = useState<DomainContextType>();
|
||||
|
||||
useEffect(() => {
|
||||
if (!config) return;
|
||||
setContext({
|
||||
features: {
|
||||
requireLogin: !!config["features"]["requireLogin"],
|
||||
privacy: !!config["features"]["privacy"],
|
||||
browse: !!config["features"]["browse"],
|
||||
},
|
||||
apiUrl: config["api_url"],
|
||||
});
|
||||
const { auth_callback_url, ...others } = config;
|
||||
setContext(others);
|
||||
}, [config]);
|
||||
|
||||
if (!context) return;
|
||||
@@ -41,9 +35,12 @@ export const DomainContextProvider = ({ config, children }) => {
|
||||
);
|
||||
};
|
||||
|
||||
// Get feature config client-side with
|
||||
export const featureEnabled = (
|
||||
featureName: "requireLogin" | "privacy" | "browse",
|
||||
) => {
|
||||
const context = useContext(DomainContext);
|
||||
return context.features[featureName] as boolean | undefined;
|
||||
};
|
||||
|
||||
// Get config server-side (out of react) : see lib/edgeConfig.
|
||||
|
||||
@@ -11,6 +11,7 @@ import About from "../(aboutAndPrivacy)/about";
|
||||
import Privacy from "../(aboutAndPrivacy)/privacy";
|
||||
import { get } from "@vercel/edge-config";
|
||||
import { DomainContextProvider } from "./domainContext";
|
||||
import { getConfig } from "../lib/edgeConfig";
|
||||
|
||||
const poppins = Poppins({ subsets: ["latin"], weight: ["200", "400", "600"] });
|
||||
|
||||
@@ -68,10 +69,8 @@ type LayoutProps = {
|
||||
};
|
||||
|
||||
export default async function RootLayout({ children, params }: LayoutProps) {
|
||||
const config = await get(params.domain);
|
||||
const requireLogin = config ? config["features"]["requireLogin"] : false;
|
||||
const privacy = config ? config["features"]["privacy"] : true;
|
||||
const browse = config ? config["features"]["browse"] : true;
|
||||
const config = await getConfig(params.domain);
|
||||
const { requireLogin, privacy, browse } = config.features;
|
||||
|
||||
return (
|
||||
<html lang="en">
|
||||
|
||||
Reference in New Issue
Block a user