@adventurelabs/scout-core 1.0.98 → 1.0.100

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.
@@ -42,62 +42,65 @@ export function useScoutRealtimeConnectivity(scoutSupabase) {
42
42
  if (!updatedConnectivity[deviceId]) {
43
43
  updatedConnectivity[deviceId] = {
44
44
  most_recent: connectivityData,
45
- history: [connectivityData],
45
+ history: [],
46
46
  };
47
47
  }
48
48
  else {
49
- // Create a copy of the existing historical data
50
49
  const newHistory = [
50
+ updatedConnectivity[deviceId].most_recent,
51
51
  ...updatedConnectivity[deviceId].history,
52
- connectivityData,
53
- ];
54
- // Keep only recent 100 entries
55
- const sortedHistory = newHistory
56
- .sort((a, b) => new Date(b.timestamp_start || 0).getTime() -
57
- new Date(a.timestamp_start || 0).getTime())
58
- .slice(0, 100);
52
+ ].slice(0, 99);
59
53
  updatedConnectivity[deviceId] = {
60
- most_recent: sortedHistory[0],
61
- history: sortedHistory,
54
+ most_recent: connectivityData,
55
+ history: newHistory,
62
56
  };
63
57
  }
64
58
  break;
65
59
  case "UPDATE":
66
60
  if (updatedConnectivity[deviceId]) {
67
- // Create a copy of the history array before modifying
68
- const newHistory = [...updatedConnectivity[deviceId].history];
69
- const index = newHistory.findIndex((c) => c.id === connectivityData.id);
70
- if (index >= 0) {
71
- newHistory[index] = connectivityData;
72
- // Update most_recent if this was the most recent item
73
- const sortedHistory = newHistory.sort((a, b) => new Date(b.timestamp_start || 0).getTime() -
74
- new Date(a.timestamp_start || 0).getTime());
61
+ if (updatedConnectivity[deviceId].most_recent.id ===
62
+ connectivityData.id) {
75
63
  updatedConnectivity[deviceId] = {
76
- most_recent: sortedHistory[0],
77
- history: sortedHistory,
64
+ ...updatedConnectivity[deviceId],
65
+ most_recent: connectivityData,
78
66
  };
79
67
  }
68
+ else {
69
+ const historyIndex = updatedConnectivity[deviceId].history.findIndex((c) => c.id === connectivityData.id);
70
+ if (historyIndex >= 0) {
71
+ const newHistory = [...updatedConnectivity[deviceId].history];
72
+ newHistory[historyIndex] = connectivityData;
73
+ updatedConnectivity[deviceId] = {
74
+ ...updatedConnectivity[deviceId],
75
+ history: newHistory,
76
+ };
77
+ }
78
+ }
80
79
  }
81
80
  break;
82
81
  case "DELETE":
83
82
  if (updatedConnectivity[deviceId]) {
84
- // Filter creates a new array, so this is safe
85
- const newHistory = updatedConnectivity[deviceId].history.filter((c) => c.id !== connectivityData.id);
86
- if (newHistory.length === 0) {
87
- delete updatedConnectivity[deviceId];
83
+ if (updatedConnectivity[deviceId].most_recent.id ===
84
+ connectivityData.id) {
85
+ if (updatedConnectivity[deviceId].history.length === 0) {
86
+ delete updatedConnectivity[deviceId];
87
+ }
88
+ else {
89
+ updatedConnectivity[deviceId] = {
90
+ most_recent: updatedConnectivity[deviceId].history[0],
91
+ history: updatedConnectivity[deviceId].history.slice(1),
92
+ };
93
+ }
88
94
  }
89
95
  else {
90
- const sortedHistory = newHistory.sort((a, b) => new Date(b.timestamp_start || 0).getTime() -
91
- new Date(a.timestamp_start || 0).getTime());
92
96
  updatedConnectivity[deviceId] = {
93
- most_recent: sortedHistory[0],
94
- history: sortedHistory,
97
+ ...updatedConnectivity[deviceId],
98
+ history: updatedConnectivity[deviceId].history.filter((c) => c.id !== connectivityData.id),
95
99
  };
96
100
  }
97
101
  }
98
102
  break;
99
103
  }
100
- console.log("[Connectivity] updating tracker connectivity in response to broadcast");
101
104
  dispatch(setActiveHerdGpsTrackersConnectivity(updatedConnectivity));
102
105
  }, [connectivity, dispatch]);
