Skip to content

Commit

Permalink
refactor(extension): Improve floating sphere drag interaction and sta…
Browse files Browse the repository at this point in the history
…te management

- Add `isDraggingRef` to track dragging state more reliably
- Optimize drag event handling with `useCallback`
- Add additional event listeners for mouse leave and mouse out
- Remove unused `isHovered` state
- Enhance drag and dropdown visibility state management
  • Loading branch information
pftom committed Feb 2, 2025
1 parent d7d8f50 commit 8e589f5
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions apps/extension/src/entrypoints/floatingSphere-csui.content/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ export const App = () => {
// 加载快捷键
const [shortcut] = useState<string>(reflyEnv.getOsType() === 'OSX' ? '⌘ J' : 'Ctrl J');
const [isDragging, setIsDragging] = useState(false);
const isDraggingRef = useRef(false);
const [position, setPosition] = useState({ y: 0 });
const sphereRef = useRef<HTMLDivElement>(null);
const dropdownRef = useRef<HTMLDivElement>(null);
const dragStartPos = useRef({ y: 0, offsetY: 0 });
const [dropdownPosition, setDropdownPosition] = useState<'top' | 'bottom'>('top');
const [isHovered, setIsHovered] = useState(false);
const bottomDistanceRef = useRef(0);
const [isDropdownVisible, setIsDropdownVisible] = useState(false);
const timeoutRef = useRef<NodeJS.Timeout | null>(null);

// listen to copilotType
useListenToCopilotType();
Expand Down Expand Up @@ -133,17 +135,18 @@ export const App = () => {
}
};

const handleDragStart = (e: React.MouseEvent) => {
setIsDragging(true);
dragStartPos.current = {
y: e.clientY,
offsetY: position.y,
};
e.preventDefault();
};

const [isDropdownVisible, setIsDropdownVisible] = useState(false);
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
const handleDragStart = useCallback(
(e: React.MouseEvent) => {
isDraggingRef.current = true;
setIsDragging(true);
dragStartPos.current = {
y: e.clientY,
offsetY: position.y,
};
e.preventDefault();
},
[position.y],
);

const handleMouseEnter = useCallback(() => {
if (timeoutRef.current) {
Expand Down Expand Up @@ -262,7 +265,7 @@ export const App = () => {

useEffect(() => {
const handleMouseMove = (e: MouseEvent) => {
if (isDragging && sphereRef.current) {
if (isDraggingRef.current && sphereRef.current) {
const dy = e.clientY - dragStartPos.current.y;
const newY = dragStartPos.current.offsetY + dy;
updateSpherePosition(newY);
Expand All @@ -271,17 +274,23 @@ export const App = () => {
};

const handleMouseUp = () => {
console.log('handleMouseUp');
setIsDragging(false);
isDraggingRef.current = false;
};

document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', handleMouseUp);
document.addEventListener('mouseleave', handleMouseUp);
document.addEventListener('mouseout', handleMouseUp);

return () => {
document.removeEventListener('mousemove', handleMouseMove);
document.removeEventListener('mouseup', handleMouseUp);
document.removeEventListener('mouseleave', handleMouseUp);
document.removeEventListener('mouseout', handleMouseUp);
};
}, [isDragging]);
}, []);

const [isVisible, setIsVisible] = useState(true);

Expand Down

0 comments on commit 8e589f5

Please sign in to comment.