Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(timeline): handle React Strict Mode double WebSocket connection

Louis Beaumont2b3d16058dff
diff --git a/screenpipe-app-tauri/lib/hooks/use-timeline-data.tsx b/screenpipe-app-tauri/lib/hooks/use-timeline-data.tsx
index df644a671..8ad4ff925 100644
--- a/screenpipe-app-tauri/lib/hooks/use-timeline-data.tsx
+++ b/screenpipe-app-tauri/lib/hooks/use-timeline-data.tsx
@@ -1,6 +1,6 @@
import { StreamTimeSeriesResponse } from "@/components/rewind/timeline";
import { useTimelineStore } from "./use-timeline-store";
-import { useEffect } from "react";
+import { useEffect, useRef } from "react";
export function useTimelineData(
currentDate: Date,
@@ -16,7 +16,16 @@ export function useTimelineData(
websocket,
} = useTimelineStore();
+ // Ref to track if we've already initiated connection (handles React Strict Mode double-render)
+ const connectionInitiatedRef = useRef(false);
+
useEffect(() => {
+ // Only connect once, even in React Strict Mode
+ if (connectionInitiatedRef.current) {
+ return;
+ }
+ connectionInitiatedRef.current = true;
+
// First establish WebSocket connection
connectWebSocket();
}, []); // Only connect once when component mounts
diff --git a/screenpipe-app-tauri/lib/hooks/use-timeline-store.tsx b/screenpipe-app-tauri/lib/hooks/use-timeline-store.tsx
index 5fc787fc7..af6207487 100644
--- a/screenpipe-app-tauri/lib/hooks/use-timeline-store.tsx
+++ b/screenpipe-app-tauri/lib/hooks/use-timeline-store.tsx
@@ -131,9 +131,9 @@ export const useTimelineStore = create<TimelineState>((set, get) => ({
},
connectWebSocket: () => {
- // Close existing websocket if any
+ // Close existing websocket if any (including CONNECTING state to handle React Strict Mode double-render)
const existingWs = get().websocket;
- if (existingWs && existingWs.readyState === WebSocket.OPEN) {
+ if (existingWs && (existingWs.readyState === WebSocket.OPEN || existingWs.readyState === WebSocket.CONNECTING)) {
existingWs.close();
}