-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathComponentController.tsx
More file actions
342 lines (289 loc) · 14.2 KB
/
Copy pathComponentController.tsx
File metadata and controls
342 lines (289 loc) · 14.2 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import {
Suspense, useEffect, useMemo, useRef, useState,
} from 'react';
import { useNavigate, useParams, useSearchParams } from 'react-router';
import {
Box, Center, Loader, Text, Title,
} from '@mantine/core';
import { IconPlugConnectedX } from '@tabler/icons-react';
import { ResponseBlock } from '../components/response/ResponseBlock';
import { IframeController } from './IframeController';
import { ImageController } from './ImageController';
import { ReactComponentController } from './ReactComponentController';
import { MarkdownController } from './MarkdownController';
import { useStudyConfig } from '../store/hooks/useStudyConfig';
import {
useCurrentComponent, useCurrentIdentifier, useCurrentStep, useStudyId,
} from '../routes/utils';
import { useStoredAnswer } from '../store/hooks/useStoredAnswer';
import { ReactMarkdownWrapper } from '../components/ReactMarkdownWrapper';
import { IndividualComponent } from '../parser/types';
import { useDisableBrowserBack } from '../utils/useDisableBrowserBack';
import { useStorageEngine } from '../storage/storageEngineHooks';
import {
useStoreActions, useStoreDispatch, useStoreSelector,
} from '../store/store';
import { StudyEnd } from '../components/StudyEnd';
import { TrainingFailed } from '../components/TrainingFailed';
import { ResourceNotFound } from '../ResourceNotFound';
import { TimedOut } from '../components/TimedOut';
import { findBlockForStep } from '../utils/getSequenceFlatMap';
import { VegaController, VegaProvState } from './VegaController';
import { useIsAnalysis } from '../store/hooks/useIsAnalysis';
import { VideoController } from './VideoController';
import { studyComponentToIndividualComponent } from '../utils/handleComponentInheritance';
import { useFetchStylesheet } from '../utils/fetchStylesheet';
import { ScreenRecordingReplay } from '../components/screenRecording/ScreenRecordingReplay';
import { useScreenRecordingContext } from '../store/hooks/useScreenRecording';
import { decryptIndex, encryptIndex } from '../utils/encryptDecryptIndex';
import { useRecordingConfig } from '../store/hooks/useRecordingConfig';
// current active stimuli presented to the user
export function ComponentController() {
// Get the config for the current step
const studyConfig = useStudyConfig();
const currentStep = useCurrentStep();
const currentComponent = useCurrentComponent();
const studyId = useStudyId();
const stepConfig = studyConfig.components[currentComponent];
const { storageEngine } = useStorageEngine();
const answers = useStoreSelector((store) => store.answers);
const audioStream = useRef<MediaRecorder | null>(null);
const analysisCanPlayScreenRecording = useStoreSelector((state) => state.analysisCanPlayScreenRecording);
const { setIsRecording, setAnalysisCanPlayScreenRecording } = useStoreActions();
const analysisProvState = useStoreSelector((state) => state.analysisProvState.stimulus);
const screenCaptureTrialName = useRef<string | null>(null);
const identifier = useCurrentIdentifier();
const navigate = useNavigate();
const {
studyHasScreenRecording, studyHasAudioRecording, currentComponentHasAudioRecording, currentComponentHasScreenRecording,
} = useRecordingConfig();
const {
isMediaCapturing, stopScreenCapture, startScreenRecording, stopScreenRecording, combinedMediaRecorder: screenRecordingStream,
} = useScreenRecordingContext();
const isAnalysis = useIsAnalysis();
// If we have a trial, use that config to render the right component else use the step
const status = useStoredAnswer();
const sequence = useStoreSelector((state) => state.sequence);
const modes = useStoreSelector((state) => state.modes);
const [searchParams] = useSearchParams();
const storePartId = useStoreSelector((state) => state.participantId);
const participantId = useMemo(() => searchParams.get('participantId'), [searchParams]);
// Disable browser back button from all stimuli
useDisableBrowserBack();
// Check if we have issues connecting to the database, if so show alert modal
const storeDispatch = useStoreDispatch();
const { setAlertModal } = useStoreActions();
useEffect(() => {
if (storageEngine?.getEngine() !== import.meta.env.VITE_STORAGE_ENGINE) {
storeDispatch(setAlertModal({
show: true,
message: `There was an issue connecting to the ${import.meta.env.VITE_STORAGE_ENGINE} database. This could be caused by a network issue or your adblocker. If you are using an adblocker, please disable it for this website and refresh.`,
title: 'Failed to connect to the storage engine',
}));
}
}, [setAlertModal, storageEngine, storeDispatch]);
// For study that does not involve screen recording
useEffect(() => {
if (!studyConfig || studyHasScreenRecording || !studyHasAudioRecording || !storageEngine || (status && status.endTime > 0) || isAnalysis) {
return;
}
if (audioStream.current) {
audioStream.current.stream.getTracks().forEach((track) => { track.stop(); audioStream.current?.stream.removeTrack(track); });
audioStream.current.stream.getAudioTracks().forEach((track) => { track.stop(); audioStream.current?.stream.removeTrack(track); });
audioStream.current.stop();
audioStream.current = null;
}
if ((stepConfig && !currentComponentHasAudioRecording) || currentComponent === 'end') {
storeDispatch(setIsRecording(false));
} else {
navigator.mediaDevices.getUserMedia({
audio: true,
}).then((s) => {
const recorder = new MediaRecorder(s);
audioStream.current = recorder;
let chunks : Blob[] = [];
recorder.addEventListener('start', () => {
chunks = [];
});
recorder.addEventListener('dataavailable', (event: BlobEvent) => {
if (event.data && event.data.size > 0) {
chunks.push(event.data);
}
});
const trialName = identifier;
recorder.addEventListener('stop', () => {
const { mimeType } = recorder;
const blob = new Blob(chunks, { type: mimeType });
storageEngine?.saveAudioRecording(blob, trialName);
});
audioStream.current.start();
storeDispatch(setIsRecording(true));
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentComponent, identifier, currentComponentHasAudioRecording]);
// For study involving screen recording
useEffect(() => {
if (!studyConfig || !(studyHasScreenRecording) || !storageEngine || (status && status.endTime > 0) || isAnalysis) {
return;
}
if (screenRecordingStream.current) {
stopScreenRecording();
screenCaptureTrialName.current = null;
}
if (currentComponent !== 'end' && isMediaCapturing && screenCaptureTrialName.current !== identifier && (currentComponentHasAudioRecording || currentComponentHasScreenRecording)) {
screenCaptureTrialName.current = identifier;
startScreenRecording(identifier);
}
if (currentComponent === 'end') {
stopScreenCapture();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentComponent, identifier, currentComponentHasAudioRecording, currentComponentHasScreenRecording, isMediaCapturing]);
// Find current block, if it has an ID, add it as a participant tag
const [blockForStep, setBlockForStep] = useState<string[]>([]);
const prevBlockForStepRef = useRef<string[]>([]);
useEffect(() => {
if (isAnalysis) {
return;
}
async function updateBlockForStep() {
// Get all nested block IDs
const blockIds = findBlockForStep(sequence, currentStep)
?.map((block) => block.currentBlock.id)
.filter((blockId) => blockId !== undefined) as string[] | undefined || [];
setBlockForStep(blockIds);
}
async function addParticipantTag() {
const prevBlockForStep = prevBlockForStepRef.current;
// Check if blockForStep has actually changed
const hasChanged = JSON.stringify(prevBlockForStep) !== JSON.stringify(blockForStep);
if (hasChanged && blockForStep.length > 0 && storageEngine) {
await storageEngine.addParticipantTags(blockForStep);
}
// Update the ref with the current value
prevBlockForStepRef.current = blockForStep;
}
updateBlockForStep().then(addParticipantTag);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentStep, storageEngine, sequence]);
const currentIdentifier = useCurrentIdentifier();
const currentConfig = useMemo(() => {
const toReturn = currentComponent && currentComponent !== 'end' && !currentComponent.startsWith('__') && studyComponentToIndividualComponent(stepConfig, studyConfig) as IndividualComponent;
if (typeof toReturn === 'object') {
const funcParams = answers[currentIdentifier]?.parameters;
const funcCorrectAnswer = answers[currentIdentifier]?.correctAnswer;
return {
...toReturn,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
parameters: funcParams || (toReturn as any).parameters || {},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
correctAnswer: funcCorrectAnswer || (toReturn as any).correctAnswer || undefined,
};
}
return toReturn as unknown as IndividualComponent;
}, [answers, currentComponent, currentIdentifier, stepConfig, studyConfig]);
useEffect(() => {
// Assume that screen recording video exists.
// The value is set to false from ScreenRecordingReplay component if video starts after stimulus start time.
storeDispatch(setAnalysisCanPlayScreenRecording(true));
}, [currentStep, setAnalysisCanPlayScreenRecording, storeDispatch]);
useFetchStylesheet(currentConfig?.stylesheetPath);
const { funcIndex } = useParams();
// Automatically forward a user to their last completed trial if they are returning to the study
useEffect(() => {
if (status && status.endTime > 0 && !isAnalysis && !modes.studyNavigatorEnabled && currentComponent !== 'end' && !currentComponent.startsWith('__') && typeof currentStep === 'number') {
let lastAnsweredTrialOrder = '0';
Object.values(answers).forEach((a) => {
if (a.endTime > 0) {
lastAnsweredTrialOrder = a.trialOrder;
}
});
const [trialOrderIndex, trialOrderFuncIndex] = lastAnsweredTrialOrder.split('_');
const indexNumber = Number(trialOrderIndex);
const funcIndexNumber = trialOrderFuncIndex ? Number(trialOrderFuncIndex) : undefined;
if (indexNumber > currentStep || (indexNumber === currentStep && funcIndexNumber !== undefined && funcIndex !== undefined && funcIndexNumber > Number(decryptIndex(funcIndex)))) {
navigate(`/${studyId}/${encryptIndex(indexNumber)}${funcIndexNumber !== undefined ? `/${encryptIndex(funcIndexNumber)}` : ''}`);
}
}
}, [answers, currentComponent, currentStep, funcIndex, isAnalysis, modes.studyNavigatorEnabled, navigate, status, studyId]);
// We're not using hooks below here, so we can return early if we're at the end of the study.
// This avoids issues with the component config being undefined for the end of the study.
if (currentComponent === 'end') {
return <StudyEnd />;
}
if (currentComponent === '__dynamicLoading') {
return null;
}
// Handle failed training
if (currentComponent === '__trainingFailed') {
return <TrainingFailed />;
}
// Handle timed out participants
if (currentComponent === '__timedOut') {
return <TimedOut />;
}
if (currentComponent === 'Notfound') {
return <ResourceNotFound email={studyConfig.uiConfig.contactEmail} />;
}
if (!storageEngine?.isConnected()) {
return (
<Center style={{ height: '80vh', flexDirection: 'column', textAlign: 'center' }}>
<IconPlugConnectedX size={48} stroke={1.5} color="orange" />
<Title mt="md" order={4}>Database Disconnected</Title>
<Text mt="md">Please check your network connection or disable your adblocker for this site, then refresh the page.</Text>
</Center>
);
}
if (participantId && storePartId !== participantId) {
return (
<Center style={{ height: '80vh' }}>
<Loader />
</Center>
);
}
const instruction = currentConfig?.instruction || '';
const instructionLocation = currentConfig.instructionLocation ?? studyConfig.uiConfig.instructionLocation ?? 'sidebar';
const instructionInSideBar = instructionLocation === 'sidebar';
if (studyHasScreenRecording && isAnalysis && analysisCanPlayScreenRecording) return <ScreenRecordingReplay key={`${currentStep}-stimulus`} />;
return (
<>
{instructionLocation === 'aboveStimulus' && <ReactMarkdownWrapper text={instruction} />}
<ResponseBlock
key={`${currentStep}-above-response-block`}
status={status}
config={currentConfig}
location="aboveStimulus"
/>
<Box
id={currentComponent}
className={currentConfig.type}
style={{
width: '100%',
display: 'flex',
flexGrow: currentConfig.type === 'website' ? 1 : undefined,
flexDirection: 'column',
...currentConfig.style,
}}
>
<Suspense key={`${currentStep}-stimulus`} fallback={<div>Loading...</div>}>
<>
{currentConfig.type === 'markdown' && <MarkdownController currentConfig={currentConfig} />}
{currentConfig.type === 'website' && <IframeController currentConfig={currentConfig} provState={analysisProvState} answers={answers} />}
{currentConfig.type === 'image' && <ImageController currentConfig={currentConfig} />}
{currentConfig.type === 'react-component' && <ReactComponentController currentConfig={currentConfig} provState={analysisProvState} answers={answers} />}
{currentConfig.type === 'vega' && <VegaController currentConfig={currentConfig} provState={analysisProvState as VegaProvState} />}
{currentConfig.type === 'video' && <VideoController currentConfig={currentConfig} />}
</>
</Suspense>
</Box>
{(instructionLocation === 'belowStimulus' || (instructionLocation === undefined && !instructionInSideBar)) && <ReactMarkdownWrapper text={instruction} />}
<ResponseBlock
key={`${currentStep}-below-response-block`}
status={status}
config={currentConfig}
location="belowStimulus"
/>
</>
);
}