Skip to content

Commit e702bd4

Browse files
committed
feat: add suspense, change demos
Signed-off-by: Todd Baert <[email protected]>
1 parent 54fd30f commit e702bd4

11 files changed

+318
-94
lines changed

.gitignore

+9-1
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,12 @@ dist
127127
.yarn/unplugged
128128
.yarn/build-state.yml
129129
.yarn/install-state.gz
130-
.pnp.*
130+
.pnp.*
131+
132+
# yalc
133+
.yalc/
134+
yalc.lock
135+
yalc.sig
136+
137+
# build
138+
build/

package-lock.json

+66-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"@openfeature/react-sdk": "^0.0.4-experimental",
6+
"@openfeature/react-sdk": "file:.yalc/@openfeature/react-sdk",
77
"@testing-library/jest-dom": "^5.17.0",
88
"@testing-library/react": "^13.4.0",
99
"@testing-library/user-event": "^13.5.0",
@@ -13,6 +13,7 @@
1313
"@types/react-dom": "^18.2.14",
1414
"react": "^18.2.0",
1515
"react-dom": "^18.2.0",
16+
"react-router-dom": "^6.20.0",
1617
"react-scripts": "5.0.1",
1718
"typescript": "^4.9.5",
1819
"web-vitals": "^2.1.4"

src/App.test.tsx

-9
This file was deleted.

src/App.tsx

-40
This file was deleted.

src/constants.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const SUSPENSE_DEMO_NAME = 'suspense';
2+
export const FLAG_CHANGE_DEMO_NAME = 'flag-change';

src/App.css renamed to src/demos/Demo.css

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
1-
.App {
1+
.Demo {
22
text-align: center;
33
}
44

5-
.App-logo {
5+
.Demo-logo {
66
height: 40vmin;
77
pointer-events: none;
88
}
99

1010
@media (prefers-reduced-motion: no-preference) {
11-
.App-logo {
12-
animation: App-logo-spin infinite 20s linear;
11+
.Demo-logo {
12+
animation: Demo-logo-spin infinite 20s linear;
1313
}
1414
}
1515

16-
.App-header {
17-
background-color: #282c34;
16+
.Demo-header {
1817
min-height: 100vh;
1918
display: flex;
2019
flex-direction: column;
2120
align-items: center;
2221
justify-content: center;
2322
font-size: calc(10px + 2vmin);
24-
color: white;
2523
}
2624

27-
.App-link {
25+
.Demo-link {
2826
color: #61dafb;
2927
}
3028

31-
@keyframes App-logo-spin {
29+
@keyframes Demo-logo-spin {
3230
from {
3331
transform: rotate(0deg);
3432
}

src/demos/FlagChangeDemo.tsx

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import {
2+
InMemoryProvider,
3+
OpenFeature,
4+
OpenFeatureProvider,
5+
useFeatureFlag
6+
} from '@openfeature/react-sdk';
7+
import { useEffect } from 'react';
8+
import { FLAG_CHANGE_DEMO_NAME } from '../constants';
9+
import logo from '../logo.svg';
10+
import './Demo.css';
11+
12+
const PROVIDER_NAME = FLAG_CHANGE_DEMO_NAME;
13+
14+
/**
15+
* A component associated with a provider whose flags change every 2 seconds.
16+
* It demonstrates the React SDKs ability to dynamically react to flag changes from the provider.
17+
*/
18+
function FlagChangeDemo() {
19+
20+
const newMessageName = 'new-message';
21+
const flagConfig = {
22+
[newMessageName]: {
23+
disabled: false,
24+
variants: {
25+
on: true,
26+
off: false,
27+
},
28+
defaultVariant: 'on',
29+
},
30+
};
31+
const provider = new InMemoryProvider();
32+
33+
// Set our provider, give it a name matching the scope of our OpenFeatureProvider below
34+
OpenFeature.setProvider(PROVIDER_NAME, provider);
35+
36+
// Start an interval to change the flag vales every 2s.
37+
const interval = window.setInterval(() => {
38+
flagConfig[newMessageName].defaultVariant = flagConfig[newMessageName].defaultVariant === 'on' ? 'off' : 'on';
39+
provider.putConfiguration(flagConfig);
40+
}, 2000);
41+
42+
useEffect(() => {
43+
return () => {
44+
window.clearInterval(interval);
45+
}
46+
});
47+
48+
return (
49+
// This page is scoped to the "flag-change" provider.
50+
<OpenFeatureProvider name={PROVIDER_NAME}>
51+
<Content />
52+
</OpenFeatureProvider>
53+
);
54+
}
55+
56+
function Content() {
57+
return (
58+
<div className='Demo'>
59+
<header className='Demo-header'>
60+
<img src={logo} className='Demo-logo' alt='logo' />
61+
{/* The Message component evaluates a feature flag.
62+
The component markup will be update dynamically when the provider emits a flag-change event. */}
63+
<Message />
64+
</header>
65+
</div>
66+
);
67+
}
68+
69+
function Message() {
70+
const { value: showNewMessage } = useFeatureFlag('new-message', true);
71+
72+
return (
73+
<>
74+
{showNewMessage ? (
75+
<p>Welcome to this OpenFeature-enabled React app!</p>
76+
) : (
77+
<p>Welcome to this boring React app.</p>
78+
)}
79+
</>
80+
);
81+
}
82+
83+
export default FlagChangeDemo;

0 commit comments

Comments
 (0)