react-native-inapp-inspector 1.1.1 → 1.1.3

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.
Files changed (45) hide show
  1. package/README.md +14 -0
  2. package/dist/commonjs/components/ConsoleLogCard.js +18 -0
  3. package/dist/commonjs/components/JsonViewer.d.ts +2 -1
  4. package/dist/commonjs/components/JsonViewer.js +6 -4
  5. package/dist/commonjs/components/LogCard.js +17 -0
  6. package/dist/commonjs/components/MetaAccordion.js +26 -6
  7. package/dist/commonjs/components/NetworkIcons.d.ts +9 -2
  8. package/dist/commonjs/components/NetworkIcons.js +59 -3
  9. package/dist/commonjs/components/ReduxTreeView.js +80 -5
  10. package/dist/commonjs/constants/version.d.ts +1 -1
  11. package/dist/commonjs/constants/version.js +1 -1
  12. package/dist/commonjs/customHooks/reduxLogger.d.ts +21 -7
  13. package/dist/commonjs/customHooks/reduxLogger.js +147 -48
  14. package/dist/commonjs/customHooks/webViewLogger.js +13 -8
  15. package/dist/commonjs/helpers/settingsStore.d.ts +24 -0
  16. package/dist/commonjs/helpers/settingsStore.js +74 -0
  17. package/dist/commonjs/index.d.ts +1 -1
  18. package/dist/commonjs/index.js +897 -170
  19. package/dist/commonjs/styles/index.d.ts +40 -0
  20. package/dist/commonjs/styles/index.js +45 -2
  21. package/dist/commonjs/types/index.d.ts +4 -0
  22. package/dist/esm/components/ConsoleLogCard.js +18 -0
  23. package/dist/esm/components/JsonViewer.d.ts +2 -1
  24. package/dist/esm/components/JsonViewer.js +6 -4
  25. package/dist/esm/components/LogCard.js +17 -0
  26. package/dist/esm/components/MetaAccordion.js +27 -7
  27. package/dist/esm/components/NetworkIcons.d.ts +9 -2
  28. package/dist/esm/components/NetworkIcons.js +51 -2
  29. package/dist/esm/components/ReduxTreeView.js +81 -6
  30. package/dist/esm/constants/version.d.ts +1 -1
  31. package/dist/esm/constants/version.js +1 -1
  32. package/dist/esm/customHooks/reduxLogger.d.ts +21 -7
  33. package/dist/esm/customHooks/reduxLogger.js +145 -47
  34. package/dist/esm/customHooks/webViewLogger.js +13 -8
  35. package/dist/esm/helpers/settingsStore.d.ts +24 -0
  36. package/dist/esm/helpers/settingsStore.js +67 -0
  37. package/dist/esm/index.d.ts +1 -1
  38. package/dist/esm/index.js +896 -172
  39. package/dist/esm/styles/index.d.ts +40 -0
  40. package/dist/esm/styles/index.js +45 -2
  41. package/dist/esm/types/index.d.ts +4 -0
  42. package/example/App.tsx +199 -61
  43. package/example/ios/example.xcodeproj/project.pbxproj +0 -8
  44. package/example/package-lock.json +4 -3
  45. package/package.json +1 -1
@@ -1,8 +1,28 @@
1
+ // #8 — Redux inspector module.
2
+ //
3
+ // Two integration paths, both feeding the same timeline/state snapshot:
4
+ //
5
+ // 1. `inspectorReduxMiddleware` (recommended) — a standard Redux middleware.
6
+ // Because it sits inside the middleware chain it sees EVERY action,
7
+ // including ones dispatched from thunks, sagas, listeners and RTK Query.
8
+ //
9
+ // 2. `connectReduxStore(store)` — zero-config fallback. It wraps the outer
10
+ // `store.dispatch` AND diffs state on `store.subscribe`, so even actions
11
+ // that bypass the wrapped dispatch (thunk/saga internals capture the raw
12
+ // dispatch reference at store-creation time) still update the state tree
13
+ // and per-reducer "last action" consistently.
1
14
  let currentReduxState = null;
2
15
  const listeners = new Set();
3
16
  let globalReduxAutoRefresh = true;
4
17
  let lastActionForReducer = {};
5
18
  let actionHistory = [];
19
+ const MAX_HISTORY = 50;
20
+ let historyIdSeq = 0;
21
+ // Guards against double-instrumentation (e.g. connectReduxStore called twice,
22
+ // or middleware + connect used together) which previously produced duplicate
23
+ // timeline entries and inconsistent counts.
24
+ const connectedStores = new WeakSet();
25
+ let middlewareAttached = false;
6
26
  export const getReduxState = () => currentReduxState;
