Skip to content

Commit 2eae359

Browse files
committed
improve reload when new review submitted; see pathname
1 parent a2bb9aa commit 2eae359

1 file changed

Lines changed: 47 additions & 28 deletions

File tree

apps/conch/app/(app)/(home)/hooks.ts

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
import { useState, useEffect, useMemo, useCallback } from 'react'
1+
import { useState, useEffect, useMemo, useCallback, useRef } from 'react'
22
import { consts, getToday, inquiryMonth, list } from '@conch/utils'
33
import { useRefresh } from '@conch/hooks/useRefresh'
4-
import { useIsFocused, useNavigation } from '@react-navigation/native'
4+
import { usePathname } from 'expo-router'
55
import type { ReviewForCalendar, MonthlyReviews, MonthlyReviewKey, ReviewForList } from './types'
66

7-
// Safe wrapper for useIsFocused to handle cases outside NavigationContainer
8-
function useSafeIsFocused(): boolean {
9-
try {
10-
const navigation = useNavigation()
11-
const isFocused = useIsFocused()
12-
return isFocused
13-
} catch {
14-
return true // Default to true if navigation context is not available
15-
}
7+
// Custom hook to detect when user returns to home screen
8+
function useHomeReturn(callback: () => void) {
9+
const pathname = usePathname()
10+
const prevPathname = useRef(pathname)
11+
12+
useEffect(() => {
13+
const isReturningHome = prevPathname.current !== pathname && (pathname === '/' || pathname.includes('(home)'))
14+
15+
if (isReturningHome) {
16+
callback()
17+
}
18+
19+
prevPathname.current = pathname
20+
}, [pathname, callback])
1621
}
1722

1823
type CalendarCell = {
@@ -64,26 +69,24 @@ export function useCalendar({ reviews: _reviews, year, month }: { reviews: Revie
6469
export function useReviewDataAtMonth({ year, month }: { year: number; month: number }): { reviews: ReviewForCalendar[] } {
6570
const [reviews, setReviews] = useState<MonthlyReviews>({})
6671
const [date, setDate] = useState<number>(new Date().getDate())
67-
const isFocused = useSafeIsFocused()
6872

6973
const fetchAndSetReviewData = useCallback(
70-
async ({ year, month }: { year: number; month: number }) => {
74+
async ({ year: targetYear, month: targetMonth }: { year: number; month: number }) => {
7175
const { year: thisYear, month: thisMonth } = getToday()
72-
const isThisMonth = year === thisYear && month === thisMonth
73-
const isFuture = !(year < thisYear || (year === thisYear && month <= thisMonth))
76+
const isThisMonth = targetYear === thisYear && targetMonth === thisMonth
77+
const isFuture = !(targetYear < thisYear || (targetYear === thisYear && targetMonth <= thisMonth))
7478

75-
const yearAndMonth: MonthlyReviewKey = `${year}-${month + 1}`
79+
const yearAndMonth: MonthlyReviewKey = `${targetYear}-${targetMonth + 1}`
7680

7781
if (!isThisMonth && !!reviews[yearAndMonth]) return
7882
if (isFuture) return
7983

80-
const _reviewsData = await inquiryMonth({ year, month: month + 1 })
84+
const _reviewsData = await inquiryMonth({ year: targetYear, month: targetMonth + 1 })
8185
const reviewsData = _reviewsData?.map((review) => ({
82-
month: month + 1,
86+
month: targetMonth + 1,
8387
...review,
8488
})) || []
8589

86-
if (isThisMonth && reviewsData?.length === reviews[yearAndMonth]?.length) return
8790
setReviews((prev) => ({
8891
...prev,
8992
[yearAndMonth]: reviewsData,
@@ -93,8 +96,18 @@ export function useReviewDataAtMonth({ year, month }: { year: number; month: num
9396
)
9497

9598
useEffect(() => {
96-
if (isFocused) fetchAndSetReviewData({ year, month })
97-
}, [year, month, fetchAndSetReviewData, isFocused])
99+
fetchAndSetReviewData({ year, month })
100+
}, [year, month, fetchAndSetReviewData])
101+
102+
// (home)으로 돌아올 때 현재월 데이터 refetch
103+
useHomeReturn(useCallback(() => {
104+
const { year: thisYear, month: thisMonth } = getToday()
105+
const isThisMonth = year === thisYear && month === thisMonth
106+
107+
if (isThisMonth) {
108+
fetchAndSetReviewData({ year, month })
109+
}
110+
}, [year, month, fetchAndSetReviewData]))
98111

99112
const refreshWhenDateChanged = useCallback(() => {
100113
const { date: todayDate } = getToday()
@@ -106,12 +119,10 @@ export function useReviewDataAtMonth({ year, month }: { year: number; month: num
106119

107120
useRefresh(() => refreshWhenDateChanged())
108121

109-
const yearAndMonth = useMemo<MonthlyReviewKey>(() => `${year}-${month + 1}`, [year, month])
110-
111122
return { reviews: Object.values(reviews).flat() || [] }
112123
}
113124

114-
export function useTodayReviewWritten({ reviews, year, month, date }: { reviews: ReviewForCalendar[]; year: number; month: number; date: number }): boolean {
125+
export function useTodayReviewWritten({ reviews, date }: { reviews: ReviewForCalendar[]; year: number; month: number; date: number }): boolean {
115126
return reviews?.some((review) => review.day === date) || false
116127
}
117128

@@ -120,7 +131,6 @@ export function useReviewList() {
120131
const [nextCursor, setNextCursor] = useState<string | null>(null)
121132
const [isLoading, setIsLoading] = useState(false)
122133
const [isRefreshing, setIsRefreshing] = useState(false)
123-
const isFocused = useSafeIsFocused()
124134

125135
const fetchReviews = useCallback(async (cursor?: string) => {
126136
try {
@@ -143,7 +153,11 @@ export function useReviewList() {
143153

144154
setNextCursor(response?.hasNext ? response.nextCursor || null : null)
145155
} catch (error) {
146-
console.error('Failed to fetch reviews:', error)
156+
// Failed to fetch reviews
157+
if (__DEV__) {
158+
// eslint-disable-next-line no-console
159+
console.error('Failed to fetch reviews:', error)
160+
}
147161
} finally {
148162
setIsLoading(false)
149163
setIsRefreshing(false)
@@ -162,10 +176,15 @@ export function useReviewList() {
162176
}, [fetchReviews])
163177

164178
useEffect(() => {
165-
if (isFocused && reviews.length === 0) {
179+
if (reviews.length === 0) {
166180
fetchReviews()
167181
}
168-
}, [isFocused, fetchReviews, reviews.length])
182+
}, [fetchReviews, reviews.length])
183+
184+
// (home)으로 돌아올 때 리스트 데이터 refetch
185+
useHomeReturn(useCallback(() => {
186+
fetchReviews()
187+
}, [fetchReviews]))
169188

170189
useRefresh(() => refresh())
171190

0 commit comments

Comments
 (0)