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

perf(chat-sidebar): stop running-pipes store from re-rendering every 30s

Louis Beaumont4c761b4e066f
diff --git a/apps/screenpipe-app-tauri/components/chat-sidebar.tsx b/apps/screenpipe-app-tauri/components/chat-sidebar.tsx
index 3064971e3..d95c07ed7 100644
--- a/apps/screenpipe-app-tauri/components/chat-sidebar.tsx
+++ b/apps/screenpipe-app-tauri/components/chat-sidebar.tsx
@@ -383,19 +383,30 @@ function CollapsibleScheduled({
);
}
+function formatElapsed(startedAt?: string): string | null {
+ if (!startedAt) return null;
+ const ms = Date.now() - Date.parse(startedAt);
+ if (Number.isNaN(ms) || ms < 0) return null;
+ if (ms < 60_000) return `${Math.floor(ms / 1000)}s`;
+ if (ms < 3600_000) return `${Math.floor(ms / 60_000)}m`;
+ return `${Math.floor(ms / 3600_000)}h`;
+}
+
function ScheduledRow({
pipe,
}: {
pipe: { pipeName: string; title?: string; startedAt?: string; executionId?: number };
}) {
- const elapsed = useMemo(() => {
- if (!pipe.startedAt) return null;
- const ms = Date.now() - Date.parse(pipe.startedAt);
- if (Number.isNaN(ms) || ms < 0) return null;
- if (ms < 60_000) return `${Math.floor(ms / 1000)}s`;
- if (ms < 3600_000) return `${Math.floor(ms / 60_000)}m`;
- return `${Math.floor(ms / 3600_000)}h`;
+ // Re-render once a minute so the elapsed badge ticks while the row is
+ // mounted. Cheap — at most one timer per visible scheduled pipe and the
+ // section is collapsed by default for many users.
+ const [, force] = useState(0);
+ useEffect(() => {
+ if (!pipe.startedAt) return;
+ const id = setInterval(() => force((n) => n + 1), 60_000);
+ return () => clearInterval(id);
}, [pipe.startedAt]);
+ const elapsed = formatElapsed(pipe.startedAt);
// Click → emit watch_pipe so standalone-chat opens the pipe execution
// and starts streaming its output. The page-level listener flips the
// active section to home if the user is on Pipes/Memories/etc.
diff --git a/apps/screenpipe-app-tauri/components/rewind/timeline/timeline-controls.tsx b/apps/screenpipe-app-tauri/components/rewind/timeline/timeline-controls.tsx
index 504399ed2..c314fa12a 100644
--- a/apps/screenpipe-app-tauri/components/rewind/timeline/timeline-controls.tsx
+++ b/apps/screenpipe-app-tauri/components/rewind/timeline/timeline-controls.tsx
@@ -19,73 +19,13 @@ import { usePlatform } from "@/lib/hooks/use-platform";
import { useSettings } from "@/lib/hooks/use-settings";
import { Calendar } from "@/components/ui/calendar";
import { listDaysWithFrames } from "@/lib/actions/has-frames-date";
+import { formatShortcutDisplay } from "@/lib/chat-utils";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
-// Helper to format shortcut string for display
-function formatShortcutForDisplay(shortcut: string, isMac: boolean): string {
- if (!shortcut) return "";
-
- const parts = shortcut.split("+");
- const modifiers: { symbol: string; order: number }[] = [];
- let key = "";
-
- for (const part of parts) {
- const upper = part.toUpperCase();
- if (isMac) {
- switch (upper) {
- case "SUPER":
- case "CMD":
- case "COMMAND":
- modifiers.push({ symbol: "⌘", order: 0 });
- break;
- case "CONTROL":
- case "CTRL":
- modifiers.push({ symbol: "⌃", order: 1 });
- break;
- case "ALT":
- case "OPTION":
- modifiers.push({ symbol: "⌥", order: 2 });
- break;
- case "SHIFT":
- modifiers.push({ symbol: "⇧", order: 3 });
- break;
- default:
- key = part.toUpperCase();
- }
- } else {
- switch (upper) {
- case "SUPER":
- case "CMD":
- case "COMMAND":
- modifiers.push({ symbol: "Win", order: 0 });
- break;
- case "CONTROL":
- case "CTRL":
- modifiers.push({ symbol: "Ctrl", order: 1 });
- break;
- case "ALT":
- case "OPTION":
- modifiers.push({ symbol: "Alt", order: 2 });
- break;
- case "SHIFT":
- modifiers.push({ symbol: "Shift", order: 3 });
- break;
- default:
- key = part;
- }
- }
- }
-
- modifiers.sort((a, b) => a.order - b.order);
- const formatted = [...modifiers.map((m) => m.symbol), key].filter(Boolean);
-
- return isMac ? formatted.join("") : formatted.join("+");
-}
-
interface TimeRange {
start: Date;
end: Date;
@@ -150,12 +90,12 @@ export function TimelineControls({
};
}, [calendarOpen]);
const searchShortcutDisplay = useMemo(
- () => formatShortcutForDisplay(settings.searchShortcut || (isMac ? "Control+Super+K" : "Alt+K"), isMac),
+ () => formatShortcutDisplay(settings.searchShortcut || (isMac ? "Control+Super+K" : "Alt+K"), isMac),
[settings.searchShortcut, isMac]
);
const chatShortcutDisplay = useMemo(
- () => formatShortcutForDisplay(settings.showChatShortcut || (isMac ? "Control+Super+L" : "Alt+L"), isMac),
+ () => formatShortcutDisplay(settings.showChatShortcut || (isMac ? "Control+Super+L" : "Alt+L"), isMac),
[settings.showChatShortcut, isMac]
);
diff --git a/apps/screenpipe-app-tauri/lib/actions/has-frames-date.ts b/apps/screenpipe-app-tauri/lib/actions/has-frames-date.ts
index dae183bc0..9ebe2678b 100644
--- a/apps/screenpipe-app-tauri/lib/actions/has-frames-date.ts
+++ b/apps/screenpipe-app-tauri/lib/actions/has-frames-date.ts
@@ -19,8 +19,17 @@ import { localFetch } from "@/lib/api";
* Includes audio_chunks because users with audio-only recording days
* (mic on, screen recording paused) would otherwise see those days
* greyed out even though the timeline has audio to play.
+ *
+ * Result is cached for 60s — the picker re-runs this on every popover
+ * open, and the underlying day-set rarely changes within a minute.
*/
+let daysCache: { at: number; days: Set<string> } | null = null;
+const DAYS_CACHE_TTL_MS = 60_000;
+
export async function listDaysWithFrames(): Promise<Set<string>> {
+ if (daysCache && Date.now() - daysCache.at < DAYS_CACHE_TTL_MS) {
+ return daysCache.days;
+ }
try {
// UNION ALL is fine — duplicates collapse via the outer DISTINCT.
// Both branches use the timestamp index (frames + audio_transcriptions
@@ -49,6 +58,7 @@ export async function listDaysWithFrames(): Promise<Set<string>> {
const rows = (await response.json()) as Array<{ day: string }>;
const set = new Set(rows.map((r) => r.day).filter(Boolean));
console.log(`[timeline] listDaysWithFrames: ${set.size} days with data`);
+ daysCache = { at: Date.now(), days: set };
return set;
} catch (e) {
console.error("listDaysWithFrames failed:", e);
diff --git a/apps/screenpipe-app-tauri/lib/hooks/use-running-pipes.ts b/apps/screenpipe-app-tauri/lib/hooks/use-running-pipes.ts
index 0fea29a0f..651c7f507 100644
--- a/apps/screenpipe-app-tauri/lib/hooks/use-running-pipes.ts
+++ b/apps/screenpipe-app-tauri/lib/hooks/use-running-pipes.ts
@@ -21,7 +21,7 @@
* (e.g. webview was hidden, ran out of memory, etc.).
*/
-import { useEffect } from "react";
+import { useEffect, useMemo } from "react";
import { create } from "zustand";
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
import { localFetch } from "@/lib/api";
@@ -58,18 +58,59 @@ const useRunningPipesStore = create<RunningPipesState & { actions: RunningPipesA
loading: true,
actions: {
hydrate: (pipes) =>
- set(() => {
+ set((s) => {
+ // Diff against current map: if the keyset is identical and every
+ // entry's executionId/startedAt/title is unchanged, only flip
+ // `loading` (preserving the existing object refs so subscribers
+ // that compare by identity don't churn every 30s).
+ const cur = s.pipes;
+ const curKeys = Object.keys(cur);
+ const sameKeys =
+ curKeys.length === pipes.length &&
+ pipes.every((p) => p.pipeName in cur);
+ if (sameKeys) {
+ const allSame = pipes.every((p) => {
+ const c = cur[p.pipeName];
+ return (
+ c.executionId === p.executionId &&
+ c.startedAt === p.startedAt &&
+ c.title === p.title
+ );
+ });
+ if (allSame) {
+ return s.loading ? { loading: false } : {};
+ }
+ }
const next: Record<string, RunningPipe> = {};
- for (const p of pipes) next[p.pipeName] = p;
+ for (const p of pipes) {
+ const c = cur[p.pipeName];
+ // Preserve the existing lastEventAt when nothing material changed
+ // — keeps recent pipe_event timestamps fresher than the poll's now.
+ next[p.pipeName] =
+ c &&
+ c.executionId === p.executionId &&
+ c.startedAt === p.startedAt &&
+ c.title === p.title
+ ? c
+ : p;
+ }
return { pipes: next, loading: false };
}),
touch: (p) =>
set((s) => {
const existing = s.pipes[p.pipeName];
- // Skip the write if the only field that changed is the
- // timestamp and we just touched it within 500ms — keeps
- // re-renders bounded under high event rates.
- if (existing && p.lastEventAt - existing.lastEventAt < 500) {
+ // Throttle pure-timestamp churn under high event rates, but never
+ // skip a write that carries a new executionId — losing that means
+ // the row can't open the right execution on click.
+ const sameExec =
+ !existing ||
+ p.executionId === undefined ||
+ p.executionId === existing.executionId;
+ if (
+ existing &&
+ sameExec &&
+ p.lastEventAt - existing.lastEventAt < 500
+ ) {
return {};
}
return {
@@ -164,6 +205,8 @@ export function useRunningPipes(): RunningPipe[] {
void mountRunningPipesTracker();
}, []);
const pipes = useRunningPipesStore((s) => s.pipes);
- // Sort by most recent activity desc, stable enough for a small list.
- return Object.values(pipes).sort((a, b) => b.lastEventAt - a.lastEventAt);
+ return useMemo(
+ () => Object.values(pipes).sort((a, b) => b.lastEventAt - a.lastEventAt),
+ [pipes],
+ );
}