@adventurelabs/scout-core 1.2.1 → 1.2.2

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.
@@ -2,3 +2,7 @@ export { useScoutRefresh, type UseScoutRefreshOptions, } from "./useScoutRefresh
2
2
  export { useScoutRealtimeConnectivity } from "./useScoutRealtimeConnectivity";
3
3
  export { useScoutRealtimeDevices } from "./useScoutRealtimeDevices";
4
4
  export { useScoutRealtimeVersionsSoftware } from "./useScoutRealtimeVersionsSoftware";
5
+ export { useScoutRealtimeEvents } from "./useScoutRealtimeEvents";
6
+ export { useScoutRealtimeTags } from "./useScoutRealtimeTags";
7
+ export { useScoutRealtimeSessions } from "./useScoutRealtimeSessions";
8
+ export { useScoutRealtimePlans } from "./useScoutRealtimePlans";
@@ -2,3 +2,7 @@ export { useScoutRefresh, } from "./useScoutRefresh";
2
2
  export { useScoutRealtimeConnectivity } from "./useScoutRealtimeConnectivity";
3
3
  export { useScoutRealtimeDevices } from "./useScoutRealtimeDevices";
4
4
  export { useScoutRealtimeVersionsSoftware } from "./useScoutRealtimeVersionsSoftware";
