-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refctor: SongDetailListPage 내 로직 custom hook 분리
1. 첫 entries fetch 담당 hook 2. 이후 extra Fetch 담당 hook
- Loading branch information
1 parent
da53a35
commit 1ef030a
Showing
3 changed files
with
104 additions
and
74 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 |
---|---|---|
@@ -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?.disconnect(); | ||
return; | ||
} | ||
|
||
prevObserverRef.current = createObserver(() => | ||
fetchExtraPrevSongDetails(getFirstSongId(dom), genreParams as Genre) | ||
); | ||
|
||
prevObserverRef.current.observe(dom); | ||
}, []); | ||
|
||
const getExtraNextSongDetailsOnObserve: React.RefCallback<HTMLDivElement> = useCallback((dom) => { | ||
if (dom === null) { | ||
nextObserverRef.current?.disconnect(); | ||
return; | ||
} | ||
|
||
nextObserverRef.current = createObserver(() => | ||
fetchExtraNextSongDetails(getLastSongId(dom), genreParams as Genre) | ||
); | ||
|
||
nextObserverRef.current.observe(dom); | ||
}, []); | ||
|
||
const getFirstSongId = (dom: HTMLDivElement) => { | ||
const firstSongId = dom.nextElementSibling?.getAttribute('data-song-id') as string; | ||
|
||
return Number(firstSongId); | ||
}; | ||
|
||
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,22 @@ | ||
import { useLayoutEffect, useRef } 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 currentSongDetailItemRef = useRef<HTMLDivElement>(null); | ||
|
||
const { data: songDetailEntries } = useFetch(() => | ||
getSongDetailEntries(Number(songIdParams), genreParams as Genre) | ||
); | ||
|
||
useLayoutEffect(() => { | ||
currentSongDetailItemRef.current?.scrollIntoView({ behavior: 'instant', block: 'start' }); | ||
}, [songDetailEntries]); | ||
|
||
return { songDetailEntries, currentSongDetailItemRef }; | ||
}; | ||
|
||
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