@adventurelabs/scout-core 1.0.22 → 1.0.23

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,3 +1,3 @@
1
1
  import { SupabaseClient } from "@supabase/supabase-js";
2
2
  import { Database } from "../types/supabase";
3
- export declare function useScoutDbListener(supabaseClient: SupabaseClient<Database>): void;
3
+ export declare function useScoutDbListener(scoutSupabase: SupabaseClient<Database>): void;
@@ -2,38 +2,26 @@
2
2
  import { useAppDispatch } from "../store/hooks";
3
3
  import { useEffect, useRef } from "react";
4
4
  import { addDevice, addPlan, addTag, deleteDevice, deletePlan, deleteTag, updateDevice, updatePlan, updateTag, } from "../store/scout";
5
- import { get_device_by_id } from "../helpers/devices";
6
- import { EnumWebResponse } from "../types/requests";
7
- export function useScoutDbListener(supabaseClient) {
8
- const url = process.env.NEXT_PUBLIC_SUPABASE_URL || "";
9
- const anon_key = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || "";
5
+ export function useScoutDbListener(scoutSupabase) {
10
6
  const supabase = useRef(null);
7
+ const channels = useRef([]);
11
8
  const dispatch = useAppDispatch();
12
- // Add instance tracking
13
- const instanceId = useRef(Math.random().toString(36).substr(2, 9));
14
- console.log(`[DB Listener] Hook initialized - Instance ID: ${instanceId.current}`);
15
9
  function handleTagInserts(payload) {
16
- console.log(`[DB Listener] Tag INSERT received (Instance ${instanceId.current}):`, payload.new);
10
+ console.log("[DB Listener] Tag INSERT received:", payload.new);
17
11
  dispatch(addTag(payload.new));
18
12
  }
19
13
  function handleTagDeletes(payload) {
20
- console.log(`[DB Listener] Tag DELETE received (Instance ${instanceId.current}):`, payload.old);
14
+ console.log("[DB Listener] Tag DELETE received:", payload.old);
21
15
  dispatch(deleteTag(payload.old));
22
16
  }
23
17
  function handleTagUpdates(payload) {
24
- console.log(`[DB Listener] Tag UPDATE received (Instance ${instanceId.current}):`, payload.new);
18
+ console.log("[DB Listener] Tag UPDATE received:", payload.new);
25
19
  dispatch(updateTag(payload.new));
26
20
  }
27
21
  async function handleDeviceInserts(payload) {
28
22
  console.log("[DB Listener] Device INSERT received:", payload.new);
29
- const response_device = await get_device_by_id(payload.new.id);
30
- if (response_device.status == EnumWebResponse.SUCCESS &&
31
- response_device.data) {
32
- dispatch(addDevice(response_device.data));
33
- }
34
- else {
35
- console.error("error getting device by id", payload);
36
- }
23
+ // For now, just dispatch the raw payload since we don't have the device helper
24
+ dispatch(addDevice(payload.new));
37
25
  }
38
26
  function handleDeviceDeletes(payload) {
39
27
  console.log("[DB Listener] Device DELETE received:", payload.old);
@@ -41,14 +29,8 @@ export function useScoutDbListener(supabaseClient) {
41
29
  }
42
30
  async function handleDeviceUpdates(payload) {
43
31
  console.log("[DB Listener] Device UPDATE received:", payload.new);
44
- const response_device = await get_device_by_id(payload.new.id);
45
- if (response_device.status == EnumWebResponse.SUCCESS &&
46
- response_device.data) {
47
- dispatch(updateDevice(response_device.data));
48
- }
49
- else {
50
- console.error("error getting device by id", payload);
51
- }
32
+ // For now, just dispatch the raw payload since we don't have the device helper
33
+ dispatch(updateDevice(payload.new));
52
34
  }
53
35
  function handlePlanInserts(payload) {
54
36
  console.log("[DB Listener] Plan INSERT received:", payload.new);
@@ -63,43 +45,99 @@ export function useScoutDbListener(supabaseClient) {
63
45
  dispatch(updatePlan(payload.new));
64
46
  }
65
47
  useEffect(() => {
66
- console.log(`[DB Listener] Using provided Supabase client - Instance ${instanceId.current}`);
67
- supabaseClient
68
- .channel("plans_insert")
48
+ console.log("=== SCOUT DB LISTENER DEBUG ===");
49
+ console.log("[DB Listener] Using shared Supabase client from ScoutRefreshProvider context");
50
+ if (!scoutSupabase) {
51
+ console.error("[DB Listener] No Supabase client available from ScoutRefreshProvider context");
52
+ return;
53
+ }
54
+ supabase.current = scoutSupabase;
55
+ // Test authentication first
56
+ const testAuth = async () => {
57
+ try {
58
+ const { data: { user }, error, } = await scoutSupabase.auth.getUser();
59
+ console.log("[DB Listener] Auth test - User:", user ? "authenticated" : "anonymous");
60
+ console.log("[DB Listener] Auth test - Error:", error);
61
+ }
62
+ catch (err) {
63
+ console.error("[DB Listener] Auth test failed:", err);
64
+ }
65
+ };
66
+ testAuth();
67
+ // Create a single channel for all operations with unique name
68
+ const channelName = `scout_realtime_${Date.now()}`;
69
+ console.log("[DB Listener] Creating channel:", channelName);
70
+ const mainChannel = scoutSupabase.channel(channelName);
71
+ // Subscribe to all events
72
+ mainChannel
69
73
  .on("postgres_changes", { event: "INSERT", schema: "public", table: "plans" }, handlePlanInserts)
70
- .subscribe();
71
- supabaseClient
72
- .channel("plans_delete")
73
74
  .on("postgres_changes", { event: "DELETE", schema: "public", table: "plans" }, handlePlanDeletes)
74
- .subscribe();
75
- supabaseClient
76
- .channel("plans_update")
77
75
  .on("postgres_changes", { event: "UPDATE", schema: "public", table: "plans" }, handlePlanUpdates)
78
- .subscribe();
79
- supabaseClient
80
- .channel("devices_delete")
81
- .on("postgres_changes", { event: "DELETE", schema: "public", table: "devices" }, handleDeviceDeletes)
82
- .subscribe();
83
- supabaseClient
84
- .channel("devices_insert")
85
76
  .on("postgres_changes", { event: "INSERT", schema: "public", table: "devices" }, handleDeviceInserts)
86
- .subscribe();
87
- supabaseClient
88
- .channel("devices_update")
77
+ .on("postgres_changes", { event: "DELETE", schema: "public", table: "devices" }, handleDeviceDeletes)
89
78
  .on("postgres_changes", { event: "UPDATE", schema: "public", table: "devices" }, handleDeviceUpdates)
90
- .subscribe();
91
- supabaseClient
92
- .channel("tags_insert")
93
79
  .on("postgres_changes", { event: "INSERT", schema: "public", table: "tags" }, handleTagInserts)
94
- .subscribe();
95
- supabaseClient
96
- .channel("tags_delete")
97
80
  .on("postgres_changes", { event: "DELETE", schema: "public", table: "tags" }, handleTagDeletes)
98
- .subscribe();
99
- supabaseClient
100
- .channel("tags_update")
101
81
  .on("postgres_changes", { event: "UPDATE", schema: "public", table: "tags" }, handleTagUpdates)
102
- .subscribe();
103
- supabase.current = supabaseClient;
104
- }, [supabaseClient]);
82
+ .subscribe((status) => {
83
+ console.log("[DB Listener] Subscription status:", status);
84
+ if (status === "SUBSCRIBED") {
85
+ console.log("[DB Listener] ✅ Successfully subscribed to real-time updates");
86
+ }
87
+ else if (status === "CHANNEL_ERROR") {
88
+ console.error("[DB Listener] ❌ Channel error occurred");
89
+ }
90
+ else if (status === "TIMED_OUT") {
91
+ console.error("[DB Listener] ⏰ Subscription timed out");
92
+ }
93
+ else if (status === "CLOSED") {
94
+ console.log("[DB Listener] 🔒 Channel closed");
95
+ }
96
+ });
97
+ channels.current.push(mainChannel);
98
+ // Test the connection with system events
99
+ const testChannelName = `test_connection_${Date.now()}`;
100
+ console.log("[DB Listener] Creating test channel:", testChannelName);
101
+ const testChannel = scoutSupabase.channel(testChannelName);
102
+ testChannel
103
+ .on("system", { event: "disconnect" }, () => {
104
+ console.log("[DB Listener] 🔌 Disconnected from Supabase");
105
+ })
106
+ .on("system", { event: "reconnect" }, () => {
107
+ console.log("[DB Listener] 🔗 Reconnected to Supabase");
108
+ })
109
+ .on("system", { event: "error" }, (error) => {
110
+ console.error("[DB Listener] ❌ System error:", error);
111
+ })
112
+ .subscribe((status) => {
113
+ console.log("[DB Listener] Test channel status:", status);
114
+ });
115
+ channels.current.push(testChannel);
116
+ // Test a simple database query to verify connection
117
+ const testDbConnection = async () => {
118
+ try {
119
+ const { data, error } = await scoutSupabase
120
+ .from("tags")
121
+ .select("count")
122
+ .limit(1);
123
+ console.log("[DB Listener] DB connection test - Success:", !!data);
124
+ console.log("[DB Listener] DB connection test - Error:", error);
125
+ }
126
+ catch (err) {
127
+ console.error("[DB Listener] DB connection test failed:", err);
128
+ }
129
+ };
130
+ testDbConnection();
131
+ console.log("=== END SCOUT DB LISTENER DEBUG ===");
132
+ // Cleanup function
133
+ return () => {
134
+ console.log("[DB Listener] 🧹 Cleaning up channels");
135
+ channels.current.forEach((channel) => {
136
+ if (channel) {
137
+ scoutSupabase.removeChannel(channel);
138
+ }
139
+ });
140
+ channels.current = [];
141
+ };
142
+ }, [scoutSupabase, dispatch]);
105
143
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adventurelabs/scout-core",
3
- "version": "1.0.22",
3
+ "version": "1.0.23",
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",