7
27
  export const setReduxAutoRefresh = (val) => {
8
28
  globalReduxAutoRefresh = val;
@@ -11,16 +31,16 @@ export const getReduxAutoRefresh = () => globalReduxAutoRefresh;
11
31
  export const getLastActionForReducer = () => lastActionForReducer;
12
32
  export const clearLastActionForReducer = () => {
13
33
  lastActionForReducer = {};
14
- listeners.forEach(cb => cb());
34
+ notify();
15
35
  };
16
36
  export const getActionHistory = () => actionHistory;
17
37
  export const clearActionHistory = () => {
18
38
  actionHistory = [];
19
- listeners.forEach(cb => cb());
39
+ notify();
20
40
  };
21
41
  export const setReduxState = (state) => {
22
42
  currentReduxState = state;
23
- listeners.forEach(cb => cb());
43
+ notify();
24
44
  };
25
45
  export const subscribeReduxState = (cb) => {
26
46
  listeners.add(cb);
@@ -28,6 +48,82 @@ export const subscribeReduxState = (cb) => {
28
48
  listeners.delete(cb);
29
49
  };
30
50
  };
51
+ function notify() {
52
+ listeners.forEach(cb => cb());
53
+ }
54
+ function actionTypeOf(action) {
55
+ if (typeof action === 'string')
56
+ return action;
57
+ if (action && typeof action === 'object' && action.type != null) {
58
+ return String(action.type);
59
+ }
60
+ if (typeof action === 'function') {
61
+ return action.name ? `thunk: ${action.name}` : 'thunk';
62
+ }
63
+ return 'UNKNOWN_ACTION';
64
+ }
65
+ function recordAction(action, prevState, nextState) {
66
+ const type = actionTypeOf(action);
67
+ const payload = action && typeof action === 'object' && action.payload !== undefined
68
+ ? action.payload
69
+ : null;
70
+ const timestamp = new Date().toLocaleTimeString();
71
+ const affectedSlices = [];
72
+ if (prevState &&
73
+ nextState &&
74
+ typeof prevState === 'object' &&
75
+ typeof nextState === 'object') {
76
+ Object.keys(nextState).forEach(key => {
77
+ if (prevState[key] !== nextState[key]) {
78
+ lastActionForReducer[key] = { type, payload, timestamp };
79
+ affectedSlices.push(key);
80
+ }
81
+ });
82
+ }
83
+ actionHistory.unshift({
84
+ id: ++historyIdSeq,
85
+ type,
86
+ payload,
87
+ timestamp,
88
+ affectedSlices,
89
+ });
90
+ if (actionHistory.length > MAX_HISTORY) {
91
+ actionHistory.length = MAX_HISTORY;
92
+ }
93
+ if (globalReduxAutoRefresh) {
94
+ currentReduxState = nextState;
95
+ }
96
+ // Timeline / last-action map always changed — notify even when the state
97
+ // tree snapshot is paused so those panels stay live.
98
+ notify();
99
+ }
100
+ /**
101
+ * Standard Redux middleware — add it to your store to capture every action,
102
+ * including those dispatched from thunks, sagas and RTK Query:
103
+ *
104
+ * const store = configureStore({
105
+ * reducer,
106
+ * middleware: gDM => gDM().concat(inspectorReduxMiddleware),
107
+ * });
108
+ *
109
+ * Pair with `connectReduxStore(store)` (safe — they de-duplicate) or rely on
110
+ * the middleware alone; the initial snapshot is taken on the first action.
111
+ */
112
+ export const inspectorReduxMiddleware = (storeApi) => (next) => (action) => {
113
+ middlewareAttached = true;
114
+ // Thunks are functions — let them run; their inner plain-action dispatches
115
+ // pass back through this same middleware, so nothing is lost.
116
+ if (typeof action === 'function') {
117
+ return next(action);
118
+ }
119
+ const prevState = storeApi.getState();
120
+ const result = next(action);
121
+ const nextState = storeApi.getState();
122
+ if (currentReduxState == null)
123
+ currentReduxState = nextState;
124
+ recordAction(action, prevState, nextState);
125
+ return result;
126
+ };
31
127
  export const connectReduxStore = (store) => {
32
128
  if (!store ||
33
129
  typeof store.getState !== 'function' ||
@@ -35,60 +131,62 @@ export const connectReduxStore = (store) => {
35
131
  console.warn('[NetworkInspector] Invalid Redux store passed to connectReduxStore.');
36
132
  return;
37
133
  }
38
- // Intercept dispatch calls to log actions and tie them to modified state slices
134
+ // Idempotent connecting the same store twice must not double-wrap
135
+ // dispatch or double-record the timeline.
136
+ if (connectedStores.has(store)) {
137
+ return;
138
+ }
139
+ connectedStores.add(store);
140
+ // Wrap the outer dispatch so directly dispatched actions get full
141
+ // type/payload attribution. Skipped when the middleware is already
142
+ // attached, otherwise every direct dispatch would be recorded twice.
39
143
  const originalDispatch = store.dispatch.bind(store);
144
+ let inWrappedDispatch = false;
40
145
  store.dispatch = (action) => {
41
- const prevState = store.getState();
42
- const result = originalDispatch(action);
43
- const nextState = store.getState();
44
- // Map the dispatched action to state slices that actually changed
45
- const affectedSlices = [];
46
- if (prevState &&
47
- nextState &&
48
- typeof prevState === 'object' &&
49
- typeof nextState === 'object' &&
50
- action &&
51
- typeof action === 'object') {
52
- Object.keys(nextState).forEach(key => {
53
- if (prevState[key] !== nextState[key]) {
54
- const actionObj = {
55
- type: action.type || 'UNKNOWN_ACTION',
56
- payload: action.payload !== undefined ? action.payload : null,
57
- timestamp: new Date().toLocaleTimeString(),
58
- };
59
- lastActionForReducer[key] = actionObj;
60
- affectedSlices.push(key);
61
- }
62
- });
63
- // Push to history
64
- actionHistory.unshift({
65
- id: Date.now() + Math.random(),
66
- type: action.type || 'UNKNOWN_ACTION',
67
- payload: action.payload !== undefined ? action.payload : null,
68
- timestamp: new Date().toLocaleTimeString(),
69
- affectedSlices,
70
- });
71
- // Cap size at 50
72
- if (actionHistory.length > 50) {
73
- actionHistory.pop();
146
+ if (middlewareAttached || typeof action === 'function') {
147
+ // Middleware handles recording, or it's a thunk whose inner dispatches
148
+ // will be picked up individually.
149
+ inWrappedDispatch = true;
150
+ try {
151
+ return originalDispatch(action);
152
+ }
153
+ finally {
154
+ inWrappedDispatch = false;
74
155
  }
75
156
  }
76
- if (globalReduxAutoRefresh) {
77
- // Refresh the displayed state tree snapshot.
78
- setReduxState(nextState);
157
+ const prevState = store.getState();
158
+ inWrappedDispatch = true;
159
+ let result;
160
+ try {
161
+ result = originalDispatch(action);
79
162
  }
80
- else {
81
- // Tree is paused, but the action timeline / last-action map still changed,
82
- // so notify subscribers to re-render those without moving the tree snapshot.
83
- listeners.forEach(cb => cb());
163
+ finally {
164
+ inWrappedDispatch = false;
84
165
  }
166
+ recordAction(action, prevState, store.getState());
85
167
  return result;
86
168
  };
87
169
  setReduxState(store.getState());
88
- // Listen to subscription for devtools updates or any other state changes
170
+ // Subscribe-diff fallback: catches state changes whose dispatch bypassed
171
+ // the wrapper above (thunk/saga internals hold the raw dispatch reference).
172
+ // Without this, the tree and per-reducer last-action drifted out of sync.
173
+ let lastSeenState = store.getState();
89
174
  store.subscribe(() => {
90
- if (globalReduxAutoRefresh) {
91
- setReduxState(store.getState());
175
+ const nextState = store.getState();
176
+ if (nextState === lastSeenState)
177
+ return;
178
+ const prevState = lastSeenState;
179
+ lastSeenState = nextState;
180
+ if (inWrappedDispatch || middlewareAttached) {
181
+ // Already recorded with proper attribution; just refresh the snapshot.
182
+ if (globalReduxAutoRefresh) {
183
+ currentReduxState = nextState;
184
+ notify();
185
+ }
186
+ return;
92
187
  }
188
+ // Change arrived outside the wrapped dispatch — record it so the
189
+ // timeline stays consistent, even without the original action type.
190
+ recordAction({ type: '@@inspector/EXTERNAL_STATE_CHANGE' }, prevState, nextState);
93
191
  });
94
192
  };
@@ -287,14 +287,19 @@ export const WebView = forwardRef((props, ref) => {
287
287
  onNavigationStateChange: handleNavigationStateChange,
288
288
  onLoadStart: handleLoadStart,
289
289
  onLoadEnd: handleLoadEnd,
290
- }), loading && showLoader && React.createElement(View, {
291
- style: {
292
- ...StyleSheet.absoluteFill,
293
- justifyContent: 'center',
294
- alignItems: 'center',
295
- backgroundColor: 'rgba(255, 255, 255, 0.4)',
296
- },
297
- }, React.createElement(ActivityIndicator, { size: 'large', color: '#684B9B' })));
290
+ }), loading &&
291
+ showLoader &&
292
+ React.createElement(View, {
293
+ style: {
294
+ ...StyleSheet.absoluteFill,
295
+ justifyContent: 'center',
296
+ alignItems: 'center',
297
+ backgroundColor: 'rgba(255, 255, 255, 0.4)',
298
+ },
299
+ }, React.createElement(ActivityIndicator, {
300
+ size: 'large',
301
+ color: '#684B9B',
302
+ })));
298
303
  });
299
304
  // Perform monkey-patching to intercept react-native-webview exports globally
300
305
  try {
@@ -0,0 +1,24 @@
1
+ export interface PersistedSettings {
2
+ isDark?: boolean;
3
+ modalHeightPercent?: number;
4
+ tabVisibility?: Record<string, boolean>;
5
+ defaultTab?: string;
6
+ maxNetworkLogs?: number;
7
+ maxConsoleLogs?: number;
8
+ showConsoleLevels?: {
9
+ info: boolean;
10
+ warn: boolean;
11
+ error: boolean;
12
+ };
13
+ webViewCaptureCssJs?: boolean;
14
+ reduxAutoRefresh?: boolean;
15
+ reduxExpandDepth?: number;
16
+ slowRequestThreshold?: number;
17
+ insightsShowConsoleAlerts?: boolean;
18
+ showDuplicateLogs?: boolean;
19
+ }
20
+ export declare function loadSettings(): Promise<PersistedSettings>;
21
+ /** Debounced save so rapid toggling doesn't hammer storage. */
22
+ export declare function saveSettings(settings: PersistedSettings): void;
23
+ export declare function clearPersistedSettings(): Promise<void>;
24
+ export declare const isPersistentStorageAvailable: () => boolean;
@@ -0,0 +1,67 @@
1
+ // #5 — Persistence layer for the inspector's settings selections.
2
+ //
3
+ // Backed by @react-native-async-storage/async-storage when the host app has it
4
+ // installed (most RN apps do), with a transparent in-memory fallback so this
5
+ // library never crashes and never forces a new native dependency.
6
+ let storage = null;
7
+ try {
8
+ // Optional dependency — resolved only if the host app already ships it.
9
+ const mod = require('@react-native-async-storage/async-storage');
10
+ storage = mod?.default ?? mod ?? null;
11
+ if (storage && typeof storage.getItem !== 'function')
12
+ storage = null;
13
+ }
14
+ catch {
15
+ storage = null;
16
+ }
17
+ // In-memory fallback (settings survive for the app session only).
18
+ const memory = new Map();
19
+ const SETTINGS_KEY = 'rn-inapp-inspector.settings.v1';
20
+ export async function loadSettings() {
21
+ try {
22
+ const raw = storage
23
+ ? await storage.getItem(SETTINGS_KEY)
24
+ : memory.get(SETTINGS_KEY) ?? null;
25
+ if (!raw)
26
+ return {};
27
+ const parsed = JSON.parse(raw);
28
+ return parsed && typeof parsed === 'object' ? parsed : {};
29
+ }
30
+ catch {
31
+ return {};
32
+ }
33
+ }
34
+ let saveTimer = null;
35
+ /** Debounced save so rapid toggling doesn't hammer storage. */
36
+ export function saveSettings(settings) {
37
+ if (saveTimer)
38
+ clearTimeout(saveTimer);
39
+ saveTimer = setTimeout(async () => {
40
+ try {
41
+ const raw = JSON.stringify(settings);
42
+ if (storage) {
43
+ await storage.setItem(SETTINGS_KEY, raw);
44
+ }
45
+ else {
46
+ memory.set(SETTINGS_KEY, raw);
47
+ }
48
+ }
49
+ catch {
50
+ // Persistence is best-effort — never crash the host app over it.
51
+ }
52
+ }, 250);
53
+ }
54
+ export async function clearPersistedSettings() {
55
+ try {
56
+ if (storage) {
57
+ await storage.removeItem(SETTINGS_KEY);
58
+ }
59
+ else {
60
+ memory.delete(SETTINGS_KEY);
61
+ }
62
+ }
63
+ catch {
64
+ // ignore
65
+ }
66
+ }
67
+ export const isPersistentStorageAvailable = () => storage != null;
@@ -6,4 +6,4 @@ export { setupConsoleLogger, clearConsoleLogs, subscribeConsoleLogs, } from './c
6
6
  export { setupAnalyticsLogger, logAnalyticsEvent, subscribeAnalyticsEvents, clearAnalyticsEvents, } from './customHooks/analyticsLogger';
7
7
  export { WebView, getWebViewLogs, getWebViewNavHistory, getWebViewHtml, getWebViewCss, getWebViewJs, getWebViewHtmlUrl, clearWebViewData, subscribeWebView, } from './customHooks/webViewLogger';
8
8
  export { default as ErrorBoundary } from './components/ErrorBoundary';
9
- export { connectReduxStore, getReduxState, subscribeReduxState, } from './customHooks/reduxLogger';
9
+ export { connectReduxStore, inspectorReduxMiddleware, getReduxState, subscribeReduxState, getActionHistory, clearActionHistory, } from './customHooks/reduxLogger';