Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | 3x 10x 10x 10x 10x 10x 10x 10x 10x 3x 1x 3x 3x 10x 2x 2x 10x 4x 4x 1x 1x 1x 3x 3x 10x 5x 1x 10x 1x 1x 10x 6x 6x 6x 6x 10x 10x | import { TouchEvent, useEffect, useRef, useState } from 'react'; import { toast } from 'react-toastify'; import { useRouter } from 'next/router'; import Link from 'next/link'; import { Image, FavoriteHandle } from 'components'; import { slugfy } from 'utils'; import { events } from 'app'; import { Entities, Entity, Partials } from 'types'; import * as S from './styles'; export type CardProps = Omit< Partials<Entity, 'entity'>, 'relations' | 'extras' >; const Card = (data: CardProps) => { const { entity = Entities.CHARACTERS, image, title } = data; const containerRef = useRef<HTMLDivElement>(null); const router = useRouter(); const [isTouchscreen, setIsTouchscreen] = useState(false); const timeoutIdRef = useRef<NodeJS.Timeout>(); const [isDragging, setIsDragging] = useState(false); const [isFocused, setIsFocused] = useState(false); const handleRedirect = () => { const id = setTimeout(() => { toast.loading('We are setting up the page for you, wait!'); }, 2000); timeoutIdRef.current = id; router.push(href); }; const onMouseUp = () => { Iif (isTouchscreen) return; handleRedirect(); }; const onTouch = () => { Iif (isDragging) { setIsDragging(false); setIsFocused(false); return; } if (isFocused) { events.off('touchend', onTouchEnd); handleRedirect(); return; } setIsFocused(true); events.on('touchend', onTouchEnd); }; const onTouchEnd = (e: TouchEvent) => { if (containerRef.current?.contains(e.target as Node)) return; handleRemoveFocused(); }; const handleRemoveFocused = () => { setIsFocused(false); events.off('touchend', onTouchEnd); }; useEffect(() => { setIsTouchscreen(!!window.matchMedia('(any-pointer: coarse)').matches); return () => { clearTimeout(timeoutIdRef.current as unknown as number); events.off('touchend', onTouchEnd); }; }, []); const href = `/universe/${entity}/${slugfy(title)}`; return ( <S.Container ref={containerRef} role="link" onMouseUp={onMouseUp} onTouchEnd={onTouch} onTouchMove={() => !isDragging && setIsDragging(true)} > <Link href={href} passHref> <S.Hide /> </Link> <Image image={image} alt={title} /> <S.Content> <S.Label>{title}</S.Label> <S.Wrapper> <FavoriteHandle {...data} entity={entity} /> </S.Wrapper> </S.Content> </S.Container> ); }; export { Card }; |