File size: 10,716 Bytes
f871fed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
 * Study Plan React Query hooks.
 */

import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import * as studyPlansApi from '../api/study-plans';
import type {
  StudyPlanCreate,
  StudyPlanUpdate,
  StudyTopicCreate,
  StudyTopicUpdate,
  StudySessionCreate,
  StudySessionUpdate,
  PlanGenerationRequest,
} from '../types/study-plan';

// ============ Query Keys ============

export const studyPlanKeys = {
  all: ['study-plans'] as const,
  lists: () => [...studyPlanKeys.all, 'list'] as const,
  list: (notebookId?: string) => [...studyPlanKeys.lists(), { notebookId }] as const,
  active: () => [...studyPlanKeys.all, 'active'] as const,
  details: () => [...studyPlanKeys.all, 'detail'] as const,
  detail: (planId: string) => [...studyPlanKeys.details(), planId] as const,
  stats: (planId: string) => [...studyPlanKeys.all, 'stats', planId] as const,
  schedule: (planId: string, weekStart?: string) => [...studyPlanKeys.all, 'schedule', planId, weekStart] as const,
  topics: (planId: string) => [...studyPlanKeys.all, 'topics', planId] as const,
  sessions: (planId: string) => [...studyPlanKeys.all, 'sessions', planId] as const,
  todaySessions: (planId?: string) => [...studyPlanKeys.all, 'today', planId] as const,
  adjustments: (planId: string) => [...studyPlanKeys.all, 'adjustments', planId] as const,
};

// ============ Plan Hooks ============

export function useStudyPlans(notebookId?: string) {
  return useQuery({
    queryKey: studyPlanKeys.list(notebookId),
    queryFn: () => studyPlansApi.getStudyPlans(notebookId),
  });
}

export function useActivePlans() {
  return useQuery({
    queryKey: studyPlanKeys.active(),
    queryFn: () => studyPlansApi.getStudyPlans(undefined, true),
  });
}

export function useStudyPlan(planId: string) {
  return useQuery({
    queryKey: studyPlanKeys.detail(planId),
    queryFn: () => studyPlansApi.getStudyPlan(planId),
    enabled: !!planId,
  });
}

export function useStudyPlanStats(planId: string) {
  return useQuery({
    queryKey: studyPlanKeys.stats(planId),
    queryFn: () => studyPlansApi.getStudyPlanStats(planId),
    enabled: !!planId,
  });
}

export function useWeeklySchedule(planId: string, weekStart?: string) {
  return useQuery({
    queryKey: studyPlanKeys.schedule(planId, weekStart),
    queryFn: () => studyPlansApi.getWeeklySchedule(planId, weekStart),
    enabled: !!planId,
  });
}

export function useTodaySessions(planId?: string) {
  return useQuery({
    queryKey: studyPlanKeys.todaySessions(planId),
    queryFn: () => studyPlansApi.getTodaySessions(planId),
  });
}

export function useCreateStudyPlan() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (data: StudyPlanCreate) => studyPlansApi.createStudyPlan(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.all });
    },
  });
}

export function useGenerateStudyPlan() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (request: PlanGenerationRequest) => studyPlansApi.generateStudyPlan(request),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.all });
    },
  });
}

export function useUpdateStudyPlan() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ planId, data }: { planId: string; data: StudyPlanUpdate }) =>
      studyPlansApi.updateStudyPlan(planId, data),
    onSuccess: (_, { planId }) => {
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.detail(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.lists() });
    },
  });
}

export function useDeleteStudyPlan() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (planId: string) => studyPlansApi.deleteStudyPlan(planId),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.all });
    },
  });
}

// ============ Topic Hooks ============

export function useTopics(planId: string) {
  return useQuery({
    queryKey: studyPlanKeys.topics(planId),
    queryFn: () => studyPlansApi.getTopics(planId),
    enabled: !!planId,
  });
}

