@adventurelabs/scout-core 1.3.2 → 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/dist/store/api.js CHANGED
@@ -1,5 +1,10 @@
1
1
  import { createApi, fakeBaseQuery } from "@reduxjs/toolkit/query/react";
2
2
  import { generateSignedUrlsBatch } from "../helpers/storage";
3
+ // Custom serialize function to exclude supabase client
4
+ const serializeQueryArgs = ({ queryArgs, endpointDefinition, endpointName, }) => {
5
+ const { supabase, ...serializableArgs } = queryArgs;
6
+ return JSON.stringify({ endpointName, args: serializableArgs });
7
+ };
3
8
  // Create the API slice
4
9
  export const scoutApi = createApi({
5
10
  reducerPath: "scoutApi",
@@ -10,6 +15,7 @@ export const scoutApi = createApi({
10
15
  // SESSIONS INFINITE QUERIES
11
16
  // =====================================================
12
17
  getSessionsInfiniteByHerd: builder.query({
18
+ serializeQueryArgs,
13
19
  async queryFn({ herdId, limit = 20, cursor, supabase }) {
14
20
  try {
15
21
  if (!herdId) {
@@ -61,6 +67,7 @@ export const scoutApi = createApi({
61
67
  : [{ type: "Session", id: "LIST" }],
62
68
  }),
63
69
  getSessionsInfiniteByDevice: builder.query({
70
+ serializeQueryArgs,
64
71
  async queryFn({ deviceId, limit = 20, cursor, supabase }) {
65
72
  try {
66
73
  if (!deviceId) {
@@ -115,6 +122,7 @@ export const scoutApi = createApi({
115
122
  // EVENTS INFINITE QUERIES
116
123
  // =====================================================
117
124
  getEventsInfiniteByHerd: builder.query({
125
+ serializeQueryArgs,
118
126
  async queryFn({ herdId, limit = 20, cursor, supabase }) {
119
127
  try {
120
128
  if (!herdId) {
@@ -166,6 +174,7 @@ export const scoutApi = createApi({
166
174
  : [{ type: "Event", id: "LIST" }],
167
175
  }),
168
176
  getEventsInfiniteByDevice: builder.query({
177
+ serializeQueryArgs,
169
178
  async queryFn({ deviceId, limit = 20, cursor, supabase }) {
170
179
  try {
171
180
  if (!deviceId) {
@@ -220,6 +229,7 @@ export const scoutApi = createApi({
220
229
  // ARTIFACTS INFINITE QUERIES
221
230
  // =====================================================
222
231
  getArtifactsInfiniteByHerd: builder.query({
232
+ serializeQueryArgs,
223
233
  async queryFn({ herdId, limit = 20, cursor, supabase }) {
224
234
  try {
225
235
  if (!herdId) {
@@ -296,6 +306,7 @@ export const scoutApi = createApi({
296
306
  : [{ type: "Artifact", id: "LIST" }],
297
307
  }),
298
308
  getArtifactsInfiniteByDevice: builder.query({
309
+ serializeQueryArgs,
299
310
  async queryFn({ deviceId, limit = 20, cursor, supabase }) {
300
311
  try {
301
312
  if (!deviceId) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adventurelabs/scout-core",
3
- "version": "1.3.2",
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",