j40-cejst-2/client/src/contexts/FlagContext.tsx
Vim 51f7666062
Adds dataset cards to Methodology page (#442)
* intial cards for methodology page

* PR and QA feedback
- adds alert above dataset section
- adds intl
- removes nbsp
- creates directory structure for new components

* revert noUsedLocals flag

* fixed path error

* re-creates scss file to test build failure

* renaming file to troubleshoot build error

* links open in new tabs and removes console.log

* removes units on all scss value that equal 0

* resolving merge conflicts from header merge

* updates snapshots from conflict resolution
2021-08-02 08:49:49 -07:00

66 lines
1.7 KiB
TypeScript

import * as React from 'react';
import * as queryString from 'query-string';
export type FlagContainer = { [key: string]: any };
/**
* FlagContext stores feature flags and passes them to consumers
*/
interface IFlagContext {
/**
* Contains a list of all currently-active flags
*/
flags: FlagContainer;
}
const FlagContext = React.createContext<IFlagContext>({flags: []});
/**
* `useFlags` returns all feature flags.
*
* @return {FlagContainer} flags All project feature flags
*/
const useFlags = () : FlagContainer => {
const {flags} = React.useContext(FlagContext);
return flags;
};
interface IURLFlagProviderProps {
children: React.ReactNode,
location: Location
}
/**
* `URLFlagProvider` is a provider for FlagContext.
* It is passed the current URL and parses the
* "flags" parameter, assumed to be a comma-separated
* list of currently-active flags.
* @param {URL} location : the current URL object
* @param {ReactNode} children : the children components
* @return {ReactNode} URLFlagProvider component
**/
const URLFlagProvider = ({children, location}: IURLFlagProviderProps) => {
const flagString = queryString.parse(location.search).flags;
const flags : FlagContainer = {};
let flagList: string[] = [];
if (flagString && typeof flagString === 'string') {
flagList = (flagString as string).split(',');
}
for (const flag of flagList) {
if (flag.includes('=')) {
const [key, value] = flag.split('=');
flags[key] = value;
} else {
flags[flag] = true;
}
}
return (
<FlagContext.Provider
value={{flags}}>
{children}
</FlagContext.Provider>
);
};
export {FlagContext, URLFlagProvider, useFlags};