@@ -26,6 +26,8 @@ import { parseTrialOrder } from '../../utils/parseTrialOrder';
2626import { useUpdateProvenance } from './useUpdateProvenance' ;
2727import { useReplayContext } from '../../store/hooks/useReplay' ;
2828import { syncChannel , syncEmitter } from '../../utils/syncReplay' ;
29+ import type { StoredProvenance } from '../../store/types' ;
30+ import { getLegacyStoredAnswerProvenance } from '../../store/provenance' ;
2931
3032const margin = {
3133 left : 20 , top : 0 , right : 20 , bottom : 0 ,
@@ -62,6 +64,12 @@ export function AudioProvenanceVis({
6264 } = useReplayContext ( ) ;
6365
6466 const { storageEngine } = useStorageEngine ( ) ;
67+ const legacyProvenanceGraph = useMemo (
68+ ( ) => getLegacyStoredAnswerProvenance ( answers [ taskName ] ) ,
69+ [ answers , taskName ] ,
70+ ) ;
71+ const [ storedProvenanceGraph , setStoredProvenanceGraph ] = useState < StoredProvenance | null > ( legacyProvenanceGraph ) ;
72+ const provenanceGraph = storedProvenanceGraph ?? legacyProvenanceGraph ;
6573
6674 const [ analysisHasAudio , _setAnalysisHasAudio ] = useState ( true ) ;
6775
@@ -97,8 +105,36 @@ export function AudioProvenanceVis({
97105
98106 const trrackForTrial = useRef < Trrack < object , string > | null > ( null ) ;
99107
108+ useEffect ( ( ) => {
109+ let canceled = false ;
110+
111+ async function fetchProvenance ( ) {
112+ if ( ! taskName || ! participantId || ! storageEngine ) {
113+ setStoredProvenanceGraph ( legacyProvenanceGraph ) ;
114+ return ;
115+ }
116+
117+ try {
118+ const storedProvenance = await storageEngine . getProvenance ( taskName , participantId ) ;
119+ if ( ! canceled ) {
120+ setStoredProvenanceGraph ( storedProvenance ?? legacyProvenanceGraph ) ;
121+ }
122+ } catch {
123+ if ( ! canceled ) {
124+ setStoredProvenanceGraph ( legacyProvenanceGraph ) ;
125+ }
126+ }
127+ }
128+
129+ fetchProvenance ( ) ;
130+
131+ return ( ) => {
132+ canceled = true ;
133+ } ;
134+ } , [ legacyProvenanceGraph , participantId , storageEngine , taskName ] ) ;
135+
100136 const _setCurrentResponseNodes = useEvent ( ( node : string | null , location : ResponseBlockLocation ) => {
101- const graph = answers [ taskName ] ?. provenanceGraph [ location ] ;
137+ const graph = provenanceGraph ?. [ location ] ;
102138 if ( graph && node ) {
103139 if ( ! currentGlobalNode || graph . nodes [ node ] . createdOn > currentGlobalNode . time || playTime < currentGlobalNode . time ) {
104140 setCurrentGlobalNode ( { name : node || '' , time : graph . nodes [ node ] . createdOn } ) ;
@@ -176,48 +212,50 @@ export function AudioProvenanceVis({
176212 } ;
177213 } , [ answers , context , navigate , participantId , routerLocation . search , setSearchParams , studyId ] ) ;
178214
179- useUpdateProvenance ( 'aboveStimulus' , playTime , answers [ taskName ] ?. provenanceGraph . aboveStimulus , currentResponseNodes . aboveStimulus , _setCurrentResponseNodes , saveProvenance ) ;
215+ useUpdateProvenance ( 'aboveStimulus' , playTime , provenanceGraph ? .aboveStimulus , currentResponseNodes . aboveStimulus , _setCurrentResponseNodes , saveProvenance ) ;
180216
181- useUpdateProvenance ( 'belowStimulus' , playTime , answers [ taskName ] ?. provenanceGraph . belowStimulus , currentResponseNodes . belowStimulus , _setCurrentResponseNodes , saveProvenance ) ;
217+ useUpdateProvenance ( 'belowStimulus' , playTime , provenanceGraph ? .belowStimulus , currentResponseNodes . belowStimulus , _setCurrentResponseNodes , saveProvenance ) ;
182218
183- useUpdateProvenance ( 'sidebar' , playTime , answers [ taskName ] ?. provenanceGraph . sidebar , currentResponseNodes . sidebar , _setCurrentResponseNodes , saveProvenance ) ;
219+ useUpdateProvenance ( 'sidebar' , playTime , provenanceGraph ? .sidebar , currentResponseNodes . sidebar , _setCurrentResponseNodes , saveProvenance ) ;
184220
185221 // Create an instance of trrack to ensure getState works, incase the saved state is not a full state node.
186222 useEffect ( ( ) => {
187- if ( taskName && answers [ taskName ] ?. provenanceGraph ) {
223+ trrackForTrial . current = null ;
224+
225+ if ( taskName && provenanceGraph ) {
188226 const reg = Registry . create ( ) ;
189227
190228 const trrack = initializeTrrack ( { registry : reg , initialState : { } } ) ;
191229
192- if ( answers [ taskName ] ?. provenanceGraph . stimulus ) {
193- trrack . importObject ( structuredClone ( answers [ taskName ] ?. provenanceGraph ! . stimulus ) ) ;
230+ if ( provenanceGraph . stimulus ) {
231+ trrack . importObject ( structuredClone ( provenanceGraph . stimulus ) ) ;
194232
195233 trrackForTrial . current = trrack ;
196234 }
197235 }
198- } , [ answers , taskName ] ) ;
236+ } , [ provenanceGraph , taskName ] ) ;
199237
200238 const _setCurrentNode = useCallback ( ( node : string | undefined ) => {
201239 if ( ! node ) {
202240 return ;
203241 }
204242
205243 if ( taskName && trrackForTrial . current && context === 'provenanceVis' && saveProvenance ) {
206- saveProvenance ( { prov : trrackForTrial . current . getState ( answers [ taskName ] ?. provenanceGraph . stimulus ?. nodes [ node ] ) , location : 'stimulus' } ) ;
244+ saveProvenance ( { prov : trrackForTrial . current . getState ( provenanceGraph ? .stimulus ?. nodes [ node ] ) , location : 'stimulus' } ) ;
207245
208246 trrackForTrial . current . to ( node ) ;
209247 }
210248
211249 _setCurrentResponseNodes ( node , 'stimulus' ) ;
212250 setCurrentNode ( node ) ;
213- } , [ taskName , context , _setCurrentResponseNodes , saveProvenance , answers ] ) ;
251+ } , [ taskName , context , _setCurrentResponseNodes , saveProvenance , provenanceGraph ] ) ;
214252
215253 // use effect to control the current provenance node based on the changing playtime.
216254 useEffect ( ( ) => {
217- if ( ! taskName || ! trrackForTrial . current || ! answers [ taskName ] ?. provenanceGraph ) {
255+ if ( ! taskName || ! trrackForTrial . current || ! provenanceGraph ) {
218256 return ;
219257 }
220- const provGraph = answers [ taskName ] ?. provenanceGraph ;
258+ const provGraph = provenanceGraph ;
221259
222260 if ( ! provGraph . stimulus ) {
223261 return ;
@@ -249,7 +287,7 @@ export function AudioProvenanceVis({
249287 if ( tempNode . id !== currentNode ) {
250288 _setCurrentNode ( tempNode . id ) ;
251289 }
252- } , [ _setCurrentNode , currentNode , participantId , playTime , taskName , answers ] ) ;
290+ } , [ _setCurrentNode , currentNode , participantId , playTime , taskName , provenanceGraph ] ) ;
253291
254292 useEffect ( ( ) => {
255293 if ( duration === 0 ) {
@@ -353,13 +391,13 @@ export function AudioProvenanceVis({
353391 </ Box >
354392 ) : null }
355393
356- { xScale && taskName && answers [ taskName ] ?. provenanceGraph
394+ { xScale && taskName && provenanceGraph
357395 ? (
358396 < TaskProvenanceTimeline
359397 xScale = { xScale }
360398 trialName = { taskName }
361399 currentNode = { currentGlobalNode ?. name || '' }
362- answers = { answers }
400+ provenanceGraph = { provenanceGraph }
363401 width = { waveSurferWidth || ( width - margin . left - margin . right ) }
364402 height = { 25 }
365403 margin = { margin }
0 commit comments