|
| 1 | +import { FormEvent, useState } from 'react'; |
| 2 | +import { |
| 3 | + useFlag, |
| 4 | + useFlagsStatus, |
| 5 | + useUnleashContext, |
| 6 | + useVariant |
| 7 | +} from '@unleash/proxy-client-react'; |
| 8 | +import './App.css'; |
| 9 | + |
| 10 | +const TOGGLE_NAME = 'demo-app.simple-toggle'; |
| 11 | + |
| 12 | +const VariantDetails = ({ toggleName }: { toggleName: string }) => { |
| 13 | + const variant = useVariant(toggleName); |
| 14 | + |
| 15 | + if (!variant?.enabled) { |
| 16 | + return ( |
| 17 | + <p> |
| 18 | + Variant is not enabled for <code>{toggleName}</code>. |
| 19 | + </p> |
| 20 | + ); |
| 21 | + } |
| 22 | + |
| 23 | + return ( |
| 24 | + <> |
| 25 | + <p> |
| 26 | + <strong>Variant name:</strong> {variant.name} |
| 27 | + </p> |
| 28 | + {variant.payload && ( |
| 29 | + <p> |
| 30 | + <strong>Payload:</strong> {JSON.stringify(variant.payload)} |
| 31 | + </p> |
| 32 | + )} |
| 33 | + </> |
| 34 | + ); |
| 35 | +}; |
| 36 | + |
| 37 | +const ToggleStatus = ({ toggleName }: { toggleName: string }) => { |
| 38 | + const enabled = useFlag(toggleName); |
| 39 | + |
| 40 | + return ( |
| 41 | + <p className={`toggle toggle--${enabled ? 'on' : 'off'}`}> |
| 42 | + Feature <code>{toggleName}</code> is{' '} |
| 43 | + <strong>{enabled ? 'enabled' : 'disabled'}</strong> for the current user. |
| 44 | + </p> |
| 45 | + ); |
| 46 | +}; |
| 47 | + |
| 48 | +const ContextForm = ({ |
| 49 | + userId, |
| 50 | + onSubmit |
| 51 | +}: { |
| 52 | + userId: string; |
| 53 | + onSubmit: (nextUserId: string) => Promise<void>; |
| 54 | +}) => { |
| 55 | + const [value, setValue] = useState(userId); |
| 56 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 57 | + const [error, setError] = useState<string | null>(null); |
| 58 | + |
| 59 | + const handleSubmit = async (event: FormEvent<HTMLFormElement>) => { |
| 60 | + event.preventDefault(); |
| 61 | + if (!value) { |
| 62 | + setError('Provide a user id before updating the context.'); |
| 63 | + return; |
| 64 | + } |
| 65 | + if (value === userId) { |
| 66 | + setError('The provided user id matches the current context.'); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + setError(null); |
| 71 | + setIsSubmitting(true); |
| 72 | + try { |
| 73 | + await onSubmit(value); |
| 74 | + } catch (err) { |
| 75 | + setError( |
| 76 | + err instanceof Error ? err.message : 'Failed to update the Unleash context.' |
| 77 | + ); |
| 78 | + } finally { |
| 79 | + setIsSubmitting(false); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + return ( |
| 84 | + <form className="context-form" onSubmit={handleSubmit}> |
| 85 | + <label htmlFor="userId">Simulate another user</label> |
| 86 | + <div className="context-form__row"> |
| 87 | + <input |
| 88 | + id="userId" |
| 89 | + name="userId" |
| 90 | + value={value} |
| 91 | + onChange={event => { |
| 92 | + setError(null); |
| 93 | + setValue(event.target.value); |
| 94 | + }} |
| 95 | + placeholder="e.g. [email protected]" |
| 96 | + /> |
| 97 | + <button type="submit" disabled={isSubmitting}> |
| 98 | + {isSubmitting ? 'Updating…' : 'Update context'} |
| 99 | + </button> |
| 100 | + </div> |
| 101 | + {error && <p className="context-form__error">{error}</p>} |
| 102 | + </form> |
| 103 | + ); |
| 104 | +}; |
| 105 | + |
| 106 | +const App = () => { |
| 107 | + const { flagsReady, flagsError } = useFlagsStatus(); |
| 108 | + const [currentUserId, setCurrentUserId] = useState<string>( |
| 109 | + import.meta.env.VITE_EXAMPLE_DEFAULT_USER ?? 'guest' |
| 110 | + ); |
| 111 | + const updateContext = useUnleashContext(); |
| 112 | + |
| 113 | + const handleContextUpdate = async (nextUserId: string) => { |
| 114 | + await updateContext({ userId: nextUserId }); |
| 115 | + setCurrentUserId(nextUserId); |
| 116 | + }; |
| 117 | + |
| 118 | + if (!flagsReady) { |
| 119 | + return ( |
| 120 | + <main className="app app--loading"> |
| 121 | + <h1>Unleash React SDK Example</h1> |
| 122 | + <p>Fetching feature toggles…</p> |
| 123 | + </main> |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + return ( |
| 128 | + <main className="app"> |
| 129 | + <header> |
| 130 | + <h1>Unleash React SDK Example</h1> |
| 131 | + <p> |
| 132 | + Connected to the Unleash Frontend API configured in{' '} |
| 133 | + <code>.env</code>. The example evaluates the{' '} |
| 134 | + <code>{TOGGLE_NAME}</code> toggle. |
| 135 | + </p> |
| 136 | + </header> |
| 137 | + |
| 138 | + {flagsError && ( |
| 139 | + <section className="card"> |
| 140 | + <h2>Latest client error</h2> |
| 141 | + <pre>{JSON.stringify(flagsError, null, 2)}</pre> |
| 142 | + </section> |
| 143 | + )} |
| 144 | + |
| 145 | + <section className="card"> |
| 146 | + <h2>Toggle status</h2> |
| 147 | + <ToggleStatus toggleName={TOGGLE_NAME} /> |
| 148 | + <VariantDetails toggleName={TOGGLE_NAME} /> |
| 149 | + </section> |
| 150 | + |
| 151 | + <section className="card"> |
| 152 | + <h2>Current context</h2> |
| 153 | + <p> |
| 154 | + Evaluating toggles for <code>{currentUserId}</code>. |
| 155 | + </p> |
| 156 | + <ContextForm userId={currentUserId} onSubmit={handleContextUpdate} /> |
| 157 | + </section> |
| 158 | + </main> |
| 159 | + ); |
| 160 | +}; |
| 161 | + |
| 162 | +export default App; |
0 commit comments