103
106
  // Fetch initial connectivity data
@@ -108,7 +111,6 @@ export function useScoutRealtimeConnectivity(scoutSupabase) {
108
111
  if (deviceIds.length === 0) {
109
112
  return;
110
113
  }
111
- console.log(`[Connectivity] Loading data for ${deviceIds.length} GPS trackers`);
112
114
  const timestampFilter = getDaysAgoTimestamp(1);
113
115
  const connectivityData = {};
114
116
  await Promise.all(deviceIds.map(async (deviceId) => {
@@ -123,25 +125,20 @@ export function useScoutRealtimeConnectivity(scoutSupabase) {
123
125
  .slice(0, 100);
124
126
  connectivityData[deviceId] = {
125
127
  most_recent: sortedData[0],
126
- history: sortedData,
128
+ history: sortedData.slice(1), // Exclude the most recent item
127
129
  };
128
130
  }
129
131
  }
130
- else {
131
- console.warn(`[Connectivity] API error for device ${deviceId}:`, response.msg || "Unknown error");
132
- }
133
132
  }
134
133
  catch (error) {
135
- console.warn(`[Connectivity] Failed to fetch data for device ${deviceId}:`, error);
134
+ // Silent error handling
136
135
  }
137
136
  }));
138
137
  dispatch(setActiveHerdGpsTrackersConnectivity(connectivityData));
139
- console.log(`[Connectivity] Loaded data for ${Object.keys(connectivityData).length} devices`);
140
138
  }, [gpsDeviceIds, dispatch]);
141
139
  useEffect(() => {
142
140
  if (!scoutSupabase || gpsDeviceIds === "")
143
141
  return;
144
- console.log(`[Connectivity Hook] Loading data for ${gpsDeviceIds}`);
145
142
  // Clean up existing channels
146
143
  channels.current.forEach((channel) => scoutSupabase.removeChannel(channel));
147
144
  channels.current = [];
@@ -149,14 +146,7 @@ export function useScoutRealtimeConnectivity(scoutSupabase) {
149
146
  const channel = scoutSupabase
150
147
  .channel(`${activeHerdId}-connectivity`, { config: { private: true } })
151
148
  .on("broadcast", { event: "*" }, handleConnectivityBroadcast)
152
- .subscribe((status) => {
153
- if (status === "SUBSCRIBED") {
154
- console.log(`[Connectivity] ✅ Connected to herd ${activeHerdId}`);
155
- }
156
- else if (status === "CHANNEL_ERROR") {
157
- console.warn(`[Connectivity] 🟡 Failed to connect to herd ${activeHerdId}`);
158
- }
159
- });
149
+ .subscribe();
160
150
  channels.current.push(channel);
161
151
  // Fetch initial data
162
152
  fetchInitialData();
@@ -164,11 +154,5 @@ export function useScoutRealtimeConnectivity(scoutSupabase) {
164
154
  channels.current.forEach((ch) => scoutSupabase.removeChannel(ch));
165
155
  channels.current = [];
166
156
  };
167
- }, [
168
- scoutSupabase,
169
- gpsDeviceIds,
170
- activeHerdId,
171
- handleConnectivityBroadcast,
172
- fetchInitialData,
173
- ]);
157
+ }, [scoutSupabase, gpsDeviceIds, activeHerdId, handleConnectivityBroadcast]);
174
158
  }
@@ -1,4 +1,4 @@
1
- import { HistoricalData } from "./data";
1
+ import { HistoricalData } from "./historical";
2
2
  import { IConnectivityWithCoordinates } from "./db";
3
3
  export type MapDeviceIdToConnectivity = {
4
4
  [deviceId: number]: HistoricalData<IConnectivityWithCoordinates>;
@@ -0,0 +1,4 @@
1
+ export type HistoricalData<T> = {
2
+ most_recent: T;
3
+ history: T[];
4
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -7,3 +7,4 @@ export * from "./supabase";
7
7
  export * from "./events";
8
8
  export * from "./data_source";
9
9
  export * from "./connectivity";
10
+ export * from "./historical";
@@ -7,3 +7,4 @@ export * from "./supabase";
7
7
  export * from "./events";
8
8
  export * from "./data_source";
9
9
  export * from "./connectivity";
10
+ export * from "./historical";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adventurelabs/scout-core",
3
- "version": "1.0.98",
3
+ "version": "1.0.100",
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",