From c50dcdd4c6d23e506924b2824f304399a217553b Mon Sep 17 00:00:00 2001 From: Enzo Vieira Date: Fri, 26 Jan 2024 15:13:16 +0000 Subject: [PATCH] add: feature flag middleware --- context/FeatureFlags/FeatureFlag.js | 11 --------- context/FeatureFlags/FeatureFlagsProvider.js | 24 -------------------- context/FeatureFlags/index.js | 3 --- context/FeatureFlags/useFeatureFlags.js | 4 ---- layout/Home/Home.tsx | 5 +--- middleware.ts | 20 ++++++++++++++++ pages/_app.tsx | 12 ++++------ 7 files changed, 25 insertions(+), 54 deletions(-) delete mode 100644 context/FeatureFlags/FeatureFlag.js delete mode 100644 context/FeatureFlags/FeatureFlagsProvider.js delete mode 100644 context/FeatureFlags/index.js delete mode 100644 context/FeatureFlags/useFeatureFlags.js create mode 100644 middleware.ts diff --git a/context/FeatureFlags/FeatureFlag.js b/context/FeatureFlags/FeatureFlag.js deleted file mode 100644 index d4cc5703..00000000 --- a/context/FeatureFlags/FeatureFlag.js +++ /dev/null @@ -1,11 +0,0 @@ -import { useFeatureFlags } from "./useFeatureFlags"; - -export const FeatureFlag = ({ feature, children }) => { - const { features } = useFeatureFlags(); - - if (features[feature]) { - return children; - } - - return null; -} diff --git a/context/FeatureFlags/FeatureFlagsProvider.js b/context/FeatureFlags/FeatureFlagsProvider.js deleted file mode 100644 index 1f67c650..00000000 --- a/context/FeatureFlags/FeatureFlagsProvider.js +++ /dev/null @@ -1,24 +0,0 @@ -import { createContext } from "react"; - -export const FeatureFlagsContext = createContext({}); - -export const FeatureFlagsProvider = ({ children }) => { - const features = fetchFeatures(); - - return ( - - {children} - - ); -}; - -const fetchFeatures = () => { - const features = {}; - for (const [key, value] of Object.entries(process.env)) { - if (key.endsWith("_FEATURE_FLAG")) { - const featureName = key.replace("_FEATURE_FLAG", "").replace("NEXT_PUBLIC_", ""); - features[featureName] = value === "true"; - } - } - return features; -} diff --git a/context/FeatureFlags/index.js b/context/FeatureFlags/index.js deleted file mode 100644 index a1716792..00000000 --- a/context/FeatureFlags/index.js +++ /dev/null @@ -1,3 +0,0 @@ -export { FeatureFlagsProvider } from './FeatureFlagsProvider'; -export { useFeatureFlags } from './useFeatureFlags'; -export { FeatureFlag } from './FeatureFlag'; diff --git a/context/FeatureFlags/useFeatureFlags.js b/context/FeatureFlags/useFeatureFlags.js deleted file mode 100644 index 241f65ff..00000000 --- a/context/FeatureFlags/useFeatureFlags.js +++ /dev/null @@ -1,4 +0,0 @@ -import { useContext } from 'react'; -import { FeatureFlagsContext } from './FeatureFlagsProvider'; - -export const useFeatureFlags = () => useContext(FeatureFlagsContext); diff --git a/layout/Home/Home.tsx b/layout/Home/Home.tsx index 40ab0dcb..bfa4c3e8 100644 --- a/layout/Home/Home.tsx +++ b/layout/Home/Home.tsx @@ -1,7 +1,6 @@ import { withoutAuth } from "@context/Auth"; import { Hero, Sponsors, Hackathon, Speakers, Partners } from "./components"; -import { FeatureFlag } from "@context/FeatureFlags/FeatureFlag"; import Navbar from "@components/Navbar"; import Schedule from "@components/Schedule"; @@ -11,9 +10,7 @@ function Home() { return ( {/* FIXME: The parameter could probably be better in some way */} - - - + diff --git a/middleware.ts b/middleware.ts new file mode 100644 index 00000000..bc1c1334 --- /dev/null +++ b/middleware.ts @@ -0,0 +1,20 @@ +import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; + +export function middleware(request: NextRequest) { + if (!process.env.NEXT_PUBLIC_BACKOFFICE_FEATURE_FLAG) { + return NextResponse.redirect(new URL("/404", request.url)); + } +} + +export const config = { + matcher: [ + "/attendee/:path*", + "/attendees/:path*", + "/badge/:path*", + "/product/:path*", + "/register/:path*", + "/sponsor/:path*", + "/staff/:path*", + ], +}; diff --git a/pages/_app.tsx b/pages/_app.tsx index 1bd2694f..271e7ab7 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -1,18 +1,14 @@ import { AuthProvider } from "@context/Auth"; import Header from "@components/Header"; -import { FeatureFlagsProvider } from "@context/FeatureFlags/FeatureFlagsProvider"; - import "../styles/globals.css"; function App({ Component, pageProps }) { return ( - - -
- - - + +
+ + ); }