-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #337 from NYPL/SFR-1446_feature_flags
SFR-1446: Implement feature flags
- Loading branch information
Showing
8 changed files
with
210 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { render, RenderOptions } from "@testing-library/react"; | ||
import { ReactElement } from "react"; | ||
import { FeatureFlagProvider } from "~/src/context/FeatureFlagContext"; | ||
|
||
const customRender = ( | ||
ui: ReactElement, | ||
options?: Omit<RenderOptions, "wrapper"> | ||
) => { | ||
render(ui, { wrapper: FeatureFlagProvider, ...options }); | ||
}; | ||
|
||
export * from "@testing-library/react"; | ||
export { customRender as render }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { useRouter } from "next/router"; | ||
import { ParsedUrlQuery } from "querystring"; | ||
import React, { createContext, useContext, useEffect, useState } from "react"; | ||
import { extractQueryParam } from "../util/LinkUtils"; | ||
|
||
type FeatureFlag = { | ||
[flag: string]: boolean; | ||
}; | ||
|
||
type FeatureFlagContextType = { | ||
featureFlags: FeatureFlag; | ||
setFeatureFlags: (featureFlags: FeatureFlag) => void; | ||
isFlagActive: (flag: string) => boolean; | ||
}; | ||
|
||
export const FeatureFlagContext = | ||
createContext<FeatureFlagContextType>(undefined); | ||
|
||
const extractFeatureFlagParams = (query: ParsedUrlQuery) => { | ||
const featureFlags = {}; | ||
for (const param in query) { | ||
if (param.includes("feature_")) { | ||
const featureFlag = param.split("_")[1]; | ||
featureFlags[featureFlag] = JSON.parse(extractQueryParam(query, param)); | ||
} | ||
} | ||
return featureFlags; | ||
}; | ||
|
||
export const FeatureFlagProvider: React.FC = ({ children }) => { | ||
const [featureFlags, setFeatureFlags] = useState<FeatureFlag>({}); | ||
const isFlagActive = (flag: string) => { | ||
return featureFlags[flag]; | ||
}; | ||
|
||
const router = useRouter(); | ||
const { query } = router; | ||
|
||
useEffect(() => { | ||
const storedFeatureFlagsStr = sessionStorage.getItem("featureFlags"); | ||
let storedFeatureFlags: FeatureFlag = {}; | ||
if (storedFeatureFlagsStr) { | ||
try { | ||
storedFeatureFlags = JSON.parse(storedFeatureFlagsStr); | ||
for (const flag in storedFeatureFlags) { | ||
const featureFlag = "feature_" + flag; | ||
if (!query[featureFlag]) { | ||
router.push({ | ||
query: { ...query, [featureFlag]: storedFeatureFlags[flag] }, | ||
}); | ||
} | ||
} | ||
} catch (e) { | ||
throw new Error(e); | ||
} | ||
} | ||
|
||
const newFeatureFlags = extractFeatureFlagParams(query); | ||
sessionStorage.setItem("featureFlags", JSON.stringify(newFeatureFlags)); | ||
setFeatureFlags(newFeatureFlags); | ||
}, [query, router]); | ||
|
||
return ( | ||
<FeatureFlagContext.Provider | ||
value={{ | ||
featureFlags: featureFlags, | ||
setFeatureFlags: setFeatureFlags, | ||
isFlagActive, | ||
}} | ||
> | ||
{children} | ||
</FeatureFlagContext.Provider> | ||
); | ||
}; | ||
|
||
export const useFeatureFlags = () => { | ||
const context = useContext(FeatureFlagContext); | ||
if (typeof context === "undefined") { | ||
throw new Error( | ||
"useFeatureFlags must be used within a FeatureFlagProvider" | ||
); | ||
} | ||
return context; | ||
}; | ||
|
||
export default useFeatureFlags; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f79931f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
sfr-bookfinder-front-end – ./
sfr-bookfinder-front-end-nypl.vercel.app
sfr-bookfinder-front-end.vercel.app
sfr-bookfinder-front-end-git-development-nypl.vercel.app