@@ -22,6 +22,7 @@ import {
2222 getAuditHistory ,
2323 renameAudit ,
2424 saveAuditData ,
25+ watchAuditData ,
2526 watchAuditHistory ,
2627} from "@/lib/storage/audit-storage" ;
2728import { createContext , useContext , useEffect , useMemo , useState } from "react" ;
@@ -128,6 +129,13 @@ export function AuditContextProvider({
128129 } ) ;
129130 } , [ ] ) ;
130131
132+ useEffect ( ( ) => {
133+ if ( ! currentAuditId ) return ;
134+
135+ setAuditData ( null ) ;
136+ return watchAuditData ( currentAuditId , setAuditData ) ;
137+ } , [ currentAuditId ] ) ;
138+
131139 useEffect ( ( ) => {
132140 let cancelled = false ;
133141 setLoaded ( false ) ;
@@ -166,11 +174,6 @@ export function AuditContextProvider({
166174 } , [ currentAuditId , updateLastAuditId ] ) ;
167175
168176 const value = useMemo < AuditContextValue > ( ( ) => {
169- const persist = async ( auditId : string , updated : CachedAuditData ) => {
170- await saveAuditData ( auditId , updated ) ;
171- setAuditData ( updated ) ;
172- } ;
173-
174177 // currentAuditId and history are guaranteed non-null past the loading
175178 // guard below, which is the only path that renders this provider's value.
176179 return {
@@ -204,7 +207,7 @@ export function AuditContextProvider({
204207 if ( ! auditData || ! currentAuditId ) return false ;
205208 const updated = moveCourseToSemester ( auditData , courseId , semester ) ;
206209 if ( ! updated ) return false ;
207- await persist ( currentAuditId , updated ) ;
210+ await saveAuditData ( currentAuditId , updated ) ;
208211 return true ;
209212 } ,
210213 addPlannedCourse : async ( course , requirementTitle , ruleTitle ) => {
@@ -216,7 +219,7 @@ export function AuditContextProvider({
216219 ruleTitle ,
217220 ) ;
218221 if ( ! result ) return null ;
219- await persist ( currentAuditId , result . audit ) ;
222+ await saveAuditData ( currentAuditId , result . audit ) ;
220223 return result . courseId ;
221224 } ,
222225 } ;
@@ -233,7 +236,9 @@ export function AuditContextProvider({
233236 updateLastAuditId ,
234237 ] ) ;
235238
236- if ( ! loaded || ! currentAuditId || ! history ) return < LoadingPage /> ;
239+ if ( ! loaded || ! currentAuditId || ! history || ! auditData ) {
240+ return < LoadingPage /> ;
241+ }
237242
238243 return (
239244 < AuditContext . Provider value = { value } > { children } </ AuditContext . Provider >
0 commit comments