Skip to content

Commit 32419a6

Browse files
committed
Implement prefetch
1 parent 5a47cc6 commit 32419a6

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/components/common/PostCard.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useRef } from 'react';
22
import styled, { css } from 'styled-components';
33
import RatioImage from './RatioImage';
44
import { ellipsis } from '../../lib/styles/utils';
@@ -12,6 +12,7 @@ import SkeletonTexts from './SkeletonTexts';
1212
import Skeleton from './Skeleton';
1313
import { mediaQuery } from '../../lib/styles/media';
1414
import { Link } from 'react-router-dom';
15+
import usePrefetchPost from '../../lib/hooks/usePrefetchPost';
1516

1617
export type PostCardProps = {
1718
post: PartialPost;
@@ -20,8 +21,21 @@ export type PostCardProps = {
2021
function PostCard({ post }: PostCardProps) {
2122
const url = `/@${post.user.username}/${post.url_slug}`;
2223

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+
2337
return (
24-
<Block>
38+
<Block onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave}>
2539
{post.thumbnail && (
2640
<StyledLink to={url}>
2741
<RatioImage

0 commit comments

Comments
 (0)