@adventurelabs/scout-core
Version:
Core utilities and helpers for Adventure Labs Scout applications
56 lines (55 loc) • 2.16 kB
JavaScript
"use client";
import { useAppDispatch } from "../store/hooks";
import { useSelector } from "react-redux";
import { useEffect, useRef, useCallback } from "react";
import { addDevice, deleteDevice, updateDevice } from "../store/scout";
export function useScoutRealtimeDevices(scoutSupabase) {
const channels = useRef([]);
const dispatch = useAppDispatch();
const activeHerdId = useSelector((state) => state.scout.active_herd_id);
// Device broadcast handler
const handleDeviceBroadcast = useCallback((payload) => {
console.log("[Devices] Broadcast received:", payload.payload.operation);
const data = payload.payload;
switch (data.operation) {
case "INSERT":
if (data.record)
dispatch(addDevice(data.record));
break;
case "UPDATE":
if (data.record)
dispatch(updateDevice(data.record));
break;
case "DELETE":
if (data.old_record)
dispatch(deleteDevice(data.old_record));
break;
}
}, [dispatch]);
const cleanupChannels = () => {
channels.current.forEach((channel) => scoutSupabase.removeChannel(channel));
channels.current = [];
};
const createDevicesChannel = (herdId) => {
return scoutSupabase
.channel(`${herdId}-devices`, { config: { private: true } })
.on("broadcast", { event: "*" }, handleDeviceBroadcast)
.subscribe((status) => {
if (status === "SUBSCRIBED") {
console.log(`[Devices] ✅ Connected to herd ${herdId}`);
}
else if (status === "CHANNEL_ERROR") {
console.warn(`[Devices] 🟡 Failed to connect to herd ${herdId}`);
}
});
};
useEffect(() => {
cleanupChannels();
// Create devices channel for active herd
if (activeHerdId) {
const channel = createDevicesChannel(activeHerdId);
channels.current.push(channel);
}
return cleanupChannels;
}, [activeHerdId]);
}