1- import { useState , useEffect } from 'react'
1+ import { useState , useEffect , useMemo } from 'react'
22import {
33 X ,
44 ChevronLeft ,
88 MoreHorizontal ,
99} from 'lucide-react'
1010import { useNavigate , Link } from '@tanstack/react-router'
11- import { useQueryClient } from '@tanstack/react-query'
11+ import { useQueryClient , useQuery } from '@tanstack/react-query'
1212import type { StoryFeedItem , Story } from '@/entities/story/model/types'
1313import { useStoryViewer } from '../model/useStoryViewer'
1414import { STORY_VIEWER_UI } from './constants'
@@ -17,6 +17,7 @@ import ReportModal from '@/components/post/ReportModal'
1717import AccountInfoModal from '@/components/post/AccountInfoModal'
1818import { instance } from '@/shared/api/ky'
1919import { useCurrentUser } from '@/shared/auth/useCurrentUser'
20+ import { getStoryDetail } from '@/entities/story/api/getStoryDetail'
2021import instagramLogo from '@/assets/instagram-black-logo.png'
2122
2223interface StoryViewerProps {
@@ -30,25 +31,36 @@ const formatRelativeTime = (createdAt: string) => {
3031 const diffInMinutes = Math . floor (
3132 ( now . getTime ( ) - created . getTime ( ) ) / ( 1000 * 60 )
3233 )
33-
3434 if ( diffInMinutes < 1 ) return '방금 전'
3535 if ( diffInMinutes < 60 ) return `${ diffInMinutes } 분 전`
36-
3736 const diffInHours = Math . floor ( diffInMinutes / 60 )
3837 return `${ diffInHours } 시간 전`
3938}
4039
4140export function StoryViewer ( { feed, userId } : StoryViewerProps ) {
4241 const navigate = useNavigate ( )
4342 const queryClient = useQueryClient ( )
44- const { data : me } = useCurrentUser ( )
43+ const { data : me , isLoading : isMeLoading } = useCurrentUser ( )
4544
4645 const [ imageError , setImageError ] = useState ( false )
4746 const [ isOptionsOpen , setIsOptionsOpen ] = useState ( false )
4847 const [ isReportOpen , setIsReportOpen ] = useState ( false )
4948 const [ isAccountInfoOpen , setIsAccountInfoOpen ] = useState ( false )
5049 const [ isDeleteConfirmOpen , setIsDeleteConfirmOpen ] = useState ( false )
5150
51+ const { data : detailData , isLoading : isDetailLoading } = useQuery ( {
52+ queryKey : [ 'stories' , 'user' , userId ] ,
53+ queryFn : ( ) => getStoryDetail ( userId ) ,
54+ enabled : ! ! userId ,
55+ } )
56+
57+ const enrichedFeed = useMemo ( ( ) => {
58+ if ( ! detailData ) return feed
59+ return feed . map ( ( item ) =>
60+ String ( item . userId ) === String ( userId ) ? detailData : item
61+ )
62+ } , [ feed , detailData , userId ] )
63+
5264 const {
5365 currentUser,
5466 currentStory,
@@ -59,9 +71,10 @@ export function StoryViewer({ feed, userId }: StoryViewerProps) {
5971 handleNext,
6072 handlePrev,
6173 togglePause,
62- } = useStoryViewer ( feed , userId )
74+ } = useStoryViewer ( enrichedFeed , userId )
6375
6476 const isMine =
77+ ! isMeLoading &&
6578 me ?. userId !== undefined &&
6679 currentUser ?. userId !== undefined &&
6780 String ( me . userId ) === String ( currentUser . userId )
@@ -72,7 +85,13 @@ export function StoryViewer({ feed, userId }: StoryViewerProps) {
7285 }
7386 } , [ imageError , isPaused , togglePause ] )
7487
75- if ( ! currentUser || ! currentStory ) return null
88+ if ( isDetailLoading || ! currentUser || ! currentStory ) {
89+ return (
90+ < div className = "fixed inset-0 z-[100] flex items-center justify-center bg-black" >
91+ < div className = "h-8 w-8 animate-spin rounded-full border-4 border-gray-600 border-t-white" />
92+ </ div >
93+ )
94+ }
7695
7796 const isFirstStoryOfFirstUser =
7897 currentUserIndex === 0 && currentStoryIndex === 0
@@ -111,9 +130,9 @@ export function StoryViewer({ feed, userId }: StoryViewerProps) {
111130 const response = await instance
112131 . delete ( `api/v1/stories/${ currentStory . id } ` )
113132 . json < { isSuccess : boolean ; code : string ; message : string } > ( )
114-
115133 if ( response . isSuccess ) {
116134 queryClient . invalidateQueries ( { queryKey : [ 'stories' , 'feed' ] } )
135+ queryClient . invalidateQueries ( { queryKey : [ 'stories' , 'user' , userId ] } )
117136 navigate ( { to : '/' , search : { page : 1 } } )
118137 }
119138 } catch ( error ) {
@@ -274,6 +293,7 @@ export function StoryViewer({ feed, userId }: StoryViewerProps) {
274293 < StoryOptionsModal
275294 isOpen = { isOptionsOpen }
276295 onClose = { handleCloseOptions }
296+ isMine = { isMine }
277297 userId = { currentUser . userId }
278298 onReport = { handleOpenReport }
279299 onAccountInfo = { handleOpenAccountInfo }
0 commit comments