import React, { useEffect } from "react";
import { View, StyleSheet, Dimensions } from "react-native";
import { Text } from "@/components/ui/text";
import { useToast, ToastType } from "@/lib/toast-store";
import {
CheckCircle2,
AlertCircle,
AlertTriangle,
Lightbulb,
} from "@/lib/icons";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import Animated, {
useSharedValue,
useAnimatedStyle,
withSpring,
withTiming,
runOnJS,
} from "react-native-reanimated";
import { Gesture, GestureDetector } from "react-native-gesture-handler";
const { width: SCREEN_WIDTH } = Dimensions.get("window");
const SWIPE_THRESHOLD = SCREEN_WIDTH * 0.4;
const TOAST_VARIANTS: Record<
ToastType,
{
bg: string;
border: string;
icon: React.ReactNode;
}
> = {
success: {
bg: "rgba(34, 197, 94, 0.05)",
border: "#22c55e",
icon: ,
},
info: {
bg: "rgba(14, 165, 233, 0.05)",
border: "#0ea5e9",
icon: ,
},
warning: {
bg: "rgba(245, 158, 11, 0.05)",
border: "#f59e0b",
icon: ,
},
error: {
bg: "rgba(239, 68, 68, 0.05)",
border: "#ef4444",
icon: ,
},
};
export function Toast() {
const { visible, type, title, message, hide, duration } = useToast();
const insets = useSafeAreaInsets();
const opacity = useSharedValue(0);
const translateY = useSharedValue(-100);
const translateX = useSharedValue(0);
useEffect(() => {
if (visible) {
opacity.value = withTiming(1, { duration: 300 });
translateY.value = withSpring(0, { damping: 15, stiffness: 100 });
translateX.value = 0;
const timer = setTimeout(() => {
handleHide();
}, duration);
return () => clearTimeout(timer);
}
}, [visible]);
const handleHide = () => {
opacity.value = withTiming(0, { duration: 300 });
translateY.value = withTiming(-100, { duration: 300 }, () => {
runOnJS(hide)();
});
};
const swipeGesture = Gesture.Pan()
.onUpdate((event) => {
translateX.value = event.translationX;
})
.onEnd((event) => {
if (Math.abs(event.translationX) > SWIPE_THRESHOLD) {
translateX.value = withTiming(
event.translationX > 0 ? SCREEN_WIDTH : -SCREEN_WIDTH,
{ duration: 200 },
() => runOnJS(handleHide)(),
);
} else {
translateX.value = withSpring(0);
}
});
const animatedStyle = useAnimatedStyle(() => ({
opacity: opacity.value,
transform: [
{ translateY: translateY.value },
{ translateX: translateX.value },
],
}));
if (!visible) return null;
const variant = TOAST_VARIANTS[type];
return (
{variant.icon}
{title}
{message}
);
}
const styles = StyleSheet.create({
container: {
position: "absolute",
left: 16,
right: 16,
zIndex: 9999,
},
});