@@ -17,12 +17,12 @@ import { Input } from "@/components/ui/input";
1717import { Label } from "@/components/ui/label" ;
1818import { HABIT_PET_DATA_UPDATED_EVENT } from "@/lib/app-events" ;
1919import {
20- getDailyTasks ,
21- type DailyTask ,
22- } from "@/lib/daily-tasks" ;
20+ getCachedHabitsTabData ,
21+ prefetchHabitsTabData ,
22+ } from "@/lib/app-tab-data-cache" ;
23+ import type { DailyTask } from "@/lib/daily-tasks" ;
2324import {
2425 addHabit ,
25- getCustomHabits ,
2626 getTodayDateKey ,
2727 isHabitCompletedToday ,
2828 removeHabit ,
@@ -31,8 +31,6 @@ import {
3131} from "@/lib/habits-storage" ;
3232import { formatCoinsDelta } from "@/lib/coins" ;
3333import { describeHabitWellnessBoost } from "@/lib/habit-wellness-effects" ;
34- import { getProfilePreferences } from "@/lib/profile-preferences-storage" ;
35- import { hasCompletedDailyQuizToday } from "@/lib/daily-quiz-storage" ;
3634
3735type HabitTrackerProps = {
3836 mode ?: "daily" | "manage" | "all" ;
@@ -184,13 +182,22 @@ function CustomHabitList({
184182}
185183
186184export function HabitTracker ( { mode = "daily" } : HabitTrackerProps ) {
187- const [ dailyTasks , setDailyTasks ] = useState < DailyTask [ ] > ( [ ] ) ;
188- const [ customHabits , setCustomHabits ] = useState < Habit [ ] > ( [ ] ) ;
185+ const cachedHabits = getCachedHabitsTabData ( ) ;
186+ const [ dailyTasks , setDailyTasks ] = useState < DailyTask [ ] > (
187+ cachedHabits ?. dailyTasks ?? [ ] ,
188+ ) ;
189+ const [ customHabits , setCustomHabits ] = useState < Habit [ ] > (
190+ cachedHabits ?. customHabits ?? [ ] ,
191+ ) ;
189192 const [ newHabitLabel , setNewHabitLabel ] = useState ( "" ) ;
190- const [ focusTopic , setFocusTopic ] = useState < string | null > ( null ) ;
191- const [ quizCompletedToday , setQuizCompletedToday ] = useState ( false ) ;
193+ const [ focusTopic , setFocusTopic ] = useState < string | null > (
194+ cachedHabits ?. focusTopic ?? null ,
195+ ) ;
196+ const [ quizCompletedToday , setQuizCompletedToday ] = useState (
197+ cachedHabits ?. quizCompletedToday ?? false ,
198+ ) ;
192199 const [ error , setError ] = useState < string | null > ( null ) ;
193- const [ isReady , setIsReady ] = useState ( false ) ;
200+ const [ isReady , setIsReady ] = useState ( cachedHabits !== null ) ;
194201 const [ isSaving , setIsSaving ] = useState ( false ) ;
195202 const [ optimisticCompletion , setOptimisticCompletion ] = useState <
196203 Record < string , boolean >
@@ -223,17 +230,21 @@ export function HabitTracker({ mode = "daily" }: HabitTrackerProps) {
223230 [ toggleErrors ] ,
224231 ) ;
225232
233+ const applyHabitsData = useCallback (
234+ ( data : NonNullable < ReturnType < typeof getCachedHabitsTabData > > ) => {
235+ setDailyTasks ( data . dailyTasks ) ;
236+ setCustomHabits ( data . customHabits ) ;
237+ setFocusTopic ( data . focusTopic ) ;
238+ setQuizCompletedToday ( data . quizCompletedToday ) ;
239+ } ,
240+ [ ] ,
241+ ) ;
242+
226243 const refresh = useCallback ( async ( ) => {
227244 try {
228245 setError ( null ) ;
229- const [ tasks , customs ] = await Promise . all ( [
230- getDailyTasks ( ) ,
231- getCustomHabits ( ) ,
232- ] ) ;
233- setDailyTasks ( tasks ) ;
234- setCustomHabits ( customs ) ;
235- setFocusTopic ( ( await getProfilePreferences ( ) ) . focusTopic ) ;
236- setQuizCompletedToday ( await hasCompletedDailyQuizToday ( ) ) ;
246+ const data = await prefetchHabitsTabData ( { force : true } ) ;
247+ applyHabitsData ( data ) ;
237248 setIsReady ( true ) ;
238249 } catch ( refreshError ) {
239250 setError (
@@ -243,7 +254,27 @@ export function HabitTracker({ mode = "daily" }: HabitTrackerProps) {
243254 ) ;
244255 setIsReady ( true ) ;
245256 }
246- } , [ ] ) ;
257+ } , [ applyHabitsData ] ) ;
258+
259+ useEffect ( ( ) => {
260+ void prefetchHabitsTabData ( )
261+ . then ( applyHabitsData )
262+ . catch ( ( refreshError ) => {
263+ setError (
264+ refreshError instanceof Error
265+ ? refreshError . message
266+ : "Could not load habits." ,
267+ ) ;
268+ } )
269+ . finally ( ( ) => {
270+ setIsReady ( true ) ;
271+ } ) ;
272+
273+ window . addEventListener ( HABIT_PET_DATA_UPDATED_EVENT , refresh ) ;
274+ return ( ) => {
275+ window . removeEventListener ( HABIT_PET_DATA_UPDATED_EVENT , refresh ) ;
276+ } ;
277+ } , [ applyHabitsData , refresh ] ) ;
247278
248279 useEffect ( ( ) => {
249280 if ( ! coinMessage ) {
@@ -290,15 +321,6 @@ export function HabitTracker({ mode = "daily" }: HabitTrackerProps) {
290321 } ;
291322 } , [ refresh , todayKey ] ) ;
292323
293- useEffect ( ( ) => {
294- void refresh ( ) ;
295-
296- window . addEventListener ( HABIT_PET_DATA_UPDATED_EVENT , refresh ) ;
297- return ( ) => {
298- window . removeEventListener ( HABIT_PET_DATA_UPDATED_EVENT , refresh ) ;
299- } ;
300- } , [ refresh ] ) ;
301-
302324 const handleToggle = ( habitId : string ) => {
303325 const habit = dailyTasks . find ( ( task ) => task . id === habitId ) ;
304326
0 commit comments