-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtargeting-by-traits.tsx
62 lines (58 loc) · 2.18 KB
/
targeting-by-traits.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
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 Traits = { teamMember: boolean };
type ServerSideProps = {
initialFlagState: InitialFlagState;
traits: Traits;
};
// 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 traits: Traits = { teamMember: true };
const { initialFlagState } = await getFlags({ context, traits });
return { props: { initialFlagState, traits } };
};
export default function Page(props: ServerSideProps) {
const flagBag = useFlags({
initialState: props.initialFlagState,
traits: props.traits,
});
return (
<Layout
title="Targeting by Traits"
source={`https://github.com/happykit/flags/blob/${process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_REF}/example/pages/demo/targeting-by-traits.tsx`}
flagBag={flagBag}
>
<article className="py-4 prose max-w-prose">
<p>
This demo shows how to use <code>@happykit/flags</code> for targeting
by traits.
</p>
<p>
You can pass any traits into the flag evaluation. These traits can
then be used by the rules defined in your flags. This allows you to
resolve flags differently based on the provided traits.
</p>
<p>
Traits can be related to the visitor, to the authenticated user or to
anything else. You can pass any traits you want. Use the traits in
your HappyKit flag rules to resolve the flag to different variants
based on the passed traits.
</p>
<Result key="targeting-by-traits" value={flagBag} />
<p>
Note that aside from traits, HappyKit also has the concepts of a
visitor and a user. These three concepts are all independent of each
other.
</p>
</article>
</Layout>
);
}