This repository has been archived on 2026-10-25. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Sable/src/app/components/SwipeableChatWrapper.tsx
T
2026-02-22 17:45:23 -06:00

96 lines
2.8 KiB
TypeScript

import React, { ReactNode } from 'react';
import { motion, useMotionValue, useSpring } from 'framer-motion';
import { useDrag } from '@use-gesture/react';
import { useAtomValue } from 'jotai';
import { settingsAtom, RightSwipeAction } from '../state/settings';
import { mobileOrTablet } from '../utils/user-agent';
interface SwipeableChatWrapperProps {
children: ReactNode;
onOpenSidebar?: () => void;
onOpenMembers?: () => void;
onReply?: () => void;
}
export function SwipeableChatWrapper({
children,
onOpenSidebar,
onOpenMembers,
onReply
}: SwipeableChatWrapperProps) {
const settings = useAtomValue(settingsAtom);
const x = useMotionValue(0);
const springX = useSpring(x, { stiffness: 400, damping: 40 });
const bind = useDrag(({ active, movement: [mx], velocity: [vx], direction: [dx] }) => {
if (!settings.mobileGestures || !mobileOrTablet()) return;
let val = mx;
const canSwipeRight = !!onOpenSidebar;
const canSwipeLeft = settings.rightSwipeAction === RightSwipeAction.Members
? !!onOpenMembers
: !!onReply;
if (!canSwipeRight && val > 0) val = 0;
if (!canSwipeLeft && val < 0) val = 0;
if (active) {
x.set(val);
} else {
const swipeThreshold = 120;
const velocityThreshold = 0.5;
if (val > swipeThreshold || (vx > velocityThreshold && dx > 0 && val > 0)) {
onOpenSidebar?.();
} else if (val < -swipeThreshold || (vx > velocityThreshold && dx < 0 && val < 0)) {
if (settings.rightSwipeAction === RightSwipeAction.Members) {
onOpenMembers?.();
} else {
onReply?.();
}
}
x.set(0);
}
}, {
axis: 'x',
bounds: { left: -200, right: 200 },
rubberband: true,
filterTaps: true,
});
if (!settings.mobileGestures || !mobileOrTablet()) {
return (
<div style={{ display: 'flex', flexDirection: 'column', flexGrow: 1, height: '100%', width: '100%' }}>
{children}
</div>
);
}
return (
<div
{...bind()}
style={{
touchAction: 'pan-y',
overflow: 'hidden',
display: 'flex',
flexDirection: 'column',
flexGrow: 1,
height: '100%',
width: '100%',
}}
>
<motion.div
style={{
x: springX,
display: 'flex',
flexDirection: 'column',
flexGrow: 1,
height: '100%',
}}
>
{children}
</motion.div>
</div>
);
}