5
+ export { useScoutRealtimeEvents } from "./useScoutRealtimeEvents";
6
+ export { useScoutRealtimeTags } from "./useScoutRealtimeTags";
7
+ export { useScoutRealtimeSessions } from "./useScoutRealtimeSessions";
8
+ export { useScoutRealtimePlans } from "./useScoutRealtimePlans";
@@ -0,0 +1,5 @@
1
+ import { SupabaseClient } from "@supabase/supabase-js";
2
+ import { Database } from "../types/supabase";
3
+ import { IEventAndTagsPrettyLocation } from "../types/db";
4
+ import { RealtimeData } from "../types/realtime";
5
+ export declare function useScoutRealtimeEvents(scoutSupabase: SupabaseClient<Database>, shouldUpdateGlobalStateOnChanges: boolean): RealtimeData<IEventAndTagsPrettyLocation>[];
@@ -0,0 +1,96 @@
1
+ "use client";
2
+ import { useAppDispatch } from "../store/hooks";
3
+ import { useSelector } from "react-redux";
4
+ import { useEffect, useRef, useCallback, useState } from "react";
5
+ import { updateEventValuesForHerdModule } from "../store/scout";
6
+ import { EnumRealtimeOperation } from "../types/realtime";
7
+ export function useScoutRealtimeEvents(scoutSupabase, shouldUpdateGlobalStateOnChanges) {
8
+ const channels = useRef([]);
9
+ const dispatch = useAppDispatch();
10
+ const [newEventItems, setNewEventItems] = useState([]);
11
+ const activeHerdId = useSelector((state) => state.scout.active_herd_id);
12
+ // Event broadcast handler
13
+ const handleEventBroadcast = useCallback((payload) => {
14
+ console.log("[Events] Broadcast received:", payload.payload.operation);
15
+ const data = payload.payload;
16
+ const eventData = data.record || data.old_record;
17
+ if (!eventData)
18
+ return;
19
+ let operation;
20
+ // TODO: UNCOMMENT GLOBAL STORE OPERATIONS IF OKAY WITH FREQUENT
21
+ switch (data.operation) {
22
+ case "INSERT":
23
+ operation = EnumRealtimeOperation.INSERT;
24
+ if (data.record && activeHerdId && shouldUpdateGlobalStateOnChanges) {
25
+ console.log("[Events] New event received:", data.record);
26
+ // For events, we need to update the event values in the herd module
27
+ dispatch(updateEventValuesForHerdModule({
28
+ herd_id: activeHerdId,
29
+ events: [data.record],
30
+ }));
31
+ }
32
+ break;
33
+ case "UPDATE":
34
+ operation = EnumRealtimeOperation.UPDATE;
35
+ if (data.record && activeHerdId && shouldUpdateGlobalStateOnChanges) {
36
+ console.log("[Events] Event updated:", data.record);
37
+ dispatch(updateEventValuesForHerdModule({
38
+ herd_id: activeHerdId,
39
+ events: [data.record],
40
+ }));
41
+ }
42
+ break;
43
+ case "DELETE":
44
+ operation = EnumRealtimeOperation.DELETE;
45
+ if (data.old_record &&
46
+ activeHerdId &&
47
+ shouldUpdateGlobalStateOnChanges) {
48
+ console.log("[Events] Event deleted:", data.old_record);
49
+ // TODO: WRITE DELETION STORE ACTION
50
+ console.log("[Events] Event deletion detected - manual refresh may be needed");
51
+ }
52
+ break;
53
+ default:
54
+ return;
55
+ }
56
+ const realtimeData = {
57
+ data: eventData,
58
+ operation,
59
+ };
60
+ console.log(`[scout-core realtime] EVENT ${data.operation} received:`, JSON.stringify(realtimeData));
61
+ setNewEventItems((prev) => [realtimeData, ...prev]);
62
+ }, [dispatch, activeHerdId]);
63
+ // Clear new items when herd changes
64
+ const clearNewItems = useCallback(() => {
65
+ setNewEventItems([]);
66
+ }, []);
67
+ const cleanupChannels = () => {
68
+ channels.current.forEach((channel) => scoutSupabase.removeChannel(channel));
69
+ channels.current = [];
70
+ };
71
+ const createEventsChannel = (herdId) => {
72
+ return scoutSupabase
73
+ .channel(`${herdId}-events`, { config: { private: true } })
74
+ .on("broadcast", { event: "*" }, handleEventBroadcast)
75
+ .subscribe((status) => {
76
+ if (status === "SUBSCRIBED") {
77
+ console.log(`[Events] ✅ Connected to herd ${herdId}`);
78
+ }
79
+ else if (status === "CHANNEL_ERROR") {
80
+ console.warn(`[Events] 🟡 Failed to connect to herd ${herdId}`);
81
+ }
82
+ });
83
+ };
84
+ useEffect(() => {
85
+ cleanupChannels();
86
+ // Clear previous items when switching herds
87
+ clearNewItems();
88
+ // Create events channel for active herd
89
+ if (activeHerdId) {
90
+ const channel = createEventsChannel(activeHerdId);
91
+ channels.current.push(channel);
92
+ }
93
+ return cleanupChannels;
94
+ }, [activeHerdId, clearNewItems]);
95
+ return newEventItems;
96
+ }
@@ -0,0 +1,5 @@
1
+ import { SupabaseClient } from "@supabase/supabase-js";
2
+ import { Database } from "../types/supabase";
3
+ import { IPlan } from "../types/db";
4
+ import { RealtimeData } from "../types/realtime";
5
+ export declare function useScoutRealtimePlans(scoutSupabase: SupabaseClient<Database>, shouldUpdateGlobalStateOnChanges: boolean): RealtimeData<IPlan>[];
@@ -0,0 +1,83 @@
1
+ "use client";
2
+ import { useAppDispatch } from "../store/hooks";
3
+ import { useSelector } from "react-redux";
4
+ import { useEffect, useRef, useCallback, useState } from "react";
5
+ import { addPlan, deletePlan, updatePlan } from "../store/scout";
6
+ import { EnumRealtimeOperation } from "../types/realtime";
7
+ export function useScoutRealtimePlans(scoutSupabase, shouldUpdateGlobalStateOnChanges) {
8
+ const channels = useRef([]);
9
+ const dispatch = useAppDispatch();
10
+ const [newPlanItems, setNewPlanItems] = useState([]);
11
+ const activeHerdId = useSelector((state) => state.scout.active_herd_id);
12
+ // Plan broadcast handler
13
+ const handlePlanBroadcast = useCallback((payload) => {
14
+ console.log("[Plans] Broadcast received:", payload.payload.operation);
15
+ const data = payload.payload;
16
+ const planData = data.record || data.old_record;
17
+ if (!planData)
18
+ return;
19
+ let operation;
20
+ switch (data.operation) {
21
+ case "INSERT":
22
+ operation = EnumRealtimeOperation.INSERT;
23
+ if (data.record && shouldUpdateGlobalStateOnChanges) {
24
+ console.log("[Plans] New plan received:", data.record);
25
+ dispatch(addPlan(data.record));
26
+ }
27
+ break;
28
+ case "UPDATE":
29
+ operation = EnumRealtimeOperation.UPDATE;
30
+ if (data.record && shouldUpdateGlobalStateOnChanges) {
31
+ dispatch(updatePlan(data.record));
32
+ }
33
+ break;
34
+ case "DELETE":
35
+ operation = EnumRealtimeOperation.DELETE;
36
+ if (data.old_record && shouldUpdateGlobalStateOnChanges) {
37
+ dispatch(deletePlan(data.old_record));
38
+ }
39
+ break;
40
+ default:
41
+ return;
42
+ }
43
+ const realtimeData = {
44
+ data: planData,
45
+ operation,
46
+ };
47
+ console.log(`[scout-core realtime] PLAN ${data.operation} received:`, JSON.stringify(realtimeData));
48
+ setNewPlanItems((prev) => [realtimeData, ...prev]);
49
+ }, [dispatch]);
50
+ // Clear new items when herd changes
51
+ const clearNewItems = useCallback(() => {
52
+ setNewPlanItems([]);
53
+ }, []);
54
+ const cleanupChannels = () => {
55
+ channels.current.forEach((channel) => scoutSupabase.removeChannel(channel));
56
+ channels.current = [];
57
+ };
58
+ const createPlansChannel = (herdId) => {
59
+ return scoutSupabase
60
+ .channel(`${herdId}-plans`, { config: { private: true } })
61
+ .on("broadcast", { event: "*" }, handlePlanBroadcast)
62
+ .subscribe((status) => {
63
+ if (status === "SUBSCRIBED") {
64
+ console.log(`[Plans] ✅ Connected to herd ${herdId}`);
65
+ }
66
+ else if (status === "CHANNEL_ERROR") {
67
+ console.warn(`[Plans] 🟡 Failed to connect to herd ${herdId}`);
68
+ }
69
+ });
70
+ };
71
+ useEffect(() => {
72
+ cleanupChannels();
73
+ // Clear previous items when switching herds
74
+ clearNewItems();
75
+ // Create plans channel for active herd
76
+ if (activeHerdId) {
77
+ const channel = createPlansChannel(activeHerdId);
78
+ channels.current.push(channel);
79
+ }
80
+ return cleanupChannels;
81
+ }, [activeHerdId, clearNewItems]);
82
+ return newPlanItems;
83
+ }
@@ -0,0 +1,5 @@
1
+ import { SupabaseClient } from "@supabase/supabase-js";
2
+ import { Database } from "../types/supabase";
3
+ import { ISessionWithCoordinates } from "../types/db";
4
+ import { RealtimeData } from "../types/realtime";
5
+ export declare function useScoutRealtimeSessions(scoutSupabase: SupabaseClient<Database>, shouldUpdateGlobalStateOnChanges: boolean): RealtimeData<ISessionWithCoordinates>[];
@@ -0,0 +1,83 @@
1
+ "use client";
2
+ import { useAppDispatch } from "../store/hooks";
3
+ import { useSelector } from "react-redux";
4
+ import { useEffect, useRef, useCallback, useState } from "react";
5
+ import { addSessionToStore, deleteSessionFromStore, updateSessionInStore, } from "../store/scout";
6
+ import { EnumRealtimeOperation } from "../types/realtime";
7
+ export function useScoutRealtimeSessions(scoutSupabase, shouldUpdateGlobalStateOnChanges) {
8
+ const channels = useRef([]);
9
+ const dispatch = useAppDispatch();
10
+ const [newSessionItems, setNewSessionItems] = useState([]);
11
+ const activeHerdId = useSelector((state) => state.scout.active_herd_id);
12
+ // Session broadcast handler
13
+ const handleSessionBroadcast = useCallback((payload) => {
14
+ console.log("[Sessions] Broadcast received:", payload.payload.operation);
15
+ const data = payload.payload;
16
+ const sessionData = data.record || data.old_record;
17
+ if (!sessionData)
18
+ return;
19
+ let operation;
20
+ switch (data.operation) {
21
+ case "INSERT":
22
+ operation = EnumRealtimeOperation.INSERT;
23
+ if (data.record && shouldUpdateGlobalStateOnChanges) {
24
+ console.log("[Sessions] New session received:", data.record);
25
+ dispatch(addSessionToStore(data.record));
26
+ }
27
+ break;
28
+ case "UPDATE":
29
+ operation = EnumRealtimeOperation.UPDATE;
30
+ if (data.record && shouldUpdateGlobalStateOnChanges) {
31
+ dispatch(updateSessionInStore(data.record));
32
+ }
33
+ break;
34
+ case "DELETE":
35
+ operation = EnumRealtimeOperation.DELETE;
36
+ if (data.old_record && shouldUpdateGlobalStateOnChanges) {
37
+ dispatch(deleteSessionFromStore(data.old_record));
38
+ }
39
+ break;
40
+ default:
41
+ return;
42
+ }
43
+ const realtimeData = {
44
+ data: sessionData,
45
+ operation,
46
+ };
47
+ console.log(`[scout-core realtime] SESSION ${data.operation} received:`, JSON.stringify(realtimeData));
48
+ setNewSessionItems((prev) => [realtimeData, ...prev]);
49
+ }, [dispatch]);
50
+ // Clear new items when herd changes
51
+ const clearNewItems = useCallback(() => {
52
+ setNewSessionItems([]);
53
+ }, []);
54
+ const cleanupChannels = () => {
55
+ channels.current.forEach((channel) => scoutSupabase.removeChannel(channel));
56
+ channels.current = [];
57
+ };
58
+ const createSessionsChannel = (herdId) => {
59
+ return scoutSupabase
60
+ .channel(`${herdId}-sessions`, { config: { private: true } })
61
+ .on("broadcast", { event: "*" }, handleSessionBroadcast)
62
+ .subscribe((status) => {
63
+ if (status === "SUBSCRIBED") {
64
+ console.log(`[Sessions] ✅ Connected to herd ${herdId}`);
65
+ }
66
+ else if (status === "CHANNEL_ERROR") {
67
+ console.warn(`[Sessions] 🟡 Failed to connect to herd ${herdId}`);
68
+ }
69
+ });
70
+ };
71
+ useEffect(() => {
72
+ cleanupChannels();
73
+ // Clear previous items when switching herds
74
+ clearNewItems();
75
+ // Create sessions channel for active herd
76
+ if (activeHerdId) {
77
+ const channel = createSessionsChannel(activeHerdId);
78
+ channels.current.push(channel);
79
+ }
80
+ return cleanupChannels;
81
+ }, [activeHerdId, clearNewItems]);
82
+ return newSessionItems;
83
+ }
@@ -0,0 +1,5 @@
1
+ import { SupabaseClient } from "@supabase/supabase-js";
2
+ import { Database } from "../types/supabase";
3
+ import { ITagPrettyLocation } from "../types/db";
4
+ import { RealtimeData } from "../types/realtime";
5
+ export declare function useScoutRealtimeTags(scoutSupabase: SupabaseClient<Database>, shouldUpdateGlobalStateOnChanges: boolean): RealtimeData<ITagPrettyLocation>[];
@@ -0,0 +1,85 @@
1
+ "use client";
2
+ import { useAppDispatch } from "../store/hooks";
3
+ import { useSelector } from "react-redux";
4
+ import { useEffect, useRef, useCallback, useState } from "react";
5
+ import { addTag, deleteTag, updateTag } from "../store/scout";
6
+ import { EnumRealtimeOperation } from "../types/realtime";
7
+ export function useScoutRealtimeTags(scoutSupabase, shouldUpdateGlobalStateOnChanges) {
8
+ const channels = useRef([]);
9
+ const dispatch = useAppDispatch();
10
+ const [newTagItems, setNewTagItems] = useState([]);
11
+ const activeHerdId = useSelector((state) => state.scout.active_herd_id);
12
+ // Tag broadcast handler
13
+ const handleTagBroadcast = useCallback((payload) => {
14
+ console.log("[Tags] Broadcast received:", payload.payload.operation);
15
+ const data = payload.payload;
16
+ const tagData = data.record || data.old_record;
17
+ if (!tagData)
18
+ return;
19
+ let operation;
20
+ switch (data.operation) {
21
+ case "INSERT":
22
+ operation = EnumRealtimeOperation.INSERT;
23
+ if (data.record && shouldUpdateGlobalStateOnChanges) {
24
+ console.log("[Tags] New tag received:", data.record);
25
+ dispatch(addTag(data.record));
26
+ }
27
+ break;
28
+ case "UPDATE":
29
+ operation = EnumRealtimeOperation.UPDATE;
30
+ if (data.record && shouldUpdateGlobalStateOnChanges) {
31
+ console.log("[Tags] Tag updated:", data.record);
32
+ dispatch(updateTag(data.record));
33
+ }
34
+ break;
35
+ case "DELETE":
36
+ operation = EnumRealtimeOperation.DELETE;
37
+ if (data.old_record && shouldUpdateGlobalStateOnChanges) {
38
+ console.log("[Tags] Tag deleted:", data.old_record);
39
+ dispatch(deleteTag(data.old_record));
40
+ }
41
+ break;
42
+ default:
43
+ return;
44
+ }
45
+ const realtimeData = {
46
+ data: tagData,
47
+ operation,
48
+ };
49
+ console.log(`[scout-core realtime] TAG ${data.operation} received:`, JSON.stringify(realtimeData));
50
+ setNewTagItems((prev) => [realtimeData, ...prev]);
51
+ }, [dispatch]);
52
+ // Clear new items when herd changes
53
+ const clearNewItems = useCallback(() => {
54
+ setNewTagItems([]);
55
+ }, []);
56
+ const cleanupChannels = () => {
57
+ channels.current.forEach((channel) => scoutSupabase.removeChannel(channel));
58
+ channels.current = [];
59
+ };
60
+ const createTagsChannel = (herdId) => {
61
+ return scoutSupabase
62
+ .channel(`${herdId}-tags`, { config: { private: true } })
63
+ .on("broadcast", { event: "*" }, handleTagBroadcast)
64
+ .subscribe((status) => {
65
+ if (status === "SUBSCRIBED") {
66
+ console.log(`[Tags] ✅ Connected to herd ${herdId}`);
67
+ }
68
+ else if (status === "CHANNEL_ERROR") {
69
+ console.warn(`[Tags] 🟡 Failed to connect to herd ${herdId}`);
70
+ }
71
+ });
72
+ };
73
+ useEffect(() => {
74
+ cleanupChannels();
75
+ // Clear previous items when switching herds
76
+ clearNewItems();
77
+ // Create tags channel for active herd
78
+ if (activeHerdId) {
79
+ const channel = createTagsChannel(activeHerdId);
80
+ channels.current.push(channel);
81
+ }
82
+ return cleanupChannels;
83
+ }, [activeHerdId, clearNewItems]);
84
+ return newTagItems;
85
+ }
package/dist/index.d.ts CHANGED
@@ -41,6 +41,10 @@ export * from "./helpers/components";
41
41
  export * from "./hooks/useScoutRealtimeConnectivity";
