-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdynamics.tsx
219 lines (209 loc) · 7.66 KB
/
dynamics.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import * as React from "react";
import { Layout } from "../../components/Layout";
import { Result } from "../../components/Result";
import { useFlags } from "@happykit/flags/client";
import { AppFlags } from "../../types/AppFlags";
import { RadioGroup } from "@headlessui/react";
import clsx from "clsx";
export default function Page() {
const users = React.useMemo(
() => [
{
name: "No user",
description: "Passes no user",
value: null,
},
{
name: "George",
description: "Passes a user with the key george",
value: { key: "george" },
},
{
name: "Linda",
description: "Passes a user with the key linda",
value: { key: "linda" },
},
],
[]
);
const traits = React.useMemo(
() => [
{
name: "No traits",
description: "Passes no traits",
value: null,
},
{
name: "Team Member (yes)",
description: "Passes teamMember: true as a trait",
value: { teamMember: true },
},
{
name: "Team Member (no)",
description: "Passes teamMember: false as a trait",
value: { teamMember: false },
},
],
[]
);
const [user, setUser] = React.useState(users[0]);
const [trait, setTraits] = React.useState(traits[0]);
const flagBag = useFlags<AppFlags>({
user: user.value,
traits: trait.value,
});
return (
<Layout
title="Dynamic"
source={`https://github.com/happykit/flags/blob/${process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_REF}/example/pages/demo/dynamics.tsx`}
flagBag={flagBag}
>
<article className="py-4 prose max-w-prose">
<p>
This demo shows how the <code>@happykit/flags</code> client behaves
when the passed in user attributes or traits change.
</p>
<p>
Simulate changing user attributes or traits using the buttons below.
Explore how the loading state changes throughout the lifecycle.
</p>
<p>Use the buttons below to modify the passed in values</p>
<RadioGroup value={user} onChange={setUser}>
<RadioGroup.Label className="text-sm font-medium text-gray-900">
User
</RadioGroup.Label>
<div className="mt-1 bg-white rounded-md shadow-sm -space-y-px">
{users.map((setting, settingIdx) => (
<RadioGroup.Option
key={setting.name}
value={setting}
className={({ checked }) =>
clsx(
settingIdx === 0 ? "rounded-tl-md rounded-tr-md" : "",
settingIdx === users.length - 1
? "rounded-bl-md rounded-br-md"
: "",
checked
? "bg-blue-50 border-blue-200 z-10"
: "border-gray-200",
"relative border p-4 flex cursor-pointer focus:outline-none"
)
}
>
{({ active, checked }) => (
<React.Fragment>
<span
className={clsx(
checked
? "bg-blue-600 border-transparent"
: "bg-white border-gray-300",
active ? "ring-2 ring-offset-2 ring-blue-500" : "",
"h-4 w-4 mt-0.5 cursor-pointer rounded-full border flex items-center justify-center"
)}
aria-hidden="true"
>
<span className="rounded-full bg-white w-1.5 h-1.5" />
</span>
<div className="ml-3 flex flex-col">
<RadioGroup.Label
as="span"
className={clsx(
checked ? "text-blue-900" : "text-gray-900",
"block text-sm font-medium"
)}
>
{setting.name}
</RadioGroup.Label>
<RadioGroup.Description
as="span"
className={clsx(
checked ? "text-blue-700" : "text-gray-500",
"block text-sm"
)}
>
{setting.description}
</RadioGroup.Description>
</div>
</React.Fragment>
)}
</RadioGroup.Option>
))}
</div>
</RadioGroup>
<RadioGroup value={trait} onChange={setTraits} className="mt-3">
<RadioGroup.Label className="text-sm font-medium text-gray-900">
Traits
</RadioGroup.Label>
<div className="mt-1 bg-white rounded-md shadow-sm -space-y-px">
{traits.map((setting, settingIdx) => (
<RadioGroup.Option
key={setting.name}
value={setting}
className={({ checked }) =>
clsx(
settingIdx === 0 ? "rounded-tl-md rounded-tr-md" : "",
settingIdx === traits.length - 1
? "rounded-bl-md rounded-br-md"
: "",
checked
? "bg-blue-50 border-blue-200 z-10"
: "border-gray-200",
"relative border p-4 flex cursor-pointer focus:outline-none"
)
}
>
{({ active, checked }) => (
<React.Fragment>
<span
className={clsx(
checked
? "bg-blue-600 border-transparent"
: "bg-white border-gray-300",
active ? "ring-2 ring-offset-2 ring-blue-500" : "",
"h-4 w-4 mt-0.5 cursor-pointer rounded-full border flex items-center justify-center"
)}
aria-hidden="true"
>
<span className="rounded-full bg-white w-1.5 h-1.5" />
</span>
<div className="ml-3 flex flex-col">
<RadioGroup.Label
as="span"
className={clsx(
checked ? "text-blue-900" : "text-gray-900",
"block text-sm font-medium"
)}
>
{setting.name}
</RadioGroup.Label>
<RadioGroup.Description
as="span"
className={clsx(
checked ? "text-blue-700" : "text-gray-500",
"block text-sm"
)}
>
{setting.description}
</RadioGroup.Description>
</div>
</React.Fragment>
)}
</RadioGroup.Option>
))}
</div>
</RadioGroup>
<div className="text-sm font-medium text-gray-900 mt-8">
Generated Input
</div>
<pre className="font-mono rounded bg-gray-200 p-2 text-gray-800">
useFlags(
{JSON.stringify({ user: user.value, traits: trait.value }, null, 2)})
</pre>
<div className="text-sm font-medium text-gray-900 mt-8">
Value returned from hook
</div>
<Result key="static-site-generation-hybrid" value={flagBag} />
</article>
</Layout>
);
}