2026-02-05 00:07:24 +08:00
|
|
|
import React from 'react';
|
|
|
|
|
import { cn } from '@/lib/utils';
|
|
|
|
|
import { useConfigStore } from '@/stores/useConfigStore';
|
|
|
|
|
import { useSessionStore } from '@/stores/useSessionStore';
|
|
|
|
|
import { getAgentDisplayName } from './mobileControlsUtils';
|
|
|
|
|
import { getAgentColor } from '@/lib/agentColors';
|
|
|
|
|
|
|
|
|
|
interface MobileAgentButtonProps {
|
2026-02-24 17:44:43 +08:00
|
|
|
onCycleAgent: () => void;
|
2026-02-05 00:07:24 +08:00
|
|
|
onOpenAgentPanel: () => void;
|
|
|
|
|
className?: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 17:44:43 +08:00
|
|
|
const LONG_PRESS_MS = 500;
|
|
|
|
|
|
2026-02-27 02:12:29 +08:00
|
|
|
// NOTE: Use pointer events instead of onClick to keep soft keyboard open on mobile
|
|
|
|
|
export const MobileAgentButton: React.FC<MobileAgentButtonProps> = ({ onCycleAgent, onOpenAgentPanel, className }) => {
|
2026-02-05 00:07:24 +08:00
|
|
|
const { currentAgentName, getVisibleAgents } = useConfigStore();
|
|
|
|
|
const currentSessionId = useSessionStore((state) => state.currentSessionId);
|
|
|
|
|
const sessionAgentName = useSessionStore((state) =>
|
|
|
|
|
currentSessionId ? state.getSessionAgentSelection(currentSessionId) : null
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const agents = getVisibleAgents();
|
|
|
|
|
const uiAgentName = currentSessionId ? (sessionAgentName || currentAgentName) : currentAgentName;
|
|
|
|
|
const agentLabel = getAgentDisplayName(agents, uiAgentName);
|
|
|
|
|
const agentColor = getAgentColor(uiAgentName);
|
|
|
|
|
|
2026-02-27 02:12:29 +08:00
|
|
|
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 = () => {
|
2026-02-24 17:44:43 +08:00
|
|
|
if (longPressTimerRef.current) {
|
|
|
|
|
clearTimeout(longPressTimerRef.current);
|
|
|
|
|
longPressTimerRef.current = null;
|
|
|
|
|
}
|
2026-02-27 02:12:29 +08:00
|
|
|
if (!isLongPressRef.current) {
|
|
|
|
|
onCycleAgent();
|
|
|
|
|
}
|
|
|
|
|
};
|
2026-02-24 17:44:43 +08:00
|
|
|
|
2026-02-27 02:12:29 +08:00
|
|
|
const handlePointerLeave = () => {
|
|
|
|
|
if (longPressTimerRef.current) {
|
|
|
|
|
clearTimeout(longPressTimerRef.current);
|
|
|
|
|
longPressTimerRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
};
|
2026-02-24 17:44:43 +08:00
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2026-02-27 02:12:29 +08:00
|
|
|
return () => {
|
|
|
|
|
if (longPressTimerRef.current) {
|
|
|
|
|
clearTimeout(longPressTimerRef.current);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
2026-02-24 17:44:43 +08:00
|
|
|
|
2026-02-05 00:07:24 +08:00
|
|
|
return (
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
2026-02-27 02:12:29 +08:00
|
|
|
onPointerDown={handlePointerDown}
|
|
|
|
|
onPointerUp={handlePointerUp} // Don't use onClick - it closes mobile keyboard
|
|
|
|
|
onPointerLeave={handlePointerLeave}
|
|
|
|
|
onContextMenu={(e) => e.preventDefault()}
|
2026-02-05 00:07:24 +08:00
|
|
|
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)]',
|
2026-02-24 17:44:43 +08:00
|
|
|
'touch-none',
|
2026-02-05 00:07:24 +08:00
|
|
|
className
|
|
|
|
|
)}
|
|
|
|
|
style={{
|
|
|
|
|
height: '26px',
|
|
|
|
|
maxHeight: '26px',
|
|
|
|
|
minHeight: '26px',
|
|
|
|
|
color: `var(${agentColor.var})`,
|
|
|
|
|
}}
|
|
|
|
|
title={agentLabel}
|
|
|
|
|
>
|
2026-02-27 02:12:29 +08:00
|
|
|
<span className="truncate">{agentLabel}</span>
|
2026-02-05 00:07:24 +08:00
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default MobileAgentButton;
|