import { useCallback , useEffect , useState } from "react" ; import { getComments , PostType , QnaComment } from "../../../lib/api/qna" ; export default function useComments ( type , postId ) { const [ loading , setLoading ] = useState ( false ) ; const [ data , setData ] = useState < QnaComment [ ] | null > ( null ) ; const load = useCallback ( async ( ) => { if ( loading ) { return ; } setLoading ( true ) ; const comments = await getComments ( type , postId ) ; setData ( comments ) ; setLoading ( false ) ; } , [ loading , setLoading , setData , postId , type ] ) ; useEffect ( ( ) => { load ( ) ; } , [ ] ) ; const refetch = useCallback ( async ( ) => { await load ( ) ; } , [ load ] ) ; return { data , loading , refetch } ; }