42
42
  export * from "./hooks/useScoutRealtimeDevices";
43
43
  export * from "./hooks/useScoutRealtimeVersionsSoftware";
44
+ export * from "./hooks/useScoutRealtimeEvents";
45
+ export * from "./hooks/useScoutRealtimeTags";
46
+ export * from "./hooks/useScoutRealtimeSessions";
47
+ export * from "./hooks/useScoutRealtimePlans";
44
48
  export * from "./hooks/useScoutRefresh";
45
49
  export * from "./providers";
46
50
  export * from "./store/scout";
package/dist/index.js CHANGED
@@ -45,6 +45,10 @@ export * from "./helpers/components";
45
45
  export * from "./hooks/useScoutRealtimeConnectivity";
46
46
  export * from "./hooks/useScoutRealtimeDevices";
47
47
  export * from "./hooks/useScoutRealtimeVersionsSoftware";
48
+ export * from "./hooks/useScoutRealtimeEvents";
49
+ export * from "./hooks/useScoutRealtimeTags";
50
+ export * from "./hooks/useScoutRealtimeSessions";
51
+ export * from "./hooks/useScoutRealtimePlans";
48
52
  export * from "./hooks/useScoutRefresh";
49
53
  // Providers
50
54
  export * from "./providers";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adventurelabs/scout-core",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
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",