@adventurelabs/scout-core 1.3.3 → 1.3.4

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.
@@ -1,4 +1,4 @@
1
- import { useState, useCallback, useMemo, useEffect } from "react";
1
+ import { useState, useCallback, useMemo, useEffect, useRef } from "react";
2
2
  import { useGetSessionsInfiniteByHerdQuery, useGetSessionsInfiniteByDeviceQuery, useGetEventsInfiniteByHerdQuery, useGetEventsInfiniteByDeviceQuery, useGetArtifactsInfiniteByHerdQuery, useGetArtifactsInfiniteByDeviceQuery, } from "../store/api";
3
3
  // =====================================================
4
4
  // SESSIONS INFINITE SCROLL HOOKS
@@ -6,14 +6,26 @@ import { useGetSessionsInfiniteByHerdQuery, useGetSessionsInfiniteByDeviceQuery,
6
6
  export const useInfiniteSessionsByHerd = (herdId, options) => {
7
7
  const [pages, setPages] = useState([]);
8
8
  const [currentCursor, setCurrentCursor] = useState(null);
9
+ const prevHerdIdRef = useRef();
9
10
  const currentQuery = useGetSessionsInfiniteByHerdQuery({
10
11
  herdId,
11
12
  limit: options.limit || 20,
12
13
  cursor: currentCursor,
13
14
  supabase: options.supabase,
14
15
  }, {
15
- skip: !options.enabled,
16
+ skip: !options.enabled || !herdId,
16
17
  });
18
+ // Reset state when herdId changes
19
+ useEffect(() => {
20
+ if (prevHerdIdRef.current !== undefined &&
21
+ prevHerdIdRef.current !== herdId &&
22
+ options.enabled &&
23
+ herdId) {
24
+ setPages([]);
25
+ setCurrentCursor(null);
26
+ }
27
+ prevHerdIdRef.current = herdId;
28
+ }, [herdId, options.enabled]);
17
29
  // Update pages when new data arrives
18
30
  useEffect(() => {
19
31
  if (currentQuery.data && !currentQuery.isLoading) {
@@ -62,14 +74,26 @@ export const useInfiniteSessionsByHerd = (herdId, options) => {
62
74
  export const useInfiniteSessionsByDevice = (deviceId, options) => {
63
75
  const [pages, setPages] = useState([]);
64
76
  const [currentCursor, setCurrentCursor] = useState(null);
77
+ const prevDeviceIdRef = useRef();
65
78
  const currentQuery = useGetSessionsInfiniteByDeviceQuery({
66
79
  deviceId,
67
80
  limit: options.limit || 20,
68
81
  cursor: currentCursor,
69
82
  supabase: options.supabase,
70
83
  }, {
71
- skip: !options.enabled,
84
+ skip: !options.enabled || !deviceId,
72
85
  });
86
+ // Reset state when deviceId changes
87
+ useEffect(() => {
88
+ if (prevDeviceIdRef.current !== undefined &&
89
+ prevDeviceIdRef.current !== deviceId &&
90
+ options.enabled &&
91
+ deviceId) {
92
+ setPages([]);
93
+ setCurrentCursor(null);
94
+ }
95
+ prevDeviceIdRef.current = deviceId;
96
+ }, [deviceId, options.enabled]);
73
97
  useEffect(() => {
74
98
  if (currentQuery.data && !currentQuery.isLoading) {
75
99
  setPages((prev) => {
@@ -119,14 +143,26 @@ export const useInfiniteSessionsByDevice = (deviceId, options) => {
119
143
  export const useInfiniteEventsByHerd = (herdId, options) => {
120
144
  const [pages, setPages] = useState([]);
121
145
  const [currentCursor, setCurrentCursor] = useState(null);
146
+ const prevHerdIdRef = useRef();
122
147
  const currentQuery = useGetEventsInfiniteByHerdQuery({
123
148
  herdId,
124
149
  limit: options.limit || 20,
125
150
  cursor: currentCursor,
126
151
  supabase: options.supabase,
127
152
  }, {
128
- skip: !options.enabled,
153
+ skip: !options.enabled || !herdId,
129
154
  });
155
+ // Reset state when herdId changes
156
+ useEffect(() => {
157
+ if (prevHerdIdRef.current !== undefined &&
158
+ prevHerdIdRef.current !== herdId &&
159
+ options.enabled &&
160
+ herdId) {
161
+ setPages([]);
162
+ setCurrentCursor(null);
163
+ }
164
+ prevHerdIdRef.current = herdId;
165
+ }, [herdId, options.enabled]);
130
166
  useEffect(() => {
131
167
  if (currentQuery.data && !currentQuery.isLoading) {
132
168
  setPages((prev) => {
@@ -173,14 +209,26 @@ export const useInfiniteEventsByHerd = (herdId, options) => {
173
209
  export const useInfiniteEventsByDevice = (deviceId, options) => {
174
210
  const [pages, setPages] = useState([]);
175
211
  const [currentCursor, setCurrentCursor] = useState(null);
212
+ const prevDeviceIdRef = useRef();
176
213
  const currentQuery = useGetEventsInfiniteByDeviceQuery({
177
214
  deviceId,
178
215
  limit: options.limit || 20,
179
216
  cursor: currentCursor,
180
217
  supabase: options.supabase,
181
218
  }, {
182
- skip: !options.enabled,
219
+ skip: !options.enabled || !deviceId,
183
220
  });
221
+ // Reset state when deviceId changes
222
+ useEffect(() => {
223
+ if (prevDeviceIdRef.current !== undefined &&
224
+ prevDeviceIdRef.current !== deviceId &&
225
+ options.enabled &&
226
+ deviceId) {
227
+ setPages([]);
228
+ setCurrentCursor(null);
229
+ }
230
+ prevDeviceIdRef.current = deviceId;
231
+ }, [deviceId, options.enabled]);
184
232
  useEffect(() => {
185
233
  if (currentQuery.data && !currentQuery.isLoading) {
186
234
  setPages((prev) => {
@@ -230,14 +278,26 @@ export const useInfiniteEventsByDevice = (deviceId, options) => {
230
278
  export const useInfiniteArtifactsByHerd = (herdId, options) => {
231
279
  const [pages, setPages] = useState([]);
232
280
  const [currentCursor, setCurrentCursor] = useState(null);
281
+ const prevHerdIdRef = useRef();
233
282
  const currentQuery = useGetArtifactsInfiniteByHerdQuery({
234
283
  herdId,
235
284
  limit: options.limit || 20,
236
285
  cursor: currentCursor,
237
286
  supabase: options.supabase,
238
287
  }, {
239
- skip: !options.enabled,
288
+ skip: !options.enabled || !herdId,
240
289
  });
290
+ // Reset state when herdId changes
291
+ useEffect(() => {
292
+ if (prevHerdIdRef.current !== undefined &&
293
+ prevHerdIdRef.current !== herdId &&
294
+ options.enabled &&
295
+ herdId) {
296
+ setPages([]);
297
+ setCurrentCursor(null);
298
+ }
299
+ prevHerdIdRef.current = herdId;
300
+ }, [herdId, options.enabled]);
241
301
  useEffect(() => {
242
302
  if (currentQuery.data && !currentQuery.isLoading) {
243
303
  setPages((prev) => {
@@ -284,14 +344,26 @@ export const useInfiniteArtifactsByHerd = (herdId, options) => {
284
344
  export const useInfiniteArtifactsByDevice = (deviceId, options) => {
285
345
  const [pages, setPages] = useState([]);
286
346
  const [currentCursor, setCurrentCursor] = useState(null);
347
+ const prevDeviceIdRef = useRef();
287
348
  const currentQuery = useGetArtifactsInfiniteByDeviceQuery({
288
349
  deviceId,
289
350
  limit: options.limit || 20,
290
351
  cursor: currentCursor,
291
352
  supabase: options.supabase,
292
353
  }, {
293
- skip: !options.enabled,
354
+ skip: !options.enabled || !deviceId,
294
355
  });
356
+ // Reset state when deviceId changes
357
+ useEffect(() => {
358
+ if (prevDeviceIdRef.current !== undefined &&
359
+ prevDeviceIdRef.current !== deviceId &&
360
+ options.enabled &&
361
+ deviceId) {
362
+ setPages([]);
363
+ setCurrentCursor(null);
364
+ }
365
+ prevDeviceIdRef.current = deviceId;
366
+ }, [deviceId, options.enabled]);
295
367
  useEffect(() => {
296
368
  if (currentQuery.data && !currentQuery.isLoading) {
297
369
  setPages((prev) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adventurelabs/scout-core",
3
- "version": "1.3.3",
3
+ "version": "1.3.4",
4
4
  "description": "Core utilities and helpers for Adventure Labs Scout applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",