1
- import React from 'react' ;
1
+ import React , { useRef } from 'react' ;
2
2
import styled , { css } from 'styled-components' ;
3
3
import RatioImage from './RatioImage' ;
4
4
import { ellipsis } from '../../lib/styles/utils' ;
@@ -12,6 +12,7 @@ import SkeletonTexts from './SkeletonTexts';
12
12
import Skeleton from './Skeleton' ;
13
13
import { mediaQuery } from '../../lib/styles/media' ;
14
14
import { Link } from 'react-router-dom' ;
15
+ import usePrefetchPost from '../../lib/hooks/usePrefetchPost' ;
15
16
16
17
export type PostCardProps = {
17
18
post : PartialPost ;
@@ -20,8 +21,21 @@ export type PostCardProps = {
20
21
function PostCard ( { post } : PostCardProps ) {
21
22
const url = `/@${ post . user . username } /${ post . url_slug } ` ;
22
23
24
+ const prefetch = usePrefetchPost ( post . user . username , post . url_slug ) ;
25
+ const prefetchTimeoutId = useRef < number | null > ( null ) ;
26
+
27
+ const onMouseEnter = ( ) => {
28
+ prefetchTimeoutId . current = setTimeout ( prefetch , 2000 ) ;
29
+ } ;
30
+
31
+ const onMouseLeave = ( ) => {
32
+ if ( prefetchTimeoutId . current ) {
33
+ clearTimeout ( prefetchTimeoutId . current ) ;
34
+ }
35
+ } ;
36
+
23
37
return (
24
- < Block >
38
+ < Block onMouseEnter = { onMouseEnter } onMouseLeave = { onMouseLeave } >
25
39
{ post . thumbnail && (
26
40
< StyledLink to = { url } >
27
41
< RatioImage
0 commit comments