-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable features from backend feature flags (#8512)
- Loading branch information
1 parent
ed31b71
commit cf8f87b
Showing
9 changed files
with
102 additions
and
14 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
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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import Sentry from "./Sentry"; | ||
import Plausible from "./Plausible"; | ||
|
||
const Intergrations = { Sentry, Plausible }; | ||
const Integrations = { Sentry, Plausible }; | ||
|
||
export default Intergrations; | ||
export default Integrations; |
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,78 @@ | ||
import { createContext, useContext, useState, useEffect } from "react"; | ||
import useQuery from "./request/useQuery"; | ||
import routes from "../Redux/api"; | ||
import useAuthUser from "../Common/hooks/useAuthUser"; | ||
import { FacilityModel } from "../Components/Facility/models"; | ||
|
||
export type FeatureFlag = "SCRIBE_ENABLED"; // "HCX_ENABLED" | "ABDM_ENABLED" | | ||
|
||
export interface FeatureFlagsResponse { | ||
user_flags: FeatureFlag[]; | ||
facility_flags: { | ||
facility: string; | ||
features: FeatureFlag[]; | ||
}[]; | ||
} | ||
|
||
const defaultFlags: FeatureFlag[] = []; | ||
|
||
const FeatureFlagsContext = createContext<FeatureFlagsResponse>({ | ||
user_flags: defaultFlags, | ||
facility_flags: [], | ||
}); | ||
|
||
export const FeatureFlagsProvider = (props: { children: React.ReactNode }) => { | ||
const [featureFlags, setFeatureFlags] = useState<FeatureFlagsResponse>({ | ||
user_flags: defaultFlags, | ||
facility_flags: [], | ||
}); | ||
|
||
const user = useAuthUser(); | ||
|
||
useEffect(() => { | ||
if (user.user_flags) { | ||
setFeatureFlags((ff) => ({ | ||
...ff, | ||
user_flags: [...defaultFlags, ...(user.user_flags || [])], | ||
})); | ||
} | ||
}, [user]); | ||
|
||
return ( | ||
<FeatureFlagsContext.Provider value={featureFlags}> | ||
{props.children} | ||
</FeatureFlagsContext.Provider> | ||
); | ||
}; | ||
|
||
export const useFeatureFlags = (facility?: FacilityModel | string) => { | ||
const [facilityObject, setFacilityObject] = useState< | ||
FacilityModel | undefined | ||
>(typeof facility === "string" ? undefined : facility); | ||
|
||
const context = useContext(FeatureFlagsContext); | ||
if (context === undefined) { | ||
throw new Error( | ||
"useFeatureFlags must be used within a FeatureFlagsProvider", | ||
); | ||
} | ||
|
||
const facilityQuery = useQuery(routes.getPermittedFacility, { | ||
pathParams: { | ||
id: typeof facility === "string" ? facility : "", | ||
}, | ||
prefetch: false, | ||
silent: true, | ||
onResponse: (res) => { | ||
setFacilityObject(res.data); | ||
}, | ||
}); | ||
|
||
const facilityFlags = facilityObject?.facility_flags || []; | ||
|
||
useEffect(() => { | ||
facilityQuery.refetch(); | ||
}, [facility]); | ||
|
||
return [...context.user_flags, ...facilityFlags]; | ||
}; |