export function useCreateTopic() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ planId, data }: { planId: string; data: StudyTopicCreate }) =>
      studyPlansApi.createTopic(planId, data),
    onSuccess: (_, { planId }) => {
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.topics(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.detail(planId) });
    },
  });
}

export function useUpdateTopic() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ topicId, data, planId }: { topicId: string; data: StudyTopicUpdate; planId: string }) =>
      studyPlansApi.updateTopic(topicId, data),
    onSuccess: (_, { planId }) => {
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.topics(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.detail(planId) });
    },
  });
}

export function useDeleteTopic() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ topicId, planId }: { topicId: string; planId: string }) =>
      studyPlansApi.deleteTopic(topicId),
    onSuccess: (_, { planId }) => {
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.topics(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.detail(planId) });
    },
  });
}

// ============ Session Hooks ============

export function useSessions(planId: string) {
  return useQuery({
    queryKey: studyPlanKeys.sessions(planId),
    queryFn: () => studyPlansApi.getSessions(planId),
    enabled: !!planId,
  });
}

export function useCreateSession() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ planId, data }: { planId: string; data: StudySessionCreate }) =>
      studyPlansApi.createSession(planId, data),
    onSuccess: (_, { planId }) => {
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.sessions(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.detail(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.todaySessions() });
    },
  });
}

export function useUpdateSession() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ sessionId, data, planId }: { sessionId: string; data: StudySessionUpdate; planId: string }) =>
      studyPlansApi.updateSession(sessionId, data),
    onSuccess: (_, { planId }) => {
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.sessions(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.detail(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.todaySessions() });
    },
  });
}

export function useStartSession() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ sessionId, planId }: { sessionId: string; planId: string }) =>
      studyPlansApi.startSession(sessionId),
    onSuccess: (_, { planId }) => {
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.sessions(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.detail(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.todaySessions() });
    },
  });
}

export function useCompleteSession() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: async ({ sessionId, planId, rating, notes }: {
      sessionId: string;
      planId: string;
      rating?: number;
      notes?: string
    }) => {
      console.log('[useCompleteSession] Calling API with:', { sessionId, planId, rating, notes });
      const result = await studyPlansApi.completeSession(sessionId, rating, notes);
      console.log('[useCompleteSession] API returned:', result);
      return result;
    },
    onSuccess: (data, { planId }) => {
      console.log('[useCompleteSession] onSuccess - invalidating queries for planId:', planId);
      console.log('[useCompleteSession] Session data after completion:', data);
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.sessions(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.detail(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.stats(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.todaySessions() });
      // Also invalidate the active plans list so progress % updates in the tab buttons
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.active() });
      console.log('[useCompleteSession] All queries invalidated');
    },
    onError: (error) => {
      console.error('[useCompleteSession] Error:', error);
    },
  });
}

export function useDeleteSession() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ sessionId, planId }: { sessionId: string; planId: string }) =>
      studyPlansApi.deleteSession(sessionId),
    onSuccess: (_, { planId }) => {
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.sessions(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.detail(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.todaySessions() });
    },
  });
}

export function useSkipSession() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ sessionId, planId, reason }: { sessionId: string; planId: string; reason?: string }) =>
      studyPlansApi.skipSession(sessionId, reason),
    onSuccess: (_, { planId }) => {
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.sessions(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.detail(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.stats(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.todaySessions() });
    },
  });
}

// ============ Adjustment Hooks ============

export function useAdjustments(planId: string) {
  return useQuery({
    queryKey: studyPlanKeys.adjustments(planId),
    queryFn: () => studyPlansApi.getAdjustments(planId),
    enabled: !!planId,
  });
}

export function useRespondToAdjustment() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ adjustmentId, accepted, planId }: { adjustmentId: string; accepted: boolean; planId: string }) =>
      studyPlansApi.respondToAdjustment(adjustmentId, accepted),
    onSuccess: (_, { planId }) => {
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.adjustments(planId) });
      queryClient.invalidateQueries({ queryKey: studyPlanKeys.detail(planId) });
    },
  });
}