"use client"; import { useRef } from "react"; import { motion, useMotionValue, useTransform, animate, PanInfo, MotionValue, } from "framer-motion"; import RestaurantCard from "./RestaurantCard"; import { Restaurant, SwipeDirection } from "@/types"; const SWIPE_THRESHOLD = 120; const getExitX = () => typeof window !== "undefined" ? window.innerWidth * 1.5 : 600; const ROTATION_RANGE = 18; interface SwipeableCardProps { restaurant: Restaurant; isTop: boolean; onSwipe: (direction: SwipeDirection) => void; registerSwipe?: (fn: (direction: SwipeDirection) => void) => void; likeCount: number; } function SwipeOverlay({ x }: { x: MotionValue }) { const likeOpacity = useTransform(x, [0, SWIPE_THRESHOLD], [0, 1]); const nopeOpacity = useTransform(x, [-SWIPE_THRESHOLD, 0], [1, 0]); return ( <> LIKE NOPE ); } export default function SwipeableCard({ restaurant, isTop, onSwipe, registerSwipe, likeCount, }: SwipeableCardProps) { const x = useMotionValue(0); const rotate = useTransform(x, [-300, 300], [-ROTATION_RANGE, ROTATION_RANGE]); const opacity = useTransform(x, [-300, -100, 0, 100, 300], [0.5, 1, 1, 1, 0.5]); const isSwiping = useRef(false); const flyOut = (direction: SwipeDirection) => { if (isSwiping.current) return; isSwiping.current = true; const exit = getExitX(); const exitX = direction === "right" ? exit : -exit; animate(x, exitX, { type: "spring", stiffness: 600, damping: 40, onComplete: () => onSwipe(direction), }); }; if (registerSwipe) { registerSwipe(flyOut); } const handleDragEnd = (_: unknown, info: PanInfo) => { const offsetX = info.offset.x; if (offsetX > SWIPE_THRESHOLD) { flyOut("right"); } else if (offsetX < -SWIPE_THRESHOLD) { flyOut("left"); } else { animate(x, 0, { type: "spring", stiffness: 500, damping: 30 }); } }; return ( ); }