Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions electron/src/components/graph/GraphControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function GraphControls({
)}

<TouchButton
onClick={onSwitchToHistorical}
onClick={() => onSwitchToHistorical("button")}
variant="outline"
className={`h-auto px-3 py-3 text-base font-medium transition-colors ${
!isLiveMode
Expand Down Expand Up @@ -197,7 +197,7 @@ export function FloatingControlPanel({
)}

<TouchButton
onClick={onSwitchToHistorical}
onClick={() => onSwitchToHistorical("button")}
variant="outline"
className={`h-auto px-3 py-3 text-base font-medium transition-colors ${
!isLiveMode
Expand Down
43 changes: 37 additions & 6 deletions electron/src/components/graph/historicalMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,43 @@ export function useHistoricalMode({
);

// Switch to historical mode
const switchToHistoricalMode = useCallback(() => {
isInHistoricalModeRef.current = true;
captureHistoricalFreezeTimestamp();
stopAnimations(animationRefs);
lastProcessedCountRef.current = 0; // Reset processed count
}, [captureHistoricalFreezeTimestamp, animationRefs, lastProcessedCountRef]);
const switchToHistoricalMode = useCallback(
(origin?: "button" | "gesture" | null) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the type instead?

isInHistoricalModeRef.current = true;

// Preserve current scale
if (origin === "gesture") {
try {
if (uplotRef.current) {
const xScale = uplotRef.current.scales.x;
const yScale = uplotRef.current.scales.y;
manualScaleRef.current = {
x: {
min: xScale?.min ?? 0,
max: xScale?.max ?? 0,
},
y: {
min: yScale?.min ?? 0,
max: yScale?.max ?? 1,
},
};
}
} catch (err) {
console.warn(
"Error preserving chart scales when switching to historical mode:",
err,
);
}
} else {
manualScaleRef.current = null;
}

captureHistoricalFreezeTimestamp();
stopAnimations(animationRefs);
lastProcessedCountRef.current = 0; // Reset processed count
},
[captureHistoricalFreezeTimestamp, animationRefs, lastProcessedCountRef],
);

// Switch back to live mode
const switchToLiveMode = useCallback(() => {
Expand Down
7 changes: 5 additions & 2 deletions electron/src/components/graph/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Unit } from "@/control/units";
import { TimeSeries } from "@/lib/timeseries";
import { RefObject } from "react";

export type SwitchOrigin = "button" | "gesture" | null;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type should not be nullable. Instead, use ? in a parameter, where appropriate.


// Prop-based sync types
export type PropGraphSync = {
timeWindow: number | "all";
Expand All @@ -11,6 +13,7 @@ export type PropGraphSync = {
xRange?: { min: number; max: number };
historicalFreezeTimestamp?: number | null;
showFromTimestamp?: number | null;
historicalSwitchOrigin?: SwitchOrigin;
onTimeWindowChange?: (graphId: string, timeWindow: number | "all") => void;
onViewModeChange?: (
graphId: string,
Expand Down Expand Up @@ -83,7 +86,7 @@ export type ControlProps = {
isLiveMode: boolean;
onTimeWindowChange: (timeWindow: number | "all") => void;
onSwitchToLive: () => void;
onSwitchToHistorical: () => void;
onSwitchToHistorical: (origin?: SwitchOrigin) => void;
onExport?: () => void;
timeWindowOptions?: TimeWindowOption[];
showFromTimestamp?: number | null;
Expand All @@ -100,7 +103,7 @@ export interface HistoricalModeHandlers {
captureHistoricalFreezeTimestamp: () => number;
getHistoricalEndTimestamp: () => number;
handleHistoricalTimeWindow: (timeWindow: number | "all") => void;
switchToHistoricalMode: () => void;
switchToHistoricalMode: (origin?: SwitchOrigin) => void;
switchToLiveMode: () => void;
}

Expand Down
2 changes: 1 addition & 1 deletion electron/src/components/graph/useBigGraphEffects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export function useBigGraphEffects({
const newIsLiveMode = syncGraph.isLiveMode;

if (lastState.isLiveMode && !newIsLiveMode) {
historicalMode.switchToHistoricalMode();
historicalMode.switchToHistoricalMode(syncGraph.historicalSwitchOrigin);
} else if (!lastState.isLiveMode && newIsLiveMode) {
historicalMode.switchToLiveMode();

Expand Down
9 changes: 9 additions & 0 deletions electron/src/components/graph/useGraphSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function useGraphSync(exportGroupId?: string) {
null,
);

const historicalSwitchOriginRef = useRef<"button" | "gesture" | null>(null);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, use the type instead of inlineing it

const historicalFreezeTimestampRef = useRef<number | null>(null);
const graphDataRef = useRef<Map<string, () => GraphExportData | null>>(
new Map(),
Expand Down Expand Up @@ -65,6 +66,7 @@ export function useGraphSync(exportGroupId?: string) {
showFromTimestamp?: number | null;
clearHistoricalFreeze?: boolean;
setHistoricalFreeze?: boolean;
historicalSwitchOrigin?: "button" | "gesture" | null;
},
requestId?: number,
) => {
Expand Down Expand Up @@ -104,6 +106,10 @@ export function useGraphSync(exportGroupId?: string) {
historicalFreezeTimestampRef.current = Date.now();
}

if (updates.historicalSwitchOrigin !== undefined) {
historicalSwitchOriginRef.current = updates.historicalSwitchOrigin;
}

clearChangeSource();
});
},
Expand Down Expand Up @@ -164,6 +170,7 @@ export function useGraphSync(exportGroupId?: string) {
viewMode: "manual",
isLiveMode: false,
setHistoricalFreeze: historicalFreezeTimestampRef.current === null,
historicalSwitchOrigin: "gesture",
});
},
[updateSyncState],
Expand Down Expand Up @@ -210,6 +217,7 @@ export function useGraphSync(exportGroupId?: string) {
isLiveMode: false,
viewMode: "manual",
setHistoricalFreeze: true,
historicalSwitchOrigin: "button",
});
}, [updateSyncState]);

Expand Down Expand Up @@ -242,6 +250,7 @@ export function useGraphSync(exportGroupId?: string) {
xRange,
historicalFreezeTimestamp: historicalFreezeTimestampRef.current,
showFromTimestamp,
historicalSwitchOrigin: historicalSwitchOriginRef.current,
onTimeWindowChange: handleTimeWindowChange,
onViewModeChange: handleViewModeChange,
onZoomChange: handleZoomChangeThrottled,
Expand Down
Loading