Files
openchamber/packages/ui/src/hooks/useDrawerSwipe.ts
T
25d616f009 feat(mobile): refactor drawer system and session status bar (#494)
* feat(mobile): refactor drawer system with swipe gestures and improved status bar

- Add DrawerContext for centralized drawer state management
- Implement swipe gesture support for left/right drawers
- Refactor MobileSessionStatusBar with token usage indicator
- Update Header component with drawer toggle props
- Improve RightSidebar touch handling
- Add mobile-specific styling improvements
- Update UI store for drawer state management

* feat(mobile): update empty session hint text to clarify swipe gesture area

* fix(mobile): restore MobileAgentButton tap-to-cycle and long-press behavior

- Revert removal of tap-to-cycle agent switching functionality
- Re-add onCycleAgent prop and long-press detection (500ms)
- Click now cycles through primary agents, long-press opens selector panel
- Fixes regression from commit 569cc411

* feat(mobile): add project switcher bar and fix button interactions

- Add ProjectBar component to MobileSessionStatusBar for quick project switching
- Add useProjectStatus hook to track project-level session indicators
- Integrate project store with directory store for project management
- Add project status indicators (running/unread) to session status bar
- Fix MobileAgentButton pointer event handling with preventDefault
- Support horizontal scroll in project bar with touch gesture isolation

* feat(mobile): add long-press to remove projects and filter sessions by project

* fix: improve mobile agent switching UX

* fix: move drawer swipe hook to dedicated file

* refactor(mobile): simplify session status bar, remove More/Less toggle button

* fix: git diff navigation on mobile in sidebar mode

* feat(mobile): add configurable status bar and refine mobile chat controls

---------

Co-authored-by: Jovines <jovines@qq.com>
Co-authored-by: Bohdan Triapitsyn <artmore@protonmail.com>
2026-02-24 11:44:43 +02:00

157 lines
5.3 KiB
TypeScript

import React from 'react';
import { animate } from 'motion/react';
import { useDrawer } from '@/contexts/DrawerContext';
export function useDrawerSwipe() {
const drawer = useDrawer();
const touchStartXRef = React.useRef(0);
const touchStartYRef = React.useRef(0);
const isHorizontalSwipeRef = React.useRef<boolean | null>(null);
const isDraggingDrawerRef = React.useRef<'left' | 'right' | null>(null);
const handleTouchStart = React.useCallback((e: React.TouchEvent) => {
touchStartXRef.current = e.touches[0].clientX;
touchStartYRef.current = e.touches[0].clientY;
isHorizontalSwipeRef.current = null;
isDraggingDrawerRef.current = null;
}, []);
const handleTouchMove = React.useCallback((e: React.TouchEvent) => {
const currentX = e.touches[0].clientX;
const currentY = e.touches[0].clientY;
const deltaX = currentX - touchStartXRef.current;
const deltaY = currentY - touchStartYRef.current;
if (isHorizontalSwipeRef.current === null) {
if (Math.abs(deltaX) > 5 || Math.abs(deltaY) > 5) {
isHorizontalSwipeRef.current = Math.abs(deltaX) > Math.abs(deltaY);
}
}
if (isHorizontalSwipeRef.current === true) {
e.preventDefault();
const leftDrawerWidthPx = drawer.leftDrawerWidth.current || window.innerWidth * 0.85;
const rightDrawerWidthPx = drawer.rightDrawerWidth.current || window.innerWidth * 0.85;
if (isDraggingDrawerRef.current === null) {
if (drawer.leftDrawerOpen && deltaX > 10) {
isDraggingDrawerRef.current = 'left';
} else if (drawer.rightDrawerOpen && deltaX < -10) {
isDraggingDrawerRef.current = 'right';
} else if (!drawer.leftDrawerOpen && !drawer.rightDrawerOpen) {
if (deltaX > 30) {
isDraggingDrawerRef.current = 'left';
} else if (deltaX < -30) {
isDraggingDrawerRef.current = 'right';
}
}
}
if (isDraggingDrawerRef.current === 'left') {
if (drawer.leftDrawerOpen) {
const progress = Math.max(0, Math.min(1, deltaX / leftDrawerWidthPx));
drawer.leftDrawerX.set(-leftDrawerWidthPx * (1 - progress));
} else {
const progress = Math.max(0, Math.min(1, deltaX / leftDrawerWidthPx));
drawer.leftDrawerX.set(-leftDrawerWidthPx + (leftDrawerWidthPx * progress));
}
}
if (isDraggingDrawerRef.current === 'right') {
if (drawer.rightDrawerOpen) {
const progress = Math.max(0, Math.min(1, -deltaX / rightDrawerWidthPx));
drawer.rightDrawerX.set(rightDrawerWidthPx * (1 - progress));
} else {
const progress = Math.max(0, Math.min(1, -deltaX / rightDrawerWidthPx));
drawer.rightDrawerX.set(rightDrawerWidthPx - (rightDrawerWidthPx * progress));
}
}
}
}, [drawer]);
const handleTouchEnd = React.useCallback((e: React.TouchEvent) => {
if (isHorizontalSwipeRef.current !== true) return;
const endX = e.changedTouches[0].clientX;
const deltaX = endX - touchStartXRef.current;
const velocityThreshold = 500;
const progressThreshold = 0.3;
const leftDrawerWidthPx = drawer.leftDrawerWidth.current || window.innerWidth * 0.85;
const rightDrawerWidthPx = drawer.rightDrawerWidth.current || window.innerWidth * 0.85;
if (isDraggingDrawerRef.current === 'left') {
const isOpen = drawer.leftDrawerOpen;
const currentX = drawer.leftDrawerX.get();
const progress = isOpen
? 1 - Math.abs(currentX) / leftDrawerWidthPx
: 1 + currentX / leftDrawerWidthPx;
const shouldComplete = progress > progressThreshold || Math.abs(deltaX * 10) > velocityThreshold;
if (shouldComplete) {
const targetX = isOpen ? -leftDrawerWidthPx : 0;
animate(drawer.leftDrawerX, targetX, {
type: 'spring',
stiffness: 400,
damping: 35,
mass: 0.8,
});
drawer.setMobileLeftDrawerOpen(!isOpen);
} else {
const targetX = isOpen ? 0 : -leftDrawerWidthPx;
animate(drawer.leftDrawerX, targetX, {
type: 'spring',
stiffness: 400,
damping: 35,
mass: 0.8,
});
}
isDraggingDrawerRef.current = null;
return;
}
if (isDraggingDrawerRef.current === 'right') {
const isOpen = drawer.rightDrawerOpen;
const currentX = drawer.rightDrawerX.get();
const progress = isOpen
? 1 - Math.abs(currentX) / rightDrawerWidthPx
: 1 - currentX / rightDrawerWidthPx;
const shouldComplete = progress > progressThreshold || Math.abs(deltaX * 10) > velocityThreshold;
if (shouldComplete) {
const targetX = isOpen ? rightDrawerWidthPx : 0;
animate(drawer.rightDrawerX, targetX, {
type: 'spring',
stiffness: 400,
damping: 35,
mass: 0.8,
});
drawer.setRightSidebarOpen(!isOpen);
} else {
const targetX = isOpen ? 0 : rightDrawerWidthPx;
animate(drawer.rightDrawerX, targetX, {
type: 'spring',
stiffness: 400,
damping: 35,
mass: 0.8,
});
}
isDraggingDrawerRef.current = null;
return;
}
isHorizontalSwipeRef.current = null;
}, [drawer]);
return {
handleTouchStart,
handleTouchMove,
handleTouchEnd,
};
}