Files
openchamber/packages/ui/src/components/chat/MobileAgentButton.tsx
T
Bohdan Triapitsyn 8587db89b1 perf: reduce UI render fanout and scroll jitter
- Cut broad render fanout across the app by replacing shared-store whole-object subscriptions with leaf selectors, memoizing hot chrome boundaries, and isolating disabled global providers from live session/message state. This keeps header controls, composer toolbars, side panels, and other non-hot UI surfaces from repainting on every assistant update or keystroke.

- Rework sidebar session ordering so recent, project groups, and worktree groups derive from one ordering source while avoiding streaming-time thrash. The sidebar now uses a stabilized session snapshot, preserves structural identity for unchanged rows, reads live row status/details per session, and applies a one-shot sort bump on idle->busy instead of continuously resorting during activity.

- Fix chat/input scroll instability by separating viewport-resize handling from message-growth handling, disabling conflicting native scroll anchoring, and stopping textarea autosize from collapsing on every growth keystroke. This removes the multiline typing jiggle during streaming and reduces unnecessary composer rerenders.

- Also gate voice context wiring behind voice-mode enablement and codify the learned render/scroll/order anti-patterns in AGENTS.md so future changes avoid the same classes of regressions.
2026-04-04 02:19:55 +03:00

97 lines
3.4 KiB
TypeScript

import React from 'react';
import { cn } from '@/lib/utils';
import { useConfigStore } from '@/stores/useConfigStore';
import { useSessionUIStore } from '@/sync/session-ui-store';
import { useSelectionStore } from '@/sync/selection-store';
import { getAgentDisplayName } from './mobileControlsUtils';
import { getAgentColor } from '@/lib/agentColors';
interface MobileAgentButtonProps {
onCycleAgent: () => void;
onOpenAgentPanel: () => void;
className?: string;
}
const LONG_PRESS_MS = 500;
// NOTE: Use pointer events instead of onClick to keep soft keyboard open on mobile
export const MobileAgentButton: React.FC<MobileAgentButtonProps> = ({ onCycleAgent, onOpenAgentPanel, className }) => {
const currentAgentName = useConfigStore((state) => state.currentAgentName);
const getVisibleAgents = useConfigStore((state) => state.getVisibleAgents);
const currentSessionId = useSessionUIStore((state) => state.currentSessionId);
const sessionAgentName = useSelectionStore((state) =>
currentSessionId ? state.getSessionAgentSelection(currentSessionId) : null
);
const agents = getVisibleAgents();
const uiAgentName = currentSessionId ? (sessionAgentName || currentAgentName) : currentAgentName;
const agentLabel = getAgentDisplayName(agents, uiAgentName);
const agentColor = getAgentColor(uiAgentName);
const longPressTimerRef = React.useRef<ReturnType<typeof setTimeout> | null>(null);
const isLongPressRef = React.useRef(false);
const handlePointerDown = () => {
isLongPressRef.current = false;
longPressTimerRef.current = setTimeout(() => {
isLongPressRef.current = true;
onOpenAgentPanel();
}, LONG_PRESS_MS);
};
// Use onPointerUp (not onClick) to prevent focus transfer that closes mobile keyboard
const handlePointerUp = () => {
if (longPressTimerRef.current) {
clearTimeout(longPressTimerRef.current);
longPressTimerRef.current = null;
}
if (!isLongPressRef.current) {
onCycleAgent();
}
};
const handlePointerLeave = () => {
if (longPressTimerRef.current) {
clearTimeout(longPressTimerRef.current);
longPressTimerRef.current = null;
}
};
React.useEffect(() => {
return () => {
if (longPressTimerRef.current) {
clearTimeout(longPressTimerRef.current);
}
};
}, []);
return (
<button
type="button"
onPointerDown={handlePointerDown}
onPointerUp={handlePointerUp} // Don't use onClick - it closes mobile keyboard
onPointerLeave={handlePointerLeave}
onContextMenu={(e) => e.preventDefault()}
className={cn(
'inline-flex min-w-0 items-center select-none',
'rounded-lg border border-border/50 px-1.5',
'typography-micro font-medium',
'focus:outline-none hover:bg-[var(--interactive-hover)]',
'touch-none',
className
)}
style={{
height: '26px',
maxHeight: '26px',
minHeight: '26px',
color: `var(${agentColor.var})`,
}}
title={agentLabel}
>
<span className="truncate">{agentLabel}</span>
</button>
);
};
export default MobileAgentButton;