@instantdb/react-common 1.0.50 → 1.0.51-branch-optional-async-storage.29876965683.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instantdb/react-common",
3
- "version": "1.0.50",
3
+ "version": "1.0.51-branch-optional-async-storage.29876965683.1",
4
4
  "description": "Instant DB shared components for React and React Native",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://github.com/instantdb/instant/tree/main/client/packages/react-common",
@@ -54,8 +54,8 @@
54
54
  "react": ">=16"
55
55
  },
56
56
  "dependencies": {
57
- "@instantdb/version": "1.0.50",
58
- "@instantdb/core": "1.0.50"
57
+ "@instantdb/core": "1.0.51-branch-optional-async-storage.29876965683.1",
58
+ "@instantdb/version": "1.0.51-branch-optional-async-storage.29876965683.1"
59
59
  },
60
60
  "scripts": {
61
61
  "test": "vitest",
@@ -7,6 +7,7 @@ import {
7
7
  type AuthState,
8
8
  type User,
9
9
  type ConnectionStatus,
10
+ type AppStatusState,
10
11
  type TransactionChunk,
11
12
  type RoomSchemaShape,
12
13
  type InstaQLOptions,
@@ -44,6 +45,11 @@ const defaultAuthState = {
44
45
  error: undefined,
45
46
  };
46
47
 
48
+ const defaultAppStatusState: AppStatusState = {
49
+ isLoading: true,
50
+ isReadOnly: undefined,
51
+ };
52
+
47
53
  export default abstract class InstantReactAbstractDatabase<
48
54
  // need to pull this schema out to another generic for query params, not sure why
49
55
  Schema extends InstantSchemaDef<any, any, any>,
@@ -371,6 +377,42 @@ export default abstract class InstantReactAbstractDatabase<
371
377
  return status;
372
378
  };
373
379
 
380
+ /**
381
+ * Observe the app's maintenance-mode state. While read-only, reads and
382
+ * live queries keep working but writes are rejected.
383
+ *
384
+ * @example
385
+ * function App() {
386
+ * const { isLoading, isReadOnly } = db.useAppStatus()
387
+ * if (isLoading) return null
388
+ * if (isReadOnly) return <MaintenanceBanner />
389
+ * // ...
390
+ * }
391
+ */
392
+ useAppStatus = (): AppStatusState => {
393
+ const stateRef = useRef<AppStatusState>(this.core._reactor._appStatusState);
394
+
395
+ const subscribe = useCallback((cb: () => void) => {
396
+ return this.core.subscribeAppStatus((state) => {
397
+ if (
398
+ state.isLoading !== stateRef.current.isLoading ||
399
+ state.isReadOnly !== stateRef.current.isReadOnly
400
+ ) {
401
+ stateRef.current = state;
402
+ cb();
403
+ }
404
+ });
405
+ }, []);
406
+
407
+ const state = useSyncExternalStore<AppStatusState>(
408
+ subscribe,
409
+ () => stateRef.current,
410
+ () => defaultAppStatusState,
411
+ );
412
+
413
+ return state;
414
+ };
415
+
374
416
  /**
375
417
  * Use this for one-off queries.
376
418
  * Returns local data if available, otherwise fetches from the server.