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 all

Louis Beaumont794d561b71ab
diff --git a/screenpipe-app-tauri/app/timeline/page.tsx b/screenpipe-app-tauri/app/timeline/page.tsx
index 4f9c18976..415b39cc4 100644
--- a/screenpipe-app-tauri/app/timeline/page.tsx
+++ b/screenpipe-app-tauri/app/timeline/page.tsx
@@ -1,5 +1,5 @@
"use client";
-import { useEffect, useState, useRef, useMemo, useCallback } from "react";
+import { useEffect, useState, useRef } from "react";
import { OpenAI } from "openai";
import { generateId, Message } from "ai";
import { useSettings } from "@/lib/hooks/use-settings";
@@ -19,17 +19,7 @@ import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import { platform } from "@tauri-apps/plugin-os";
import posthog from "posthog-js";
-import { invoke } from "@tauri-apps/api/core";
-import {
- TimelineDock,
- TimelineDockIcon,
- TimelineIconsSection,
-} from "@/components/timeline/timeline-dock";
-import { debounce, throttle } from "lodash";
import { TimelineBlocks } from "@/components/timeline/timeline-block";
-import { TimeLabels } from "@/components/timeline/time-labels";
-import { CurrentTimeIndicator } from "@/components/timeline/current-time-indicator";
-import { ChatPanel } from "@/components/timeline/chat-panel";
interface StreamTimeSeriesResponse {
timestamp: string;
@@ -72,10 +62,6 @@ interface Agent {
dataSelector: (frames: StreamTimeSeriesResponse[]) => any;
}
-interface AppIconCache {
- [key: string]: string; // app_name -> base64 icon
-}
-
const AGENTS: Agent[] = [
{
id: "context-master",
@@ -128,157 +114,6 @@ const AGENTS: Agent[] = [
},
];
-export interface TimeBlock {
- appName: string;
- startTime: Date;
- endTime: Date;
- color: string; // We'll generate colors based on app names
-}
-export interface TimelineBlock extends TimeBlock {
- left: number; // percentage position from left
- width: number; // percentage width
-}
-
-function getAppColor(appName: string): string {
- // Simple hash function to generate consistent colors for apps
- let hash = 0;
- for (let i = 0; i < appName.length; i++) {
- hash = appName.charCodeAt(i) + ((hash << 5) - hash);
- }
-
- // Convert to RGB - using mostly blue and green hues for a tech feel
- const hue = Math.abs(hash) % 360;
- return `hsl(${hue}, 70%, 50%)`;
-}
-
-function calculateTimeBlocks(frames: any[]): TimeBlock[] {
- if (frames.length === 0) return [];
-
- const timeRange = getTimelineRange(frames);
- const blocks: TimeBlock[] = [];
- let currentBlock: TimeBlock | null = null;
-
- // Sort frames by timestamp
- // const sortedFrames = [...frames].sort(
- // (a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime()
- // );
-
- frames.forEach((frame) => {
- const timestamp = new Date(frame.timestamp);
- const appName = frame.devices[0].metadata.app_name;
-
- // Skip frames outside the timeline range
- if (timestamp < timeRange.start || timestamp > timeRange.end) {
- return;
- }
-
- if (!currentBlock) {
- currentBlock = {
- appName,
- startTime: timestamp,
- endTime: timestamp,
- color: getAppColor(appName),
- };
- } else if (currentBlock.appName !== appName) {
- // App changed, close current block and start new one
- currentBlock.endTime = timestamp;
- blocks.push(currentBlock);
- currentBlock = {
- appName,
- startTime: timestamp,
- endTime: timestamp,
- color: getAppColor(appName),
- };
- } else {
- // Same app, update end time
- currentBlock.endTime = timestamp;
- }
- });
-
- // Add the last block
- if (currentBlock) {
- blocks.push(currentBlock);
- }
-
- return blocks;
-}
-
-function getTimelineRange(frames: StreamTimeSeriesResponse[]): {
- start: Date;
- end: Date;
-} {
- // Default range: 8am to current time
- const now = new Date();
- const defaultEnd = now;
- const defaultStart = new Date(now);
- defaultStart.setHours(8, 0, 0, 0);
-
- if (frames.length === 0) {
- return { start: defaultStart, end: defaultEnd };
- }
-
- // Find actual data range
- const timestamps = frames.map((f) => new Date(f.timestamp));
- const dataStart = new Date(Math.min(...timestamps.map((t) => t.getTime())));
- const dataEnd = new Date(Math.max(...timestamps.map((t) => t.getTime())));
-
- // Expand range if data exists outside default range
- return {
- start:
- dataStart.getTime() < defaultStart.getTime() ? dataStart : defaultStart,
- end: dataEnd.getTime() > defaultEnd.getTime() ? dataEnd : defaultEnd,
- };
-}
-
-function getUniqueAppRanges(
- blocks: TimeBlock[],
- mergeThresholdMs: number = 300000
-): TimelineBlock[] {
- if (blocks.length === 0) return [];
-
- const sortedBlocks = [...blocks].sort(
- (a, b) => a.startTime.getTime() - b.startTime.getTime()
- );
-
- const mergedBlocks = sortedBlocks.reduce((acc: TimeBlock[], curr) => {
- const lastBlock = acc[acc.length - 1];
-
- if (!lastBlock) {
- return [curr];
- }
-
- if (
- lastBlock.appName === curr.appName &&
- curr.startTime.getTime() - lastBlock.endTime.getTime() <= mergeThresholdMs
- ) {
- lastBlock.endTime = new Date(
- Math.max(lastBlock.endTime.getTime(), curr.endTime.getTime())
- );
- return acc;
- }
-
- return [...acc, curr];
- }, []);
-
- // Use dynamic range instead of full day
- // @ts-ignore
- const timeRange = getTimelineRange(frames);
- const totalMs = timeRange.end.getTime() - timeRange.start.getTime();
-
- return mergedBlocks.map((block) => {
- const left =
- ((block.startTime.getTime() - timeRange.start.getTime()) / totalMs) * 100;
- const width =
- ((block.endTime.getTime() - block.startTime.getTime()) / totalMs) * 100;
-
- return {
- ...block,
- left: Math.max(0, Math.min(100, left)),
- width: Math.max(0, Math.min(100 - left, width)),
- };
- });
-}
-
export default function Timeline() {
const [currentFrame, setCurrentFrame] = useState<DeviceFrameResponse | null>(
null
@@ -318,19 +153,6 @@ export default function Timeline() {
height: 500,
});
const resizerRef = useRef<HTMLDivElement | null>(null);
- const [iconCache, setIconCache] = useState<AppIconCache>({});
-
- // 1. Memoize time range calculation
- const timelineRange = useMemo(() => getTimelineRange(frames), [frames]);
-
- // 2. Memoize time blocks calculation
- const timeBlocks = useMemo(() => calculateTimeBlocks(frames), [frames]);
-
- // 3. Memoize app ranges calculation with merged threshold
- const appRanges = useMemo(
- () => getUniqueAppRanges(timeBlocks, 300000),
- [timeBlocks]
- );
useEffect(() => {
setPosition({
@@ -340,17 +162,6 @@ export default function Timeline() {
}, []);
const setupEventSource = () => {
- // Reset states when reloading
- setFrames([]);
- setCurrentFrame(null);
- setCurrentIndex(0);
- setIsLoading(true);
- setError(null);
- setSelectionRange(null);
- setIsAiPanelExpanded(false);
- setChatMessages([]);
- setAiInput("");
-
if (eventSourceRef.current) {
eventSourceRef.current.close();
}
@@ -376,55 +187,40 @@ export default function Timeline() {
const data = JSON.parse(event.data);
if (data === "keep-alive-text") return;
- // Add error handling for server-side errors
- if (data.error) {
- console.error("server error:", data.error);
- setError(data.error);
- setIsLoading(false);
- return;
- }
-
if (data.timestamp && data.devices) {
- console.log("new frame:", data.timestamp, data.devices);
setFrames((prev) => {
const exists = prev.some((f) => f.timestamp === data.timestamp);
if (exists) return prev;
- const MAX_FRAMES = 1000;
-
- // Add new frame and maintain limit
- // const newFrames = [...prev, data].sort(
- // (a, b) =>
- // new Date(b.timestamp).getTime() -
- // new Date(a.timestamp).getTime()
- // );
- // .slice(0, MAX_FRAMES); // TODO: likely rem
+ // ! HACK: Add new frame and sort in descending order
+ const newFrames = [...prev, data].sort((a, b) => {
+ return (
+ new Date(b.timestamp).getTime() -
+ new Date(a.timestamp).getTime()
+ );
+ });
- // return newFrames;
- return [...prev, data];
+ return newFrames;
});
- // Only set initial frame if we don't have one
setCurrentFrame((prev) => prev || data.devices[0]);
setIsLoading(false);
}
} catch (error) {
console.error("failed to parse frame data:", error);
- setError("failed to parse server response");
- setIsLoading(false);
}
};
eventSource.onerror = (error) => {
- setIsLoading(false);
-
if (eventSource.readyState === EventSource.CLOSED) {
console.log("stream ended (expected behavior)", error);
+ setIsLoading(false);
return;
}
console.error("eventsource error:", error);
- setError("connection lost - please refresh the page");
+ setError("connection lost. retrying...");
+
eventSource.close();
};
@@ -435,6 +231,47 @@ export default function Timeline() {
};
};
+ const getLoadedTimeRangeStyles = () => {
+ if (!loadedTimeRange || frames.length === 0)
+ return { left: "0%", right: "100%" };
+
+ // Find the earliest frame timestamp
+ const firstFrame = [...frames].sort(
+ (a, b) =>
+ new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime()
+ )[0];
+ const earliestTime = new Date(firstFrame.timestamp);
+
+ // Create local date objects for today
+ const startOfDay = new Date();
+ startOfDay.setHours(0, 0, 0, 0);
+
+ const endOfDay = new Date();
+ endOfDay.setHours(23, 59, 59, 999);
+
+ // Convert UTC loadedTimeRange to local time
+ const localStart = new Date(earliestTime);
+ const localEnd = new Date(loadedTimeRange.end);
+
+ const totalMs = endOfDay.getTime() - startOfDay.getTime();
+ const startPercent =
+ ((localStart.getTime() - startOfDay.getTime()) / totalMs) * 100;
+ const endPercent =
+ ((localEnd.getTime() - startOfDay.getTime()) / totalMs) * 100;
+
+ // Temporarily return empty values to disable grey areas
+ return {
+ right: "0%",
+ left: "0%",
+ };
+
+ // Original code commented out:
+ // return {
+ // right: `${Math.max(0, Math.min(100, startPercent))}%`, // Grey before data starts
+ // left: `${Math.max(0, Math.min(100, 100 - endPercent))}%`, // Grey after data ends
+ // };
+ };
+
useEffect(() => {
setupEventSource();
@@ -473,111 +310,154 @@ export default function Timeline() {
return () => window.removeEventListener("keydown", handleKeyDown);
}, [selectionRange, isAiPanelExpanded, aiInput]);
+ const handleScroll = (e: React.WheelEvent<HTMLDivElement>) => {
+ const isWithinAiPanel = aiPanelRef.current?.contains(e.target as Node);
+ if (isWithinAiPanel) {
+ return;
+ }
+
+ e.preventDefault();
+ e.stopPropagation();
+
+ const scrollSensitivity = 1;
+ const delta = -Math.sign(e.deltaY) / scrollSensitivity;
+
+ const newIndex = Math.min(
+ Math.max(
+ 0,
+ currentIndex + (delta > 0 ? Math.ceil(delta) : Math.floor(delta))
+ ),
+ frames.length - 1
+ );
+
+ if (newIndex !== currentIndex) {
+ setCurrentIndex(newIndex);
+ setCurrentFrame(frames[newIndex].devices[0]);
+ }
+ };
+
+ const getCurrentTimePercentage = () => {
+ if (!currentFrame) return 0;
+
+ const frameTime = new Date(
+ currentFrame.metadata.timestamp || frames[currentIndex].timestamp
+ );
+ const startOfDay = new Date(frameTime);
+ startOfDay.setHours(0, 0, 0, 0);
+ const endOfDay = new Date(frameTime);
+ endOfDay.setHours(23, 59, 59, 999);
+
+ const totalDayMilliseconds = endOfDay.getTime() - startOfDay.getTime();
+ const currentMilliseconds = frameTime.getTime() - startOfDay.getTime();
+
+ return (currentMilliseconds / totalDayMilliseconds) * 100;
+ };
+
const handleTimelineMouseDown = (e: React.MouseEvent<HTMLDivElement>) => {
- if (!e.currentTarget) return;
const rect = e.currentTarget.getBoundingClientRect();
const clickX = e.clientX - rect.left;
const percentage = (clickX / rect.width) * 100;
+ // Clamp percentage between 0 and 100
+ const clampedPercentage = Math.max(0, Math.min(100, percentage));
+
setIsDragging(true);
- setDragStart(percentage);
- setIsAiPanelExpanded(false);
+ setDragStart(clampedPercentage);
- // Set initial selection range
const totalMinutesInDay = 24 * 60;
- const minutes = (percentage / 100) * totalMinutesInDay;
- const date = new Date();
- date.setHours(Math.floor(minutes / 60), Math.floor(minutes % 60), 0, 0);
+ const minutesFromMidnight = (clampedPercentage / 100) * totalMinutesInDay;
+ const hours = Math.floor(minutesFromMidnight / 60);
+ const minutes = Math.floor(minutesFromMidnight % 60);
+
+ const localDate = new Date();
+ localDate.setHours(hours, minutes, 0, 0);
+
+ const utcDate = new Date(
+ Date.UTC(
+ localDate.getUTCFullYear(),
+ localDate.getUTCMonth(),
+ localDate.getUTCDate(),
+ localDate.getUTCHours(),
+ localDate.getUTCMinutes(),
+ 0,
+ 0
+ )
+ );
setSelectionRange({
- start: date,
- end: date,
+ start: utcDate,
+ end: utcDate,
});
};
- const debouncedMouseMove = useMemo(
- () =>
- throttle(
- (e: React.MouseEvent<HTMLDivElement>) => {
- if (!isDragging || dragStart === null || !e.currentTarget) return;
-
- const rect = e.currentTarget.getBoundingClientRect();
- const moveX = e.clientX - rect.left;
- const percentage = (moveX / rect.width) * 100;
-
- const totalMinutesInDay = 24 * 60;
- const startMinutes =
- (Math.min(dragStart, percentage) / 100) * totalMinutesInDay;
- const endMinutes =
- (Math.max(dragStart, percentage) / 100) * totalMinutesInDay;
-
- const startLocal = new Date();
- startLocal.setHours(
- Math.floor(startMinutes / 60),
- Math.floor(startMinutes % 60),
- 0,
- 0
- );
-
- const endLocal = new Date();
- endLocal.setHours(
- Math.floor(endMinutes / 60),
- Math.floor(endMinutes % 60),
- 0,
- 0
- );
-
- setSelectionRange({
- start: startLocal,
- end: endLocal,
- });
- },
- 16 // ~60fps
- ),
- [isDragging, dragStart]
- );
+ const handleTimelineMouseMove = (e: React.MouseEvent<HTMLDivElement>) => {
+ if (!isDragging || dragStart === null) return;
- const debouncedScroll = useMemo(
- () =>
- throttle((e: React.WheelEvent<HTMLDivElement>) => {
- const isWithinAiPanel = aiPanelRef.current?.contains(e.target as Node);
- if (isWithinAiPanel) return;
+ // Prevent text selection during drag
+ e.preventDefault();
- e.preventDefault();
- e.stopPropagation();
-
- const scrollSensitivity = 1;
- const delta = -Math.sign(e.deltaY) / scrollSensitivity;
-
- const newIndex = Math.min(
- Math.max(
- 0,
- currentIndex + (delta > 0 ? Math.ceil(delta) : Math.floor(delta))
- ),
- frames.length - 1
- );
-
- if (newIndex !== currentIndex) {
- setCurrentIndex(newIndex);
- setCurrentFrame(frames[newIndex].devices[0]);
- }
- }, 16),
- [currentIndex, frames]
- );
+ const rect = e.currentTarget.getBoundingClientRect();
+ const moveX = e.clientX - rect.left;
+ const percentage = (moveX / rect.width) * 100;
+
+ // Clamp percentage between 0 and 100
+ const clampedPercentage = Math.max(0, Math.min(100, percentage));
+
+ const totalMinutesInDay = 24 * 60;
+ const startMinutes =
+ (Math.min(dragStart, clampedPercentage) / 100) * totalMinutesInDay;
+ const endMinutes =
+ (Math.max(dragStart, clampedPercentage) / 100) * totalMinutesInDay;
+
+ const startLocal = new Date();
+ startLocal.setHours(
+ Math.floor(startMinutes / 60),
+ Math.floor(startMinutes % 60),
+ 0,
+ 0
+ );
+
+ const endLocal = new Date();
+ endLocal.setHours(
+ Math.floor(endMinutes / 60),
+ Math.floor(endMinutes % 60),
+ 0,
+ 0
+ );
+
+ const utcStartDate = new Date(
+ Date.UTC(
+ startLocal.getUTCFullYear(),
+ startLocal.getUTCMonth(),
+ startLocal.getUTCDate(),
+ startLocal.getUTCHours(),
+ startLocal.getUTCMinutes(),
+ 0,
+ 0
+ )
+ );
+
+ const utcEndDate = new Date(
+ Date.UTC(
+ endLocal.getUTCFullYear(),
+ endLocal.getUTCMonth(),
+ endLocal.getUTCDate(),
+ endLocal.getUTCHours(),
+ endLocal.getUTCMinutes(),
+ 0,
+ 0
+ )
+ );
+
+ setSelectionRange({
+ start: utcStartDate,
+ end: utcEndDate,
+ });
+ };
const handleTimelineMouseUp = () => {
- if (isDragging) {
- setIsDragging(false);
- setDragStart(null);
-
- // Only show AI panel if we have a meaningful selection
- // if (
- // selectionRange &&
- // selectionRange.start.getTime() !== selectionRange.end.getTime()
- // ) {
- // setIsAiPanelExpanded(true);
- // }
- }
+ setIsDragging(false);
+ setDragStart(null);
};
const handleAskAI = () => {
@@ -725,12 +605,13 @@ export default function Timeline() {
};
useEffect(() => {
- const preventScroll = (e: Event) => {
- e.preventDefault();
- return false;
+ const preventScroll = (e: WheelEvent) => {
+ const isWithinAiPanel = aiPanelRef.current?.contains(e.target as Node);
+ if (!isWithinAiPanel) {
+ e.preventDefault();
+ }
};
- // Add more comprehensive scroll prevention
document.addEventListener("wheel", preventScroll, { passive: false });
return () => document.removeEventListener("wheel", preventScroll);
}, []);
@@ -788,6 +669,11 @@ export default function Timeline() {
const handleRefresh = () => {
posthog.capture("timeline_refresh");
+
+ setFrames([]);
+ setCurrentFrame(null);
+ setCurrentIndex(0);
+ setIsLoading(true);
setupEventSource();
};
@@ -821,70 +707,18 @@ export default function Timeline() {
document.addEventListener("mouseup", handleMouseUp);
};
- const loadAppIcon = async (appName: string, appPath?: string) => {
- if (iconCache[appName]) return;
-
- const icon = await invoke<{ base64: string; path: string } | null>(
- "get_app_icon",
- {
- appName,
- appPath,
- }
- );
-
- if (icon) {
- setIconCache((prev) => ({
- ...prev,
- [appName]: icon.base64,
- }));
- }
- };
-
- useEffect(() => {
- const p = platform();
- if (p !== "macos") return;
- frames.forEach((frame) => {
- frame.devices.forEach((device) => {
- loadAppIcon(device.metadata.app_name);
- });
- });
- }, [frames]);
-
- useEffect(() => {
- const cleanup = () => {
- if (eventSourceRef.current) {
- eventSourceRef.current.close();
- }
- if (retryTimeoutRef.current) {
- clearTimeout(retryTimeoutRef.current);
- }
- if (abortControllerRef.current) {
- abortControllerRef.current.abort();
- }
- };
-
- window.addEventListener("beforeunload", cleanup);
- return () => {
- cleanup();
- window.removeEventListener("beforeunload", cleanup);
- };
- }, []);
-
- useEffect(() => {
- return () => {
- debouncedMouseMove.cancel();
- debouncedScroll.cancel();
- };
- }, [debouncedMouseMove, debouncedScroll]);
-
return (
<div
className="fixed inset-0 flex flex-col bg-background text-foreground overflow-hidden relative"
- onWheel={debouncedScroll}
+ onWheel={(e) => {
+ const isWithinAiPanel = aiPanelRef.current?.contains(e.target as Node);
+ if (!isWithinAiPanel) {
+ handleScroll(e);
+ }
+ }}
style={{
height: "100vh",
overscrollBehavior: "none",
- overflow: "hidden",
WebkitUserSelect: "none",
userSelect: "none",
MozUserSelect: "none",
@@ -907,7 +741,7 @@ export default function Timeline() {
/>
<div className="flex-1 relative min-h-0">
- {(isLoading || frames.length === 0) && (
+ {isLoading && (
<div className="absolute inset-0 flex items-center justify-center">
<div className="bg-background/90 p-5 border rounded-lg shadow-lg text-center">
<p>loading frames...</p>
@@ -915,7 +749,7 @@ export default function Timeline() {
</div>
</div>
)}
- {error && !isLoading && (
+ {error && (
<div className="absolute inset-0 flex items-center justify-center">
<div className="bg-destructive/10 p-5 border-destructive/20 border rounded-lg text-destructive">
<AlertCircle className="h-4 w-4 mb-2 mx-auto" />
@@ -924,24 +758,51 @@ export default function Timeline() {
</div>
)}
{currentFrame && (
- <img
- src={`data:image/png;base64,${currentFrame.frame}`}
- className="absolute inset-0 w-full h-full object-contain mx-auto mt-6"
- alt="Current frame"
- loading="lazy"
- decoding="async"
- />
+ <>
+ <div className="w-4/5 mx-auto mt-4 mb-4 text-center select-none">
+ <div className="inline-block bg-card p-2 rounded-lg shadow-lg border">
+ <div className="flex items-center gap-4 text-sm">
+ <div>device: {currentFrame.device_id}</div>
+ <div>app: {currentFrame.metadata.app_name || "n/a"}</div>
+ <div>
+ window: {currentFrame.metadata.window_name || "n/a"}
+ </div>
+ </div>
+ </div>
+ </div>
+ <img
+ src={`data:image/png;base64,${currentFrame.frame}`}
+ className="absolute inset-0 w-4/5 h-auto max-h-[75vh] object-contain mx-auto mt-12"
+ alt="Current frame"
+ />
+ </>
)}
</div>
<div className="w-4/5 mx-auto my-8 relative select-none">
<div
- className="h-[60px] bg-card border rounded-lg shadow-sm relative overflow-hidden"
+ className="h-[60px] bg-card border rounded-lg shadow-sm cursor-crosshair relative"
onMouseDown={handleTimelineMouseDown}
- onMouseMove={debouncedMouseMove}
+ onMouseMove={handleTimelineMouseMove}
onMouseUp={handleTimelineMouseUp}
onMouseLeave={handleTimelineMouseUp}
>
+ <div
+ className="absolute inset-0 pointer-events-none"
+ style={{
+ ...getLoadedTimeRangeStyles(),
+ background:
+ "linear-gradient(to right, rgba(255,255,255,0.8), rgba(255,255,255,0.2))",
+ }}
+ />
+
+ {/* {loadedTimeRange && (
+ <TimelineBlocks
+ frames={frames}
+ timeRange={loadedTimeRange}
+ />
+ )} */}
+
<div
className="absolute inset-0"
style={{
@@ -950,196 +811,214 @@ export default function Timeline() {
backgroundSize: "10% 100%",
}}
/>
- <TimelineBlocks frames={frames} timeRange={timelineRange} />
- <CurrentTimeIndicator
- currentFrame={currentFrame}
- frames={frames}
- currentIndex={currentIndex}
- timeRange={timelineRange}
+ <div
+ className="absolute top-0 h-full w-1 bg-foreground/50 shadow-sm opacity-80 z-10"
+ style={{
+ left: `${getCurrentTimePercentage()}%`,
+ }}
/>
- {/* <TimelineIconsSection blocks={appRanges} /> */}
-
- <TimeLabels timeRange={timelineRange} />
- </div>
- </div>
-
- <div className="h-12" />
-
- <div className="fixed left-12 top-1/2 -translate-y-1/2 text-xs text-muted-foreground">
- <div className="flex flex-col items-center gap-1">
- <span>▲</span>
- <span>scroll</span>
- <span>▼</span>
+ {selectionRange && (
+ <div
+ className="absolute top-0 h-full bg-foreground/10"
+ style={{
+ left: `${
+ (new Date(selectionRange.start).getHours() * 3600 +
+ new Date(selectionRange.start).getMinutes() * 60) /
+ 864
+ }%`,
+ width: `${
+ ((selectionRange.end.getTime() -
+ selectionRange.start.getTime()) /
+ (24 * 3600 * 1000)) *
+ 100
+ }%`,
+ }}
+ />
+ )}
</div>
- </div>
- {selectionRange && (
- <div
- className="absolute top-0 h-full bg-foreground/10"
- style={{
- left: `${
- (new Date(selectionRange.start).getHours() * 3600 +
- new Date(selectionRange.start).getMinutes() * 60) /
- 864
- }%`,
- width: `${
- ((selectionRange.end.getTime() - selectionRange.start.getTime()) /
- (24 * 3600 * 1000)) *
- 100
- }%`,
- }}
- />
- )}
-
- {selectionRange && (
- <div
- ref={aiPanelRef}
- style={{
- position: "fixed",
- left: position.x,
- top: position.y,
- width: chatWindowSize.width,
- height: isAiPanelExpanded ? chatWindowSize.height : 120,
- cursor: isDraggingPanel ? "grabbing" : "default",
- }}
- className={`bg-background border border-muted-foreground rounded-lg shadow-lg transition-all duration-300 ease-in-out z-[100]`}
- >
+ {selectionRange && (
<div
- className="select-none cursor-grab active:cursor-grabbing"
- onMouseDown={handlePanelMouseDown}
- onMouseMove={handlePanelMouseMove}
- onMouseUp={handlePanelMouseUp}
- onMouseLeave={handlePanelMouseUp}
+ ref={aiPanelRef}
+ style={{
+ position: "fixed",
+ left: position.x,
+ top: position.y,
+ width: chatWindowSize.width,
+ height: isAiPanelExpanded ? chatWindowSize.height : 120,
+ cursor: isDraggingPanel ? "grabbing" : "default",
+ }}
+ className={`bg-background border border-muted-foreground rounded-lg shadow-lg transition-all duration-300 ease-in-out z-[100]`}
>
- <div className="p-4 border-b border-muted-foreground flex justify-between items-center group">
- <div className="flex items-center gap-2 flex-1">
- <GripHorizontal className="w-4 h-4 text-muted-foreground group-hover:text-foreground" />
- <div className="text-muted-foreground text-xs">
- {new Date(
- selectionRange.start.getTime()
- ).toLocaleTimeString()}{" "}
- -{" "}
- {new Date(selectionRange.end.getTime()).toLocaleTimeString()}
+ <div
+ className="select-none cursor-grab active:cursor-grabbing"
+ onMouseDown={handlePanelMouseDown}
+ onMouseMove={handlePanelMouseMove}
+ onMouseUp={handlePanelMouseUp}
+ onMouseLeave={handlePanelMouseUp}
+ >
+ <div className="p-4 border-b border-muted-foreground flex justify-between items-center group">
+ <div className="flex items-center gap-2 flex-1">
+ <GripHorizontal className="w-4 h-4 text-muted-foreground group-hover:text-foreground" />
+ <div className="text-muted-foreground text-xs">
+ {new Date(
+ selectionRange.start.getTime()
+ ).toLocaleTimeString()}{" "}
+ -{" "}
+ {new Date(
+ selectionRange.end.getTime()
+ ).toLocaleTimeString()}
+ </div>
</div>
- </div>
- <button
- onClick={() => {
- setSelectionRange(null);
- setIsAiPanelExpanded(false);
- setChatMessages([]);
- setAiInput("");
- }}
- className="text-muted-foreground hover:text-foreground transition-colors ml-2"
- >
- <X className="h-4 w-4" />
- </button>
- </div>
-
- {!isAiPanelExpanded && (
- <div className="p-4">
<button
- className="px-3 py-1 bg-background hover:bg-accent border text-foreground text-xs rounded flex items-center gap-2 transition-colors"
- onClick={(e) => {
- e.stopPropagation();
- setIsAiPanelExpanded(true);
- setTimeout(() => {
- inputRef.current?.focus();
- }, 100);
+ onClick={() => {
+ setSelectionRange(null);
+ setIsAiPanelExpanded(false);
+ setChatMessages([]);
+ setAiInput("");
}}
+ className="text-muted-foreground hover:text-foreground transition-colors ml-2"
>
- <span>ask ai</span>
- <span className="text-muted-foreground text-[10px]">
- {osType === "macos" ? "⌘K" : "Ctrl+K"}
- </span>
+ <X className="h-4 w-4" />
</button>
</div>
- )}
- </div>
- {isAiPanelExpanded && (
- <div className="flex flex-col h-[calc(100%-52px)]">
- <div
- className="flex-1 overflow-y-auto p-4 space-y-4 min-h-0 hover:cursor-auto text-foreground font-mono text-sm leading-relaxed"
- style={{
- WebkitUserSelect: "text",
- userSelect: "text",
- MozUserSelect: "text",
- msUserSelect: "text",
- overscrollBehavior: "contain",
- }}
- >
- {chatMessages.map((msg, index) => (
- <ChatMessage key={index} message={msg} />
- ))}
- {isAiLoading && (
- <div className="flex justify-center">
- <Loader2 className="h-6 w-6 animate-spin text-foreground" />
- </div>
- )}
- </div>
-
- <form
- onSubmit={handleAiSubmit}
- className="p-3 border-t border-muted-foreground"
- >
- <div className="flex flex-col gap-2">
- <select
- value={selectedAgent.id}
- onChange={(e) => handleAgentChange(e.target.value)}
- className="w-full bg-background border border-muted-foreground text-foreground rounded px-2 py-1 text-xs"
+ {!isAiPanelExpanded && (
+ <div className="p-4">
+ <button
+ className="px-3 py-1 bg-background hover:bg-accent border text-foreground text-xs rounded flex items-center gap-2 transition-colors"
+ onClick={(e) => {
+ e.stopPropagation();
+ setIsAiPanelExpanded(true);
+ setTimeout(() => {
+ inputRef.current?.focus();
+ }, 100);
+ }}
>
- {AGENTS.map((agent) => (
- <option
- key={agent.id}
- value={agent.id}
- className="bg-background text-foreground"
- >
- {agent.name} - {agent.description}
- </option>
- ))}
- </select>
- <div className="flex gap-2">
- <Input
- ref={inputRef}
- type="text"
- value={aiInput}
- onChange={(e) => setAiInput(e.target.value)}
- placeholder="ask about this time range..."
- className="flex-1 bg-background border border-muted-foreground text-foreground placeholder-muted-foreground"
- disabled={isAiLoading}
- />
- <Button
- type="submit"
- variant="outline"
- className="hover:bg-accent transition-colors"
- disabled={isAiLoading}
+ <span>ask ai</span>
+ <span className="text-muted-foreground text-[10px]">
+ {osType === "macos" ? "⌘K" : "Ctrl+K"}
+ </span>
+ </button>
+ </div>
+ )}
+ </div>
+
+ {isAiPanelExpanded && (
+ <div className="flex flex-col h-[calc(100%-52px)]">
+ <div
+ className="flex-1 overflow-y-auto p-4 space-y-4 min-h-0 hover:cursor-auto text-foreground font-mono text-sm leading-relaxed"
+ style={{
+ WebkitUserSelect: "text",
+ userSelect: "text",
+ MozUserSelect: "text",
+ msUserSelect: "text",
+ overscrollBehavior: "contain",
+ }}
+ >
+ {chatMessages.map((msg, index) => (
+ <ChatMessage key={index} message={msg} />
+ ))}
+ {isAiLoading && (
+ <div className="flex justify-center">
+ <Loader2 className="h-6 w-6 animate-spin text-foreground" />
+ </div>
+ )}
+ </div>
+
+ <form
+ onSubmit={handleAiSubmit}
+ className="p-3 border-t border-muted-foreground"
+ >
+ <div className="flex flex-col gap-2">
+ <select
+ value={selectedAgent.id}
+ onChange={(e) => handleAgentChange(e.target.value)}
+ className="w-full bg-background border border-muted-foreground text-foreground rounded px-2 py-1 text-xs"
>
- {isStreaming ? (
- <Square className="h-4 w-4" />
- ) : (
- <Send className="h-4 w-4" />
- )}
- </Button>
+ {AGENTS.map((agent) => (
+ <option
+ key={agent.id}
+ value={agent.id}
+ className="bg-background text-foreground"
+ >
+ {agent.name} - {agent.description}
+ </option>
+ ))}
+ </select>
+ <div className="flex gap-2">
+ <Input
+ ref={inputRef}
+ type="text"
+ value={aiInput}
+ onChange={(e) => setAiInput(e.target.value)}
+ placeholder="ask about this time range..."
+ className="flex-1 bg-background border border-muted-foreground text-foreground placeholder-muted-foreground"
+ disabled={isAiLoading}
+ />
+ <Button
+ type="submit"
+ variant="outline"
+ className="hover:bg-accent transition-colors"
+ disabled={isAiLoading}
+ >
+ {isStreaming ? (
+ <Square className="h-4 w-4" />
+ ) : (
+ <Send className="h-4 w-4" />
+ )}
+ </Button>
+ </div>
</div>
+ </form>
+ </div>
+ )}
+
+ <div
+ ref={resizerRef}
+ onMouseDown={handleMouseDown}
+ className="absolute right-0 bottom-0 w-4 h-4 cursor-se-resize bg-transparent"
+ style={{
+ borderTopLeftRadius: "4px",
+ borderBottomRightRadius: "4px",
+ cursor: "se-resize",
+ }}
+ />
+ </div>
+ )}
+
+ <div className="relative mt-1 px-2 text-[10px] text-muted-foreground select-none">
+ {Array(7)
+ .fill(0)
+ .map((_, i) => {
+ const hour = (i * 4) % 24;
+ const date = new Date();
+ date.setHours(hour, 0, 0, 0);
+ return (
+ <div
+ key={i}
+ className="absolute transform -translate-x-1/2"
+ style={{ left: `${(i * 100) / 6}%` }}
+ >
+ {date.toLocaleTimeString([], {
+ hour: "2-digit",
+ minute: "2-digit",
+ })}
</div>
- </form>
- </div>
- )}
+ );
+ })}
+ </div>
+ </div>
- <div
- ref={resizerRef}
- onMouseDown={handleMouseDown}
- className="absolute right-0 bottom-0 w-4 h-4 cursor-se-resize bg-transparent"
- style={{
- borderTopLeftRadius: "4px",
- borderBottomRightRadius: "4px",
- cursor: "se-resize",
- }}
- />
+ <div className="fixed left-12 top-1/2 -translate-y-1/2 text-xs text-muted-foreground">
+ <div className="flex flex-col items-center gap-1">
+ <span>▲</span>
+ <span>scroll</span>
+ <span>▼</span>
</div>
- )}
+ </div>
</div>
);
}
diff --git a/screenpipe-app-tauri/components/timeline/current-time-indicator.tsx b/screenpipe-app-tauri/components/timeline/current-time-indicator.tsx
index c446d5679..e050cec21 100644
--- a/screenpipe-app-tauri/components/timeline/current-time-indicator.tsx
+++ b/screenpipe-app-tauri/components/timeline/current-time-indicator.tsx
@@ -26,7 +26,7 @@ export function CurrentTimeIndicator({
if (!currentFrame) return 0;
const frameTime = new Date(
- currentFrame.metadata.timestamp || frames[currentIndex].timestamp
+ currentFrame.metadata.timestamp || frames[currentIndex].timestamp || ""
);
const totalMs = timeRange.end.getTime() - timeRange.start.getTime();
@@ -50,10 +50,12 @@ export function CurrentTimeIndicator({
<div className="w-0.5 h-full bg-foreground/50" />
<div className="absolute bottom-[-20px] text-xs text-foreground bg-background px-1 rounded-sm border whitespace-nowrap">
{new Date(
- currentFrame.metadata.timestamp || frames[currentIndex].timestamp
+ currentFrame.metadata.timestamp ||
+ frames[currentIndex].timestamp ||
+ ""
).toLocaleTimeString([], {
- hour: '2-digit',
- minute: '2-digit'
+ hour: "2-digit",
+ minute: "2-digit",
})}
</div>
</div>
diff --git a/screenpipe-app-tauri/components/timeline/time-labels.tsx b/screenpipe-app-tauri/components/timeline/time-labels.tsx
index f2c12bcc1..6c8394746 100644
--- a/screenpipe-app-tauri/components/timeline/time-labels.tsx
+++ b/screenpipe-app-tauri/components/timeline/time-labels.tsx
@@ -11,7 +11,7 @@ interface TimeLabelsProps {
export function TimeLabels({ timeRange, className }: TimeLabelsProps) {
const totalHours =
(timeRange.end.getTime() - timeRange.start.getTime()) / (1000 * 60 * 60);
- const labelCount = Math.min(14, Math.ceil(totalHours / 1)); // One label every ~2 hours, max 7 labels
+ const labelCount = Math.min(7, Math.ceil(totalHours / 2)); // One label every ~2 hours, max 7 labels
return (
<div
diff --git a/screenpipe-app-tauri/components/timeline/timeline-block.tsx b/screenpipe-app-tauri/components/timeline/timeline-block.tsx
index 8516030ad..aa08bfcd1 100644
--- a/screenpipe-app-tauri/components/timeline/timeline-block.tsx
+++ b/screenpipe-app-tauri/components/timeline/timeline-block.tsx
@@ -13,91 +13,92 @@ interface TimelineBlocksProps {
}
export function TimelineBlocks({ frames, timeRange }: TimelineBlocksProps) {
- // Move helper functions inside component
+ // Cache colors to avoid recalculating
+ const colorCache = useMemo(() => new Map<string, string>(), []);
+
const getAppColor = (appName: string): string => {
+ const cached = colorCache.get(appName);
+ if (cached) return cached;
+
let hash = 0;
for (let i = 0; i < appName.length; i++) {
hash = appName.charCodeAt(i) + ((hash << 5) - hash);
}
const hue = Math.abs(hash) % 360;
- return `hsl(${hue}, 70%, 50%)`;
+ const color = `hsl(${hue}, 70%, 50%)`;
+ colorCache.set(appName, color);
+ return color;
};
- // Memoize the heavy calculation
+ // Optimize block calculation by sampling frames
const blocks = useMemo(() => {
if (frames.length === 0) return [];
+ // Sample frames based on time range duration
+ const timeRangeMs = timeRange.end.getTime() - timeRange.start.getTime();
+ const sampleInterval = Math.max(1, Math.floor(timeRangeMs / 200)); // Adjust 200 to control detail level
+
+ const sampledFrames = frames.filter((_, index) => index % sampleInterval === 0);
+
const blocks: TimeBlock[] = [];
let currentBlock: TimeBlock | null = null;
- const sortedFrames = [...frames].sort(
- (a, b) =>
- new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime()
- );
+ sampledFrames
+ .sort((a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime())
+ .forEach((frame) => {
+ const timestamp = new Date(frame.timestamp);
+ const appName = frame.devices[0].metadata.app_name;
- sortedFrames.forEach((frame) => {
- const timestamp = new Date(frame.timestamp);
- const appName = frame.devices[0].metadata.app_name;
+ if (timestamp < timeRange.start || timestamp > timeRange.end) return;
- if (timestamp < timeRange.start || timestamp > timeRange.end) {
- return;
- }
-
- if (!currentBlock) {
- currentBlock = {
- appName,
- startTime: timestamp,
- endTime: timestamp,
- color: getAppColor(appName),
- };
- } else if (currentBlock.appName !== appName) {
- currentBlock.endTime = timestamp;
- blocks.push(currentBlock);
- currentBlock = {
- appName,
- startTime: timestamp,
- endTime: timestamp,
- color: getAppColor(appName),
- };
- } else {
- currentBlock.endTime = timestamp;
- }
- });
-
- if (currentBlock) {
- blocks.push(currentBlock);
- }
+ if (!currentBlock) {
+ currentBlock = {
+ appName,
+ startTime: timestamp,
+ endTime: timestamp,
+ color: getAppColor(appName),
+ };
+ } else if (currentBlock.appName !== appName) {
+ blocks.push(currentBlock);
+ currentBlock = {
+ appName,
+ startTime: timestamp,
+ endTime: timestamp,
+ color: getAppColor(appName),
+ };
+ } else {
+ currentBlock.endTime = timestamp;
+ }
+ });
+ if (currentBlock) blocks.push(currentBlock);
return blocks;
- }, [frames, timeRange]);
+ }, [frames, timeRange, colorCache]);
+ // Optimize rendering by using CSS transform instead of percentage calculations
return (
- <>
+ <div className="absolute inset-0">
{blocks.map((block, index) => {
const totalMs = timeRange.end.getTime() - timeRange.start.getTime();
- const blockStart =
- ((block.startTime.getTime() - timeRange.start.getTime()) / totalMs) *
- 100;
- const blockWidth =
- ((block.endTime.getTime() - block.startTime.getTime()) / totalMs) *
- 100;
+ const blockStart = (block.startTime.getTime() - timeRange.start.getTime()) / totalMs;
+ const blockWidth = (block.endTime.getTime() - block.startTime.getTime()) / totalMs;
+
+ if (blockWidth < 0.001) return null; // Skip tiny blocks
return (
<div
- key={index}
+ key={`${block.appName}-${index}`}
className="absolute top-0 h-full"
style={{
- left: `${Math.max(0, Math.min(100, blockStart))}%`,
- width: `${Math.max(0, Math.min(100 - blockStart, blockWidth))}%`,
+ transform: `translateX(${blockStart * 100}%)`,
+ width: `${blockWidth * 100}%`,
backgroundColor: block.color,
- transition: "all 0.1s ease-in-out",
+ willChange: 'transform',
}}
- title={`${
- block.appName
- }\n${block.startTime.toLocaleTimeString()} - ${block.endTime.toLocaleTimeString()}`}
+ title={`${block.appName}\n${block.startTime.toLocaleTimeString()} - ${block.endTime.toLocaleTimeString()}`}
/>
);
})}
- </>
+ </div>
);
}
diff --git a/screenpipe-app-tauri/src-tauri/Cargo.lock b/screenpipe-app-tauri/src-tauri/Cargo.lock
index 2a214ad5d..53f007971 100755
--- a/screenpipe-app-tauri/src-tauri/Cargo.lock
+++ b/screenpipe-app-tauri/src-tauri/Cargo.lock
@@ -3781,10 +3781,13 @@ version = "0.9.2"
dependencies = [
"anyhow",
"axum",
+ "base64 0.22.1",
+ "cocoa 0.26.0",
"dirs 5.0.1",
"fix-path-env",
"http 0.2.12",
"log",
+ "objc",
"reqwest",
"serde",
"serde_derive",
diff --git a/screenpipe-server/src/video_cache.rs b/screenpipe-server/src/video_cache.rs
index 96574f34e..6bc84cf95 100644
--- a/screenpipe-server/src/video_cache.rs
+++ b/screenpipe-server/src/video_cache.rs
@@ -849,11 +849,11 @@ impl OrderedFrameStreamer {
return Ok(());
};
- // Determine bucket range
+ // Determine bucket range - FIXED: Reversed logic for descending order
let bucket_range = if self.descending {
- (current_bucket - self.bucket_size)..=current_bucket
- } else {
current_bucket..=(current_bucket + self.bucket_size)
+ } else {
+ (current_bucket - self.bucket_size)..=current_bucket
};
// Find frames ready to be sent (outside current bucket)
@@ -865,11 +865,14 @@ impl OrderedFrameStreamer {
if !ready_timestamps.is_empty() {
// Sort timestamps based on direction
- if self.descending {
- ready_timestamps.sort_by(|a, b| b.cmp(a));
- } else {
- ready_timestamps.sort();
- }
+ ready_timestamps.sort_by(|a, b| {
+ if self.descending {
+ // FIXED: Ensure we process older timestamps first in descending mode
+ a.cmp(b)
+ } else {
+ b.cmp(a)
+ }
+ });
// Send frames and update buffer
for ts in ready_timestamps {