-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 조회수 카운터 추가 * feat: 조회수 로직 변경 * refactor: 불필요 컴포넌트 제거 * feat: 조회수 추가 * feat: 클린업 추가 * feat: 조회수 요청 보장
- Loading branch information
Showing
6 changed files
with
160 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,14 @@ | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { z } from 'zod'; | ||
|
||
import { createEndpoint } from '@/api/typedAxios'; | ||
|
||
export const postPostHit = createEndpoint({ | ||
request: (postId: string) => ({ | ||
request: (postIdList: string[]) => ({ | ||
method: 'POST', | ||
url: `api/v1/community/posts/${postId}/hit`, | ||
url: `api/v1/community/posts/hit`, | ||
data: { | ||
postIdList: postIdList.map((id) => Number(id)).filter((value) => !Number.isNaN(value)), | ||
}, | ||
}), | ||
serverResponseScheme: z.unknown(), | ||
}); | ||
|
||
export const usePostPostHitMutation = () => { | ||
return useMutation({ | ||
mutationFn: (postId: string) => postPostHit.request(postId), | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { useEffect, useRef } from 'react'; | ||
|
||
import { postPostHit } from '@/api/endpoint/feed/postPostHit'; | ||
|
||
export const useHit = () => { | ||
const { mutate } = useMutation({ | ||
mutationFn: (ids: string[]) => postPostHit.request(ids), | ||
}); | ||
|
||
const viewedSet = useRef(new Set<string>()); | ||
const timeoutTokenRef = useRef<null | ReturnType<typeof setTimeout>>(null); | ||
|
||
const handleFeedImpression = (feedId: string) => { | ||
viewedSet.current.add(feedId); | ||
scheduleProcess(); | ||
}; | ||
|
||
const scheduleProcess = () => { | ||
if (timeoutTokenRef.current != null) { | ||
return; | ||
} | ||
|
||
// 페이지를 벗어나도 조회수 요청이 나가도록 일부러 cleanup 때 정리 안함 | ||
timeoutTokenRef.current = setTimeout(() => { | ||
const ids = [...viewedSet.current.values()]; | ||
if (ids.length > 0) { | ||
mutate(ids); | ||
} | ||
|
||
viewedSet.current.clear(); | ||
timeoutTokenRef.current = null; | ||
}, 5000); | ||
}; | ||
|
||
return { | ||
queueHit: handleFeedImpression, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters