@schematichq/schematic-react 1.5.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -136,6 +136,69 @@ const MyComponent = () => {
136
136
 
137
137
  *Note: `useSchematicIsPending` is checking if entitlement data has been loaded, typically via `identify`. It should, therefore, be used to wrap flag and entitlement checks, but never the initial call to `identify`.*
138
138
 
139
+ For features metered by credit burndown, the same entitlement object carries the company's credit position:
140
+
141
+ | Property | Type | Description |
142
+ | --- | --- | --- |
143
+ | `creditId` | `string \| undefined` | The ID of the credit funding this feature |
144
+ | `creditSettled` | `number \| undefined` | The spendable balance, including any amount held by an open lease. This is the number to show end users |
145
+ | `creditRemaining` | `number \| undefined` | The balance available to fund new consumption, excluding any open lease hold |
146
+ | `creditReserved` | `number \| undefined` | The unspent amount held by an open credit lease, `0` when none is open |
147
+
148
+ All four are `undefined` when the feature is not credit-based.
149
+
150
+ ```tsx
151
+ import { useSchematicEntitlement } from "@schematichq/schematic-react";
152
+ import { Feature, OutOfCredits } from "./components";
153
+
154
+ const MyComponent = () => {
155
+ const { creditSettled, value: isFeatureEnabled } =
156
+ useSchematicEntitlement("my-flag-key");
157
+
158
+ if (!isFeatureEnabled) {
159
+ return <OutOfCredits />;
160
+ }
161
+
162
+ return <Feature creditsRemaining={creditSettled} />;
163
+ };
164
+ ```
165
+
166
+ These values refresh with each flag check. For a balance that also updates on the credit partials arriving between checks, pass `creditId` to [`useSchematicCreditBalance`](#credit-balances) instead.
167
+
168
+ ### Usage warnings
169
+
170
+ If a usage warning is configured on the entitlement, the hook returns it as `warningTiers`, so you can warn a customer before they hit the limit rather than after:
171
+
172
+ | Property | Type | Description |
173
+ | --- | --- | --- |
174
+ | `warningTiers` | `WarningTier[] \| undefined` | The usage warning thresholds configured on the entitlement, each a `{ key, value }` pair in the entitlement's usage units. `undefined` when none are configured |
175
+ | `softLimit` | `number \| undefined` | For usage-based pricing, the soft limit for overage charges or the next tier boundary |
176
+
177
+ The dashboard writes a single tier under the key `default`.
178
+
179
+ ```tsx
180
+ import { useSchematicEntitlement } from "@schematichq/schematic-react";
181
+ import { Feature, ApproachingLimit } from "./components";
182
+
183
+ const MyComponent = () => {
184
+ const { featureUsage, warningTiers } =
185
+ useSchematicEntitlement("my-flag-key");
186
+
187
+ const warning = warningTiers?.find((tier) => tier.key === "default");
188
+ const approachingLimit =
189
+ typeof featureUsage === "number" &&
190
+ typeof warning?.value === "number" &&
191
+ featureUsage >= warning.value;
192
+
193
+ return (
194
+ <>
195
+ {approachingLimit && <ApproachingLimit limit={warning.value} />}
196
+ <Feature />
197
+ </>
198
+ );
199
+ };
200
+ ```
201
+
139
202
  ### Company plan information
140
203
 
141
204
  To access the current company's plan and trial status, you can use the `useSchematicPlan` hook:
@@ -200,7 +263,22 @@ The hook returns an object with the following properties:
200
263
  | `balance` | `number` | The spendable balance, or `0` while loading or when the company holds no balance in this credit |
201
264
  | `isLoading` | `boolean` | `true` while the balance is still loading and no value has arrived yet |
202
265
 
203
- The credit ID is available on a feature's entitlement: `useSchematicEntitlement(key)` returns `creditId` for credit-based features.
266
+ The credit ID is available on a feature's entitlement, and the hook accepts `string | undefined`, so you can feed it straight through without waiting for the check to arrive:
267
+
268
+ ```tsx
269
+ const CreditMeter = () => {
270
+ const { creditId } = useSchematicEntitlement("my-flag-key");
271
+ const { balance, isLoading } = useSchematicCreditBalance(creditId);
272
+
273
+ if (isLoading) {
274
+ return <div>Loading…</div>;
275
+ }
276
+
277
+ return <div>{balance} credits remaining</div>;
278
+ };
279
+ ```
280
+
281
+ While `creditId` is `undefined`, the hook reports the client's loading state and a balance of `0`.
204
282
 
205
283
  ## Fallback Behavior
206
284
 
@@ -260,7 +338,7 @@ When events (track, identify) cannot be sent due to network issues, they are aut
260
338
 
261
339
  ### WebSocket Fallback
262
340
 
263
- In WebSocket mode, if the WebSocket connection fails, the SDK will provide the last known value or the configured fallback values as [outlined above](/#flag-check-fallbacks). The WebSocket will also automatically attempt to re-establish it's connection with Schematic using an exponential backoff.
341
+ In WebSocket mode, if the WebSocket connection fails, the SDK will provide the last known value or the configured fallback values as [outlined above](#flag-check-fallbacks). The WebSocket will also automatically attempt to re-establish its connection with Schematic using an exponential backoff.
264
342
 
265
343
  ## React Native
266
344