-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor/#551 SongDetailListPage의 스와이프 추가 fetch 옵져버 로직에 callbackRef를 적용하여 로직 흐름을 개선한다. #552
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7e9ecaa
refactor: effect 삭제 및 callbackRef로 변경
Creative-Lee da53a35
refactor: msw 로직 수정
Creative-Lee 1ef030a
refctor: SongDetailListPage 내 로직 custom hook 분리
Creative-Lee 4bf1262
refactor: refCallback setup, clean up 코드 순서 변경
Creative-Lee c7f3337
refactor: useLayoutEffect로 실행하던 스크롤 이동 로직 refCallback으로 변경
Creative-Lee 25b509d
refactor: 코드 순서 수정
Creative-Lee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { useCallback, useRef } from 'react'; | ||
import useExtraFetch from '@/shared/hooks/useExtraFetch'; | ||
import useValidParams from '@/shared/hooks/useValidParams'; | ||
import createObserver from '@/shared/utils/createObserver'; | ||
import { getExtraNextSongDetails, getExtraPrevSongDetails } from '../remotes/songs'; | ||
import type { Genre } from '../types/Song.type'; | ||
|
||
const useExtraSongDetail = () => { | ||
const { genre: genreParams } = useValidParams(); | ||
|
||
const { data: extraPrevSongDetails, fetchData: fetchExtraPrevSongDetails } = useExtraFetch( | ||
getExtraPrevSongDetails, | ||
'prev' | ||
); | ||
|
||
const { data: extraNextSongDetails, fetchData: fetchExtraNextSongDetails } = useExtraFetch( | ||
getExtraNextSongDetails, | ||
'next' | ||
); | ||
|
||
const prevObserverRef = useRef<IntersectionObserver | null>(null); | ||
const nextObserverRef = useRef<IntersectionObserver | null>(null); | ||
|
||
const getExtraPrevSongDetailsOnObserve: React.RefCallback<HTMLDivElement> = useCallback((dom) => { | ||
if (dom !== null) { | ||
prevObserverRef.current = createObserver(() => | ||
fetchExtraPrevSongDetails(getFirstSongId(dom), genreParams as Genre) | ||
); | ||
|
||
prevObserverRef.current.observe(dom); | ||
return; | ||
} | ||
|
||
prevObserverRef.current?.disconnect(); | ||
}, []); | ||
|
||
const getExtraNextSongDetailsOnObserve: React.RefCallback<HTMLDivElement> = useCallback((dom) => { | ||
if (dom !== null) { | ||
nextObserverRef.current = createObserver(() => | ||
fetchExtraNextSongDetails(getLastSongId(dom), genreParams as Genre) | ||
); | ||
|
||
nextObserverRef.current.observe(dom); | ||
return; | ||
} | ||
|
||
nextObserverRef.current?.disconnect(); | ||
}, []); | ||
|
||
const getFirstSongId = (dom: HTMLDivElement) => { | ||
const firstSongId = dom.nextElementSibling?.getAttribute('data-song-id') as string; | ||
|
||
return Number(firstSongId); | ||
}; | ||
Comment on lines
+50
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💬 저희 data-attribute로 처리하고 있는 것들 개선하는 것이 좋을 것 같아요. |
||
|
||
const getLastSongId = (dom: HTMLDivElement) => { | ||
const lastSongId = dom.previousElementSibling?.getAttribute('data-song-id') as string; | ||
|
||
return Number(lastSongId); | ||
}; | ||
|
||
return { | ||
extraPrevSongDetails, | ||
extraNextSongDetails, | ||
getExtraPrevSongDetailsOnObserve, | ||
getExtraNextSongDetailsOnObserve, | ||
}; | ||
}; | ||
|
||
export default useExtraSongDetail; |
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,21 @@ | ||
import { useCallback } from 'react'; | ||
import useFetch from '@/shared/hooks/useFetch'; | ||
import useValidParams from '@/shared/hooks/useValidParams'; | ||
import { getSongDetailEntries } from '../remotes/songs'; | ||
import type { Genre } from '../types/Song.type'; | ||
|
||
const useSongDetailEntries = () => { | ||
const { id: songIdParams, genre: genreParams } = useValidParams(); | ||
|
||
const { data: songDetailEntries } = useFetch(() => | ||
getSongDetailEntries(Number(songIdParams), genreParams as Genre) | ||
); | ||
|
||
const scrollIntoCurrentSong: React.RefCallback<HTMLDivElement> = useCallback((dom) => { | ||
if (dom !== null) dom.scrollIntoView({ behavior: 'instant', block: 'start' }); | ||
}, []); | ||
|
||
return { songDetailEntries, scrollIntoCurrentSong }; | ||
}; | ||
|
||
export default useSongDetailEntries; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useCallback 으로 참조 동일성 지켜주신 부분 👍 👍 👍 덕분에 많이 배웠어요.