/* eslint-disable */ import React, { memo, useEffect, useRef, useState } from "react"; import { Image, StyleSheet, Text, TouchableOpacity, View, Animated } from "react-native"; import Icon from "react-native-vector-icons/MaterialCommunityIcons"; import FontAwesome from "react-native-vector-icons/FontAwesome"; type Props = { onClosePress: () => void; profile: string; name: string; datePublication: string; deleteAction?: () => Promise; currentUser?: any; }; const diffDateWithNow = (date) => { let startDate = new Date(date); // Do your operations let endDate = new Date(); let seconds = (endDate.getTime() - startDate.getTime()) / 1000; let minutes = seconds / 60; let hours = minutes / 60; let days = hours / 24; let current: string; if (days >= 1) { current = days == 1 ? "dia" : "dias"; return Math.trunc(days) + " " + current; } else if (hours > 1) { current = days == 1 ? "hora" : "horas"; return Math.trunc(hours) + " " + current; } else { current = minutes == 1 ? "minuto" : "minutos"; return Math.trunc(hours) + " " + current; } }; export default memo(function UserView(props: Props) { const [deleting, setDeleting] = useState(false); const [clock, setClock] = useState(null); async function deleteStory() { setDeleting(true); if(props.deleteAction) await props.deleteAction(); setDeleting(false); } const [timing, setTiming] = useState(0); /* const count = () => { // Will change fadeAnim value to 1 in 5 seconds Animated.timing(timing, { toValue: 8, duration: 500, useNativeDriver: true }).start(); }; */ function initClock() { const count = setInterval(() => { setTiming(prev => prev + 1 > 8 ? 0 : prev + 1); }, 1000); setClock(count); } function finishClock() { clearInterval(clock); } useEffect(() => { if(deleting) { initClock(); } else { finishClock(); } }, [deleting]); useEffect(() => { if(deleting && timing == 2) { finishClock(); setTimeout(() => { setTiming(0); initClock(); }, 1000); } }, [timing]); return ( {props.name} {!!props.datePublication && `Publicado há ${diffDateWithNow(props.datePublication)}`} { props.name == props.currentUser.name && ( { deleting ? : } ) } ); }); const styles = StyleSheet.create({ barUsername: { flexDirection: "row", alignItems: "center", }, image: { width: 50, height: 50, borderRadius: 25, marginLeft: 8, }, verifyIcon: { width: 20, height: 20, marginLeft: 20, }, userView: { flexDirection: "row", position: "absolute", top: 55, width: "98%", alignItems: "center", }, name: { fontSize: 18, fontWeight: "500", marginLeft: 12, color: "white", }, time: { fontSize: 12, fontWeight: "400", marginTop: 3, marginLeft: 12, color: "white", }, });