-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
64 lines (54 loc) · 1.93 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React, { useEffect, useState } from 'react';
import { useQuery } from '@apollo/client';
import { useOktaAuth } from '@okta/okta-react';
import { asyncWithLDProvider } from 'launchdarkly-react-client-sdk';
import GetCurrentUserQuery from 'queries/GetCurrentUserQuery';
import { GetCurrentUser } from 'queries/types/GetCurrentUser';
type WrapperProps = {
children: React.ReactNode;
};
const UserTargetingWrapper = ({ children }: WrapperProps) => {
// wrapping initial value in function to get around useState and setState thinking
// the functional component is a function to be evaluated.
const [LDProvider, setLDProvider] = useState<React.FunctionComponent>(
() => () => <div />
);
const { data } = useQuery<GetCurrentUser>(GetCurrentUserQuery);
useEffect(() => {
if (data) {
(async () => {
const provider = await asyncWithLDProvider({
clientSideID: import.meta.env.VITE_LD_CLIENT_ID as string,
context: {
kind: 'user',
key: data?.currentUser?.launchDarkly.userKey
},
options: {
hash: data?.currentUser?.launchDarkly.signedHash
},
flags: {
atoProcessList: false,
downgradeGovTeam: false,
downgradeTrbAdmin: false,
systemProfileHiddenFields: false,
systemWorkspace: false,
systemWorkspaceRequestsCard: false,
systemWorkspaceTeam: false,
grbReviewTab: false,
showAtoColumn: false
}
});
setLDProvider(() => () => provider({ children }));
})();
}
}, [data, children]);
return <LDProvider>{children}</LDProvider>;
};
const FlagsWrapper = ({ children }: WrapperProps) => {
const { authState } = useOktaAuth();
const Container = authState?.isAuthenticated
? UserTargetingWrapper
: React.Fragment;
return <Container>{children}</Container>;
};
export default FlagsWrapper;