-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtargeting-by-user.tsx
58 lines (53 loc) · 1.94 KB
/
targeting-by-user.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
import * as React from "react";
import { GetServerSideProps } from "next";
import { Layout } from "components/Layout";
import { Result } from "components/Result";
import { getFlags } from "flags/server";
import { type InitialFlagState, useFlags } from "flags/client";
type User = { key: string; name: string };
type ServerSideProps = {
initialFlagState: InitialFlagState;
user: User;
};
// This demo uses server-side rendering, but this works just as well with
// static site generation or client-only rendering.
export const getServerSideProps: GetServerSideProps<ServerSideProps> = async (
context
) => {
// These could be loaded from anywhere
const user: User = { key: "fake-user-key-1", name: "Jon" };
const { initialFlagState } = await getFlags({ context, user });
return { props: { initialFlagState, user } };
};
export default function Page(props: ServerSideProps) {
const flagBag = useFlags({
initialState: props.initialFlagState,
user: props.user,
});
return (
<Layout
title="Targeting by User"
source={`https://github.com/happykit/flags/blob/${process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_REF}/example/pages/demo/targeting-by-user.tsx`}
flagBag={flagBag}
>
<article className="py-4 prose max-w-prose">
<p>
This demo shows how to use <code>@happykit/flags</code> for targeting
users.
</p>
<p>
HappyKit allows you to do pass in a user. You can use that user and
the provided uesr profile for rules or percentage-based rollouts. The
fields supported in the user profile are defined in the README and in
the TypeScript types.
</p>
<Result key="targeting-by-user" value={flagBag} />
<p>
Note that aside from users, HappyKit also has the concepts of a
visitor and traits. These three concepts are all independent of each
other.
</p>
</article>
</Layout>
);
}