Skip to content

Commit 6835a0d

Browse files
committed
added ld context change into live logs
1 parent 0054d5e commit 6835a0d

File tree

4 files changed

+53
-23
lines changed

4 files changed

+53
-23
lines changed

Diff for: components/ui/app-sidebar.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function AppSidebar() {
8787
<Card className="mb-2" key={index}>
8888
<CardContent className="p-4 w-full overflow-x-auto">
8989
<div className="bg-gray-200 rounded-md py-1 px-2 w-max mb-10 text-xs">
90-
New Flag Change Event Received
90+
{log.type}
9191
</div>
9292
<div className="flex justify-between items-center mb-4 text-xs text-gray-500">
9393
<div

Diff for: pages/_app.tsx

+20-19
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ if (typeof window !== "undefined") {
2828
<NoSSRWrapper>
2929
<DynamicContextProvider>
3030
<DynamicTelemetryProvider>
31-
<LiveLogsProvider>
32-
<SidebarProvider
33-
defaultOpen={false}
34-
style={{
35-
"--sidebar-width": "30vw",
36-
"--sidebar-width-mobile": "100vw",
37-
}}
38-
>
39-
<LoginProvider>
40-
<QuickCommandDialog>
41-
<TripsProvider>
31+
<LoginProvider>
32+
<TripsProvider>
33+
<LiveLogsProvider>
34+
<SidebarProvider
35+
defaultOpen={false}
36+
style={{
37+
"--sidebar-width": "30vw",
38+
"--sidebar-width-mobile": "100vw",
39+
}}
40+
>
41+
<QuickCommandDialog>
4242
<KeyboardNavigation />
4343

4444
<Head>
@@ -48,16 +48,17 @@ if (typeof window !== "undefined") {
4848
/>
4949
<link rel="apple-touch-icon" href="/apple-icon.png" />
5050
</Head>
51-
5251
<Component {...pageProps} />
5352
<AppSidebar />
54-
55-
<SidebarTrigger className="bg-airlinedarkblue fixed bottom-4 left-4 h-12 w-12 hover:bg-airlinedarkblue z-10" title= "Click to open sidebar to show server side calls" />
56-
</TripsProvider>
57-
</QuickCommandDialog>
58-
</LoginProvider>
59-
</SidebarProvider>
60-
</LiveLogsProvider>
53+
<SidebarTrigger
54+
className="bg-airlinedarkblue fixed bottom-4 left-4 h-12 w-12 hover:bg-airlinedarkblue z-10"
55+
title="Click to open sidebar to show server side calls"
56+
/>
57+
</QuickCommandDialog>
58+
</SidebarProvider>
59+
</LiveLogsProvider>
60+
</TripsProvider>
61+
</LoginProvider>
6162
</DynamicTelemetryProvider>
6263
</DynamicContextProvider>
6364
</NoSSRWrapper>

Diff for: utils/contexts/LiveLogsContext.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { createContext, useEffect, useState } from "react";
1+
import { createContext, useEffect, useState, useContext } from "react";
22
import { useFlags, useLDClient } from "launchdarkly-react-client-sdk";
33
import Prism from "prismjs";
4+
import LoginContext from "@/utils/contexts/login";
5+
import ContextProvider from "@/components/ContextProvider";
46

57
const LiveLogsContext = createContext();
68

@@ -11,6 +13,8 @@ export const LiveLogsProvider = ({ children }) => {
1113
const [currentLDFlagEnvValues, setCurrentLDFlagEnvValues] = useState([]);
1214
const allLDFlags = useFlags();
1315
const client = useLDClient();
16+
const { appMultiContext } = useContext(LoginContext);
17+
1418

1519
useEffect(() => {
1620
Prism.highlightAll();
@@ -30,12 +34,28 @@ export const LiveLogsProvider = ({ children }) => {
3034
{
3135
date: time,
3236
log: settings,
37+
type: "New Flag Change Event Received"
3338
},
3439
];
3540
});
3641
});
3742
}, [client]);
3843

44+
useEffect(() => {
45+
const time = new Date();
46+
47+
setLiveLogs((prevLogs) => {
48+
return [
49+
...prevLogs,
50+
{
51+
date: time,
52+
log: appMultiContext,
53+
type:"New LD Context Change Event Sent"
54+
},
55+
];
56+
});
57+
}, [appMultiContext]);
58+
3959
return (
4060
<LiveLogsContext.Provider value={{ liveLogs, currentLDFlagEnvValues }}>
4161
{children}

Diff for: utils/contexts/login.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const LoginProvider = ({ children }) => {
2727
const client = useLDClient();
2828
const [isLoggedIn, setIsLoggedIn] = useState(false);
2929
const [userObject, setUserObject] = useState({});
30+
const [appMultiContext, setAppMultiContext] = useState({});
3031
const [allUsers, setAllUsers] = useState(STARTER_PERSONAS);
3132

3233
const hashEmail = async (email) => {
@@ -72,9 +73,9 @@ export const LoginProvider = ({ children }) => {
7273
context.audience.key = existingAudienceKey;
7374
context.location = await getLocation();
7475
context.user.launchclub = foundPersona.personalaunchclubstatus;
76+
setAppMultiContext(context)
7577
await client?.identify(context);
7678
console.log("loginUser", context);
77-
console.log(foundPersona.personaname, foundPersona.personaemail, foundPersona.personarole);
7879

7980
setCookie(LD_CONTEXT_COOKIE_KEY, context);
8081
setIsLoggedIn(true);
@@ -84,13 +85,17 @@ export const LoginProvider = ({ children }) => {
8485
const context = await client?.getContext();
8586
console.log("updateAudienceContext", context);
8687
context.audience.key = uuidv4().slice(0, 10);
88+
setAppMultiContext(context);
89+
setCookie(LD_CONTEXT_COOKIE_KEY, context);
8790
await client?.identify(context);
8891
};
8992

9093
const updateUserContext = async () => {
9194
const context = await client?.getContext();
9295
console.log("updateUserContext", context);
9396
context.user.key = uuidv4().slice(0, 10);
97+
setAppMultiContext(context);
98+
setCookie(LD_CONTEXT_COOKIE_KEY, context);
9499
await client?.identify(context);
95100
};
96101

@@ -130,10 +135,11 @@ export const LoginProvider = ({ children }) => {
130135
},
131136
};
132137
const context = createAnonymousContext;
138+
setAppMultiContext(context);
133139
await client?.identify(context);
134140
setCookie(LD_CONTEXT_COOKIE_KEY, context);
135141
console.log("Anonymous User", context);
136-
setCookie("ldcontext", context);
142+
137143
};
138144

139145
// const setPlaneContext = async (plane) => {
@@ -150,7 +156,9 @@ export const LoginProvider = ({ children }) => {
150156
setUserObject((prevObj) => ({ ...prevObj, personalaunchclubstatus: LAUNCH_CLUB_PLATINUM }));
151157
context.user.launchclub = LAUNCH_CLUB_PLATINUM;
152158
console.log("User upgraded to " + LAUNCH_CLUB_PLATINUM + " status");
159+
setAppMultiContext(context);
153160
client.identify(context);
161+
setCookie(LD_CONTEXT_COOKIE_KEY, context);
154162
};
155163

156164
const enrollInLaunchClub = () => {
@@ -171,6 +179,7 @@ export const LoginProvider = ({ children }) => {
171179
loginUser,
172180
logoutUser,
173181
allUsers,
182+
appMultiContext
174183
}}
175184
>
176185
{children}

0 commit comments

Comments
 (0)