@adventurelabs/scout-core 1.4.53 → 1.4.54
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.
|
@@ -6,26 +6,24 @@ export function useScoutRealtimePlans(scoutSupabase) {
|
|
|
6
6
|
const channels = useRef([]);
|
|
7
7
|
const [latestPlanUpdate, setLatestPlanUpdate] = useState(null);
|
|
8
8
|
const activeHerdId = useSelector((state) => state.scout.active_herd_id);
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
const handlePlanBroadcast = useCallback((message) => {
|
|
10
|
+
const { payload: data } = message;
|
|
11
|
+
if (data.table !== "plans" || data.schema !== "public") {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
const planData = data.record ?? data.old_record;
|
|
14
15
|
if (!planData)
|
|
15
16
|
return;
|
|
16
17
|
let operation;
|
|
17
18
|
switch (data.operation) {
|
|
18
19
|
case "INSERT":
|
|
19
20
|
operation = EnumRealtimeOperation.INSERT;
|
|
20
|
-
console.log("[Plans] New plan received:", data.record);
|
|
21
21
|
break;
|
|
22
22
|
case "UPDATE":
|
|
23
23
|
operation = EnumRealtimeOperation.UPDATE;
|
|
24
|
-
console.log("[Plans] Plan updated:", data.record);
|
|
25
24
|
break;
|
|
26
25
|
case "DELETE":
|
|
27
26
|
operation = EnumRealtimeOperation.DELETE;
|
|
28
|
-
console.log("[Plans] Plan deleted:", data.old_record);
|
|
29
27
|
break;
|
|
30
28
|
default:
|
|
31
29
|
return;
|
|
@@ -34,40 +32,35 @@ export function useScoutRealtimePlans(scoutSupabase) {
|
|
|
34
32
|
data: planData,
|
|
35
33
|
operation,
|
|
36
34
|
};
|
|
37
|
-
console.log(`[scout-core realtime] PLAN ${data.operation} received:`, JSON.stringify(realtimeData));
|
|
38
35
|
setLatestPlanUpdate(realtimeData);
|
|
39
36
|
}, []);
|
|
40
|
-
// Clear latest update
|
|
41
37
|
const clearLatestUpdate = useCallback(() => {
|
|
42
38
|
setLatestPlanUpdate(null);
|
|
43
39
|
}, []);
|
|
44
|
-
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
if (!scoutSupabase)
|
|
42
|
+
return;
|
|
45
43
|
channels.current.forEach((channel) => scoutSupabase.removeChannel(channel));
|
|
46
44
|
channels.current = [];
|
|
47
|
-
};
|
|
48
|
-
const createPlansChannel = (herdId) => {
|
|
49
|
-
return scoutSupabase
|
|
50
|
-
.channel(`${herdId}-plans`, { config: { private: true } })
|
|
51
|
-
.on("broadcast", { event: "*" }, handlePlanBroadcast)
|
|
52
|
-
.subscribe((status) => {
|
|
53
|
-
if (status === "SUBSCRIBED") {
|
|
54
|
-
console.log(`[Plans] ✅ Connected to herd ${herdId}`);
|
|
55
|
-
}
|
|
56
|
-
else if (status === "CHANNEL_ERROR") {
|
|
57
|
-
console.warn(`[Plans] 🟡 Failed to connect to herd ${herdId}`);
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
};
|
|
61
|
-
useEffect(() => {
|
|
62
|
-
cleanupChannels();
|
|
63
|
-
// Clear previous update when switching herds
|
|
64
45
|
clearLatestUpdate();
|
|
65
|
-
// Create plans channel for active herd
|
|
66
46
|
if (activeHerdId) {
|
|
67
|
-
const channel =
|
|
47
|
+
const channel = scoutSupabase
|
|
48
|
+
.channel(`${activeHerdId}-plans`, { config: { private: true } })
|
|
49
|
+
.on("broadcast", { event: "*" }, handlePlanBroadcast)
|
|
50
|
+
.subscribe((status) => {
|
|
51
|
+
if (status === "SUBSCRIBED") {
|
|
52
|
+
console.log(`[Plans] ✅ Connected to herd ${activeHerdId}`);
|
|
53
|
+
}
|
|
54
|
+
else if (status === "CHANNEL_ERROR") {
|
|
55
|
+
console.warn(`[Plans] 🟡 Failed to connect to herd ${activeHerdId}`);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
68
58
|
channels.current.push(channel);
|
|
69
59
|
}
|
|
70
|
-
return
|
|
71
|
-
|
|
60
|
+
return () => {
|
|
61
|
+
channels.current.forEach((ch) => scoutSupabase.removeChannel(ch));
|
|
62
|
+
channels.current = [];
|
|
63
|
+
};
|
|
64
|
+
}, [scoutSupabase, activeHerdId, handlePlanBroadcast, clearLatestUpdate]);
|
|
72
65
|
return [latestPlanUpdate, clearLatestUpdate];
|
|
73
66
|
}
|