@adriansteffan/reactive 0.2.6 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +796 -229
- package/bin/add.js +56 -0
- package/bin/deploy.js +48 -0
- package/bin/download.js +28 -0
- package/bin/ghsetup.js +21 -0
- package/bin/native.js +81 -0
- package/bin/setup.js +11 -17
- package/bin/study-deploy.js +60 -0
- package/bin/undeploy.js +15 -0
- package/extras/transcription/compose.yaml +32 -0
- package/{template → extras}/transcription/transcribe.py +4 -9
- package/package.json +62 -64
- package/src/bun-env.d.ts +1 -0
- package/src/bun-plugin.ts +22 -0
- package/src/components/canvasblock.tsx +209 -537
- package/src/components/checkdevice.tsx +64 -84
- package/src/components/fullscreenblock.tsx +130 -0
- package/src/components/icons.tsx +50 -0
- package/src/components/index.ts +9 -11
- package/src/components/microphonecheck.tsx +76 -38
- package/src/components/plaininput.tsx +48 -41
- package/src/components/prolificending.tsx +64 -28
- package/src/components/quest.tsx +89 -87
- package/src/components/{randomdotkinetogram.tsx → randomdotkinematogram.tsx} +205 -267
- package/src/components/{voicerecorder.tsx → recorder.tsx} +81 -106
- package/src/components/requestfilepermission.tsx +63 -0
- package/src/components/stimulus.tsx +373 -0
- package/src/components/storeui.tsx +94 -148
- package/src/components/text.tsx +61 -59
- package/src/components/tutorial.tsx +45 -47
- package/src/components/upload.tsx +133 -309
- package/src/components/voicerecording.tsx +90 -94
- package/src/core/backend.ts +20 -0
- package/src/core/bytecode.ts +178 -0
- package/src/core/columns.ts +50 -0
- package/src/core/component.ts +238 -0
- package/src/core/development.ts +32 -0
- package/src/core/experiment.ts +263 -0
- package/src/core/files.ts +291 -0
- package/src/core/params.ts +166 -0
- package/src/core/platform.ts +29 -0
- package/src/core/rows.ts +106 -0
- package/src/core/simulation.ts +158 -0
- package/src/core/slides.ts +178 -0
- package/src/core/store.ts +22 -0
- package/src/core/timeline.ts +399 -0
- package/src/fonts/AtkinsonHyperlegible-Bold.woff2 +0 -0
- package/src/fonts/AtkinsonHyperlegible-BoldItalic.woff2 +0 -0
- package/src/fonts/AtkinsonHyperlegible-Italic.woff2 +0 -0
- package/src/fonts/AtkinsonHyperlegible-Regular.woff2 +0 -0
- package/src/index.css +5 -7
- package/src/mod.tsx +31 -17
- package/src/native.ts +127 -0
- package/src/quest.ts +2 -0
- package/src/runner/contexts.ts +35 -0
- package/src/runner/devpanel.tsx +68 -0
- package/src/runner/experimentrunner.tsx +210 -0
- package/src/runner/fullscreenguard.tsx +93 -0
- package/src/runner/paraminput.tsx +86 -0
- package/src/runner/progressbar.tsx +20 -0
- package/src/runner/settingsscreen.tsx +517 -0
- package/src/{utils → runner}/theme.ts +9 -8
- package/src/server.ts +93 -0
- package/src/tutorial.ts +3 -0
- package/src/utils/array.ts +30 -62
- package/src/utils/canvas.ts +66 -0
- package/src/utils/ddm.ts +47 -70
- package/src/utils/distributions.ts +352 -61
- package/src/utils/fullscreen.ts +38 -0
- package/src/utils/ids.ts +11 -0
- package/src/utils/keys.ts +46 -0
- package/src/utils/llm.ts +15 -15
- package/src/utils/time.ts +12 -0
- package/src/utils/tts.ts +24 -24
- package/template/.dockerignore +3 -1
- package/template/.env.example +5 -0
- package/template/.github/workflows/deploy.yml +25 -0
- package/template/.gitignore +14 -0
- package/template/Dockerfile +9 -14
- package/template/README.md +74 -214
- package/template/biome.json +34 -0
- package/template/build.ts +20 -0
- package/template/bunfig.toml +3 -0
- package/template/docker-compose.yaml +4 -33
- package/template/index.html +2 -2
- package/template/package.json +16 -89
- package/template/public/.gitkeep +0 -0
- package/template/server.ts +32 -0
- package/template/simulate.ts +4 -44
- package/template/src/Experiment.tsx +76 -128
- package/template/src/bun-env.d.ts +1 -0
- package/template/src/index.css +1 -9
- package/template/src/main.tsx +25 -10
- package/template/tsconfig.json +4 -6
- package/.claude/settings.local.json +0 -61
- package/.eslintrc.cjs +0 -18
- package/.playwright-mcp/console-2026-04-07T14-22-59-060Z.log +0 -1
- package/.prettierrc +0 -5
- package/Dockerfile +0 -20
- package/dist/mod-HDegGPrf.js +0 -87434
- package/dist/mod.d.ts +0 -946
- package/dist/reactive.es.js +0 -87
- package/dist/reactive.umd.js +0 -10255
- package/dist/style.css +0 -5
- package/dist/web-7BbR1Ad7.js +0 -435
- package/dist/web-CATfZO1Q.js +0 -12
- package/planned.md +0 -103
- package/postcss.config.js +0 -6
- package/raw-loader-hooks.mjs +0 -25
- package/raw-loader.mjs +0 -5
- package/rdk_doc.md +0 -341
- package/src/components/enterfullscreen.tsx +0 -230
- package/src/components/exitfullscreen.tsx +0 -113
- package/src/components/experimentprovider.tsx +0 -69
- package/src/components/experimentrunner.tsx +0 -497
- package/src/components/mobilefilepermission.tsx +0 -68
- package/src/components/settingsscreen.tsx +0 -770
- package/src/types/array.d.ts +0 -6
- package/src/utils/audiodevice.ts +0 -4
- package/src/utils/bytecode.ts +0 -261
- package/src/utils/common.ts +0 -291
- package/src/utils/ddm.test.ts +0 -233
- package/src/utils/distributions.test.ts +0 -209
- package/src/utils/nodecompat.ts +0 -11
- package/src/utils/overlays.ts +0 -19
- package/src/utils/request.ts +0 -25
- package/src/utils/simulation.ts +0 -299
- package/src/utils/upload.ts +0 -275
- package/src/vite-env.d.ts +0 -1
- package/template/.env.template +0 -2
- package/template/.eslintrc.cjs +0 -18
- package/template/.prettierrc +0 -5
- package/template/backend/package-lock.json +0 -2544
- package/template/backend/package.json +0 -31
- package/template/backend/src/backend.ts +0 -131
- package/template/backend/tsconfig.json +0 -110
- package/template/capacitor.config.ts +0 -15
- package/template/electron/main.js +0 -131
- package/template/electron/preload.js +0 -9
- package/template/electron.vite.config.js +0 -45
- package/template/package-lock.json +0 -12467
- package/template/postcss.config.js +0 -5
- package/template/public/Atkinson_Hyperlegible/AtkinsonHyperlegible-Bold.ttf +0 -0
- package/template/public/Atkinson_Hyperlegible/AtkinsonHyperlegible-BoldItalic.ttf +0 -0
- package/template/public/Atkinson_Hyperlegible/AtkinsonHyperlegible-Italic.ttf +0 -0
- package/template/public/Atkinson_Hyperlegible/AtkinsonHyperlegible-Regular.ttf +0 -0
- package/template/shared.vite.config.js +0 -39
- package/template/src/vite-env.d.ts +0 -2
- package/template/tsconfig.node.json +0 -12
- package/template/vite.config.js +0 -11
- package/tsconfig.json +0 -30
- package/tsconfig.node.json +0 -12
- package/vite.config.ts +0 -39
- /package/{template → extras}/transcription/Dockerfile.cpu +0 -0
- /package/{template → extras}/transcription/Dockerfile.gpu +0 -0
- /package/{template/public/Atkinson_Hyperlegible → src/fonts}/OFL.txt +0 -0
|
@@ -1,77 +1,62 @@
|
|
|
1
|
-
import { BaseComponentProps } from '../
|
|
1
|
+
import { BaseComponentProps } from '../core/component';
|
|
2
2
|
import { useState } from 'react';
|
|
3
|
-
import {
|
|
4
|
-
import { registerFlattener } from '../utils/upload';
|
|
3
|
+
import { defineComponent } from '../core/component';
|
|
5
4
|
import { uniform } from '../utils/distributions';
|
|
6
|
-
import { useTheme, t } from '../
|
|
7
|
-
|
|
8
|
-
registerFlattener('PlainInput', 'session');
|
|
9
|
-
|
|
10
|
-
registerSimulation('PlainInput', (trialProps, _experimentState, simulators, participant) => {
|
|
11
|
-
const result = simulators.respond(trialProps, participant);
|
|
12
|
-
const typingDuration = String(result.value).length * uniform(50, 150);
|
|
13
|
-
return {
|
|
14
|
-
responseData: { value: result.value },
|
|
15
|
-
participantState: result.participantState,
|
|
16
|
-
storeUpdates: trialProps.storeupdate ? trialProps.storeupdate(result.value) : undefined,
|
|
17
|
-
duration: typingDuration,
|
|
18
|
-
};
|
|
19
|
-
}, {
|
|
20
|
-
respond: (_input: any, participant: any) => ({
|
|
21
|
-
value: 'simulated_input',
|
|
22
|
-
participantState: participant,
|
|
23
|
-
}),
|
|
24
|
-
});
|
|
5
|
+
import { useTheme, t } from '../runner/theme';
|
|
25
6
|
|
|
26
7
|
function PlainInput({
|
|
27
8
|
content,
|
|
28
|
-
buttonText = '
|
|
9
|
+
buttonText = 'Continue',
|
|
29
10
|
className = '',
|
|
11
|
+
containerClass,
|
|
12
|
+
centered = true,
|
|
30
13
|
next,
|
|
31
|
-
updateStore,
|
|
32
14
|
animate = false,
|
|
33
|
-
storeupdate,
|
|
34
15
|
placeholder = 'Enter your response here',
|
|
35
|
-
|
|
36
|
-
|
|
16
|
+
required = true,
|
|
17
|
+
}: BaseComponentProps<{ value: string }> & {
|
|
18
|
+
content?: React.ReactNode;
|
|
19
|
+
/** The button that submits the answer; an empty text shows none, and Enter submits. */
|
|
37
20
|
buttonText?: string;
|
|
38
|
-
onButtonClick?: () => void;
|
|
39
21
|
className?: string;
|
|
22
|
+
containerClass?: string;
|
|
23
|
+
centered?: boolean;
|
|
40
24
|
animate?: boolean;
|
|
41
|
-
storeupdate?: (entry: string) => { [key: string]: any };
|
|
42
25
|
placeholder?: string;
|
|
26
|
+
/** Keeps the button disabled until something is entered. */
|
|
27
|
+
required?: boolean;
|
|
43
28
|
}) {
|
|
44
29
|
const th = t(useTheme());
|
|
45
30
|
const [inputValue, setInputValue] = useState('');
|
|
31
|
+
const canSubmit = !required || inputValue.trim() !== '';
|
|
46
32
|
|
|
47
33
|
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
48
34
|
setInputValue(e.target.value);
|
|
49
35
|
};
|
|
50
36
|
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
next({ value: inputValue });
|
|
37
|
+
const handleSubmit = (e: React.FormEvent) => {
|
|
38
|
+
e.preventDefault();
|
|
39
|
+
if (canSubmit) next({ value: inputValue });
|
|
56
40
|
};
|
|
57
41
|
|
|
58
42
|
return (
|
|
59
|
-
<div className={`min-h-screen ${th.containerBg}`}>
|
|
60
|
-
<div className={`max-w-prose mx-auto ${className}
|
|
43
|
+
<div className={`min-h-screen ${centered ? 'flex items-center justify-center' : ''} ${containerClass ?? th.containerBg}`}>
|
|
44
|
+
<div className={`w-full max-w-prose mx-auto ${className} pt-20 pb-20 px-4 `}>
|
|
61
45
|
<article
|
|
62
|
-
className={
|
|
46
|
+
className={`${th.article}
|
|
63
47
|
${animate ? 'animate-slide-down opacity-0' : ''}`}
|
|
64
48
|
>
|
|
65
49
|
{content}
|
|
66
50
|
</article>
|
|
67
51
|
|
|
52
|
+
<form onSubmit={handleSubmit}>
|
|
68
53
|
<div className={`mt-8 ${animate ? 'animate-slide-down opacity-0' : ''}`}>
|
|
69
54
|
<input
|
|
70
55
|
type='text'
|
|
71
56
|
value={inputValue}
|
|
72
57
|
onChange={handleChange}
|
|
73
58
|
placeholder={placeholder}
|
|
74
|
-
className={`w-full px-4 py-3 border-2 ${th.inputBorder} ${th.inputBg} ${th.inputText} rounded-xl text-lg focus:outline-none
|
|
59
|
+
className={`w-full px-4 py-3 border-2 ${th.inputBorder} ${th.inputBg} ${th.inputText} rounded-xl text-lg focus:outline-none`}
|
|
75
60
|
/>
|
|
76
61
|
</div>
|
|
77
62
|
|
|
@@ -81,16 +66,38 @@ function PlainInput({
|
|
|
81
66
|
style={animate ? { animationDelay: '1s' } : {}}
|
|
82
67
|
>
|
|
83
68
|
<button
|
|
84
|
-
|
|
85
|
-
|
|
69
|
+
type='submit'
|
|
70
|
+
disabled={!canSubmit}
|
|
71
|
+
className={
|
|
72
|
+
canSubmit
|
|
73
|
+
? th.button
|
|
74
|
+
: `px-8 py-3 border-2 font-bold text-lg rounded-xl ${th.buttonDisabledBg} ${th.buttonDisabledBorder} ${th.buttonDisabledText} ${th.buttonDisabledShadow} cursor-default`
|
|
75
|
+
}
|
|
86
76
|
>
|
|
87
77
|
{buttonText}
|
|
88
78
|
</button>
|
|
89
79
|
</div>
|
|
90
80
|
)}
|
|
81
|
+
</form>
|
|
91
82
|
</div>
|
|
92
83
|
</div>
|
|
93
84
|
);
|
|
94
85
|
}
|
|
95
86
|
|
|
96
|
-
export default
|
|
87
|
+
export default defineComponent({
|
|
88
|
+
name: 'PlainInput',
|
|
89
|
+
component: PlainInput,
|
|
90
|
+
session: true,
|
|
91
|
+
simulate: ({ props, simulators, participant }) => {
|
|
92
|
+
const result = simulators.respond({ props, participant });
|
|
93
|
+
const typingDuration = String(result.value).length * uniform(50, 150);
|
|
94
|
+
return {
|
|
95
|
+
recorded: { value: result.value },
|
|
96
|
+
participant: result.participant,
|
|
97
|
+
durationMs: typingDuration,
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
defaultSimulators: {
|
|
101
|
+
respond: () => ({ value: 'simulated_input' }),
|
|
102
|
+
},
|
|
103
|
+
});
|
|
@@ -1,45 +1,81 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import { useParams } from '../runner/contexts';
|
|
3
|
+
import Text from './text';
|
|
4
|
+
import { noopSimulate } from '../core/simulation';
|
|
5
|
+
import { defineComponent } from '../core/component';
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
const params = {
|
|
8
|
+
cc: { default: '', description: 'Prolific completion code, used when the prolificCode prop is not set' },
|
|
9
|
+
};
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
const defaultContent = (code: string) => (
|
|
12
|
+
<p>
|
|
13
|
+
Thank you! Your data has been successfully submitted. <br /> You can go back to Prolific and
|
|
14
|
+
enter the code {code} to finish the study. Alternatively, you can click on this link:{' '}
|
|
15
|
+
<a
|
|
16
|
+
target='_blank'
|
|
17
|
+
rel='noopener noreferrer'
|
|
18
|
+
className='text-blue-400'
|
|
19
|
+
href={`https://app.prolific.com/submissions/complete?cc=${code}`}
|
|
20
|
+
>
|
|
21
|
+
Go to Prolific
|
|
22
|
+
</a>
|
|
23
|
+
</p>
|
|
24
|
+
);
|
|
12
25
|
|
|
13
|
-
|
|
26
|
+
function ProlificEnding({
|
|
14
27
|
prolificCode,
|
|
28
|
+
content = defaultContent,
|
|
15
29
|
className,
|
|
16
30
|
containerClass,
|
|
17
31
|
centered,
|
|
18
32
|
animate,
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
33
|
+
}: {
|
|
34
|
+
prolificCode?: string;
|
|
35
|
+
/** The message, given the completion code. Prolific's completion link is https://app.prolific.com/submissions/complete?cc=<code>. */
|
|
36
|
+
content?: (code: string) => ReactNode;
|
|
37
|
+
className?: string;
|
|
38
|
+
containerClass?: string;
|
|
39
|
+
centered?: boolean;
|
|
40
|
+
animate?: boolean;
|
|
41
|
+
}) {
|
|
42
|
+
const { cc } = useParams(params);
|
|
23
43
|
|
|
24
|
-
const
|
|
44
|
+
const message = (
|
|
25
45
|
<div className='flex flex-col items-center'>
|
|
26
46
|
<svg className='w-12 h-12' fill='none' stroke='currentColor' viewBox='0 0 24 24'>
|
|
27
47
|
<path strokeLinecap='round' strokeLinejoin='round' strokeWidth={2} d='M5 13l4 4L19 7' />
|
|
28
48
|
</svg>
|
|
29
|
-
|
|
30
|
-
Thank you! Your data has been successfully submitted. <br /> You can go back to Prolific and
|
|
31
|
-
enter the code {prolificCodeUsed} to finish the study. Alternatively, you can click on this
|
|
32
|
-
link:{' '}
|
|
33
|
-
<a
|
|
34
|
-
target='_blank'
|
|
35
|
-
className='text-blue-400'
|
|
36
|
-
href={`https://app.prolific.com/submissions/complete?cc=${prolificCodeUsed}`}
|
|
37
|
-
>
|
|
38
|
-
Go to Prolific
|
|
39
|
-
</a>
|
|
40
|
-
</p>
|
|
49
|
+
{content(prolificCode ?? cc)}
|
|
41
50
|
</div>
|
|
42
51
|
);
|
|
43
52
|
|
|
44
|
-
return
|
|
53
|
+
return (
|
|
54
|
+
<Text
|
|
55
|
+
className={className}
|
|
56
|
+
containerClass={containerClass}
|
|
57
|
+
centered={centered}
|
|
58
|
+
animate={animate}
|
|
59
|
+
content={message}
|
|
60
|
+
buttonText=''
|
|
61
|
+
next={() => {}}
|
|
62
|
+
/>
|
|
63
|
+
);
|
|
45
64
|
}
|
|
65
|
+
|
|
66
|
+
export default defineComponent({
|
|
67
|
+
name: 'ProlificEnding',
|
|
68
|
+
component: ProlificEnding,
|
|
69
|
+
csv: null,
|
|
70
|
+
params,
|
|
71
|
+
simulate: noopSimulate,
|
|
72
|
+
check: ({ props, params, trials, index, production }) => {
|
|
73
|
+
if (!trials.slice(0, index).some((trial) => trial.type === 'Upload')) {
|
|
74
|
+
return 'ProlificEnding ends the experiment, so an Upload has to come before it, or the data is never saved.';
|
|
75
|
+
}
|
|
76
|
+
// Development and simulation run without the real link, which may be what sets the code
|
|
77
|
+
if (production && props !== undefined && !props.prolificCode && !params.cc) {
|
|
78
|
+
return 'ProlificEnding has no completion code to show: set its prolificCode, or the cc param in the link.';
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
});
|
package/src/components/quest.tsx
CHANGED
|
@@ -1,70 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createElement, type ComponentType, useState } from 'react';
|
|
2
2
|
import { defaultCss, Model, Question, Serializer } from 'survey-core';
|
|
3
3
|
import { ReactQuestionFactory, Survey, SurveyQuestionElementBase } from 'survey-react-ui';
|
|
4
4
|
import { ContrastLight, ContrastDark } from 'survey-core/themes';
|
|
5
5
|
import 'survey-core/survey-core.min.css';
|
|
6
|
-
import {
|
|
7
|
-
import { registerFlattener } from '../utils/upload';
|
|
6
|
+
import { defineComponent } from '../core/component';
|
|
8
7
|
import { uniform } from '../utils/distributions';
|
|
9
|
-
import { useTheme, t } from '../
|
|
10
|
-
|
|
11
|
-
registerFlattener('Quest', 'session');
|
|
12
|
-
|
|
13
|
-
registerSimulation('Quest', (trialProps, _experimentState, simulators, participant) => {
|
|
14
|
-
const responseData: Record<string, any> = {};
|
|
15
|
-
let totalDuration = 0;
|
|
16
|
-
const pages = trialProps.surveyJson?.pages || [{ elements: trialProps.surveyJson?.elements || [] }];
|
|
17
|
-
for (const page of pages) {
|
|
18
|
-
for (const el of page.elements || []) {
|
|
19
|
-
if (!el.name) continue;
|
|
20
|
-
const result = simulators.answerQuestion(el, participant);
|
|
21
|
-
participant = result.participantState;
|
|
22
|
-
responseData[el.name] = result.value;
|
|
23
|
-
totalDuration += result.duration ?? 0;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return { responseData, participantState: participant, duration: totalDuration };
|
|
27
|
-
}, {
|
|
28
|
-
answerQuestion: (question: any, participant: any) => {
|
|
29
|
-
let value;
|
|
30
|
-
switch (question.type) {
|
|
31
|
-
case 'rating': {
|
|
32
|
-
const min = question.rateMin ?? 1, max = question.rateMax ?? 5;
|
|
33
|
-
value = min + Math.floor(uniform(0, max - min + 1));
|
|
34
|
-
break;
|
|
35
|
-
}
|
|
36
|
-
case 'boolean': value = uniform(0, 1) > 0.5; break;
|
|
37
|
-
case 'text': case 'comment': value = 'simulated_response'; break;
|
|
38
|
-
case 'radiogroup': case 'dropdown': {
|
|
39
|
-
const c = question.choices?.[Math.floor(uniform(0, question.choices?.length || 0))];
|
|
40
|
-
value = c !== undefined ? (typeof c === 'object' ? c.value : c) : null;
|
|
41
|
-
break;
|
|
42
|
-
}
|
|
43
|
-
case 'checkbox': {
|
|
44
|
-
if (question.choices?.length) {
|
|
45
|
-
const n = 1 + Math.floor(uniform(0, question.choices.length));
|
|
46
|
-
value = [...question.choices]
|
|
47
|
-
.sort(() => uniform(0, 1) - 0.5).slice(0, n)
|
|
48
|
-
.map((c: any) => typeof c === 'object' ? c.value : c);
|
|
49
|
-
}
|
|
50
|
-
break;
|
|
51
|
-
}
|
|
52
|
-
case 'matrix': {
|
|
53
|
-
if (question.rows?.length && question.columns?.length) {
|
|
54
|
-
value = Object.fromEntries(
|
|
55
|
-
question.rows.map((r: any) => {
|
|
56
|
-
const col = question.columns[Math.floor(uniform(0, question.columns.length))];
|
|
57
|
-
return [typeof r === 'object' ? r.value : r, typeof col === 'object' ? col.value : col];
|
|
58
|
-
}),
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
break;
|
|
62
|
-
}
|
|
63
|
-
default: value = null;
|
|
64
|
-
}
|
|
65
|
-
return { value, participantState: participant, duration: uniform(1000, 5000) };
|
|
66
|
-
},
|
|
67
|
-
});
|
|
8
|
+
import { useTheme, t } from '../runner/theme';
|
|
9
|
+
import { Recorder } from './recorder';
|
|
68
10
|
|
|
69
11
|
type ComponentsMap = {
|
|
70
12
|
[key: string]: ComponentType<any>;
|
|
@@ -113,52 +55,112 @@ const registerCustomQuestion = (name: string, component: ComponentType<any>) =>
|
|
|
113
55
|
});
|
|
114
56
|
};
|
|
115
57
|
|
|
116
|
-
// The
|
|
58
|
+
// The voicerecorder question type, which records an answer with the shared recorder
|
|
59
|
+
function VoiceRecorderQuestion({ setValue }: { setValue: (data: object | null) => void }) {
|
|
60
|
+
return <Recorder handleSaveVoiceData={(data) => setValue(data)} handleDiscardVoiceData={() => setValue(null)} />;
|
|
61
|
+
}
|
|
62
|
+
|
|
117
63
|
function Quest({
|
|
118
64
|
next,
|
|
119
65
|
surveyJson,
|
|
120
|
-
customQuestions
|
|
121
|
-
|
|
66
|
+
customQuestions,
|
|
67
|
+
surveyTheme,
|
|
68
|
+
className = '',
|
|
122
69
|
containerClass,
|
|
123
70
|
}: {
|
|
124
|
-
next: (data
|
|
125
|
-
surveyJson:
|
|
71
|
+
next: (data?: Record<string, any>) => void;
|
|
72
|
+
surveyJson: Record<string, any>;
|
|
73
|
+
/** Custom question types, by the name the survey's elements use as their type. */
|
|
126
74
|
customQuestions?: ComponentsMap;
|
|
127
|
-
theme
|
|
75
|
+
/** A SurveyJS theme; by default one that matches the experiment's theme. */
|
|
76
|
+
surveyTheme?: any;
|
|
77
|
+
className?: string;
|
|
128
78
|
containerClass?: string;
|
|
129
79
|
}) {
|
|
130
80
|
const contextTheme = useTheme();
|
|
131
81
|
const th = t(contextTheme);
|
|
132
82
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
...surveyJson,
|
|
139
|
-
|
|
83
|
+
// One survey for the trial, so that a re-render keeps the participant's answers
|
|
84
|
+
const [survey] = useState(() => {
|
|
85
|
+
for (const [name, component] of Object.entries({ voicerecorder: VoiceRecorderQuestion, ...customQuestions })) {
|
|
86
|
+
registerCustomQuestion(name, component);
|
|
87
|
+
}
|
|
88
|
+
const model = new Model({ ...surveyJson, css: { ...defaultCss, root: 'sd-root-modern custom-root' } });
|
|
89
|
+
model.applyTheme(surveyTheme ?? (contextTheme.startsWith('dark') ? ContrastDark : ContrastLight));
|
|
90
|
+
model.onComplete.add((sender: Model) => next({ ...sender.data }));
|
|
91
|
+
return model;
|
|
140
92
|
});
|
|
141
|
-
survey.applyTheme(theme ?? (contextTheme.startsWith('dark') ? ContrastDark : ContrastLight));
|
|
142
|
-
|
|
143
|
-
const saveResults = useCallback(
|
|
144
|
-
(sender: any) => {
|
|
145
|
-
next({ ...sender.data });
|
|
146
|
-
},
|
|
147
|
-
[next],
|
|
148
|
-
);
|
|
149
|
-
|
|
150
|
-
survey.onComplete.add(saveResults);
|
|
151
93
|
|
|
152
94
|
const useCustomBg = !!containerClass || contextTheme.startsWith('dark');
|
|
153
95
|
|
|
154
96
|
return (
|
|
155
97
|
<div className={`min-h-screen ${containerClass ?? th.containerBg}`}>
|
|
156
98
|
{!useCustomBg && <div className='absolute inset-0 -z-10 h-full w-full bg-white bg-[radial-gradient(#e5e7eb_1px,transparent_1px)] [background-size:16px_16px]' />}
|
|
157
|
-
<div className={`max-w-4xl mx-auto px-4${useCustomBg ? ' quest-custom-bg' : ''}`}>
|
|
99
|
+
<div className={`max-w-4xl mx-auto px-4 ${className}${useCustomBg ? ' quest-custom-bg' : ''}`}>
|
|
158
100
|
<Survey model={survey} />
|
|
159
101
|
</div>
|
|
160
102
|
</div>
|
|
161
103
|
);
|
|
162
104
|
}
|
|
163
105
|
|
|
164
|
-
export default
|
|
106
|
+
export default defineComponent({
|
|
107
|
+
name: 'Quest',
|
|
108
|
+
component: Quest,
|
|
109
|
+
session: true,
|
|
110
|
+
simulate: ({ props, simulators, participant }) => {
|
|
111
|
+
const recorded: Record<string, any> = {};
|
|
112
|
+
let totalDuration = 0;
|
|
113
|
+
const pages = props.surveyJson?.pages || [{ elements: props.surveyJson?.elements || [] }];
|
|
114
|
+
for (const page of pages) {
|
|
115
|
+
for (const el of page.elements || []) {
|
|
116
|
+
if (!el.name) continue;
|
|
117
|
+
const result = simulators.answerQuestion({ props, participant, question: el });
|
|
118
|
+
participant = result.participant ?? participant;
|
|
119
|
+
recorded[el.name] = result.value.answer;
|
|
120
|
+
totalDuration += result.value.durationMs;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return { recorded, participant, durationMs: totalDuration };
|
|
124
|
+
},
|
|
125
|
+
defaultSimulators: {
|
|
126
|
+
answerQuestion: ({ question }) => {
|
|
127
|
+
let value: any;
|
|
128
|
+
switch (question.type) {
|
|
129
|
+
case 'rating': {
|
|
130
|
+
const min = question.rateMin ?? 1, max = question.rateMax ?? 5;
|
|
131
|
+
value = min + Math.floor(uniform(0, max - min + 1));
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
case 'boolean': value = uniform(0, 1) > 0.5; break;
|
|
135
|
+
case 'text': case 'comment': value = 'simulated_response'; break;
|
|
136
|
+
case 'radiogroup': case 'dropdown': {
|
|
137
|
+
const c = question.choices?.[Math.floor(uniform(0, question.choices?.length || 0))];
|
|
138
|
+
value = c !== undefined ? (typeof c === 'object' ? c.value : c) : null;
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
case 'checkbox': {
|
|
142
|
+
if (question.choices?.length) {
|
|
143
|
+
const n = 1 + Math.floor(uniform(0, question.choices.length));
|
|
144
|
+
value = [...question.choices]
|
|
145
|
+
.sort(() => uniform(0, 1) - 0.5).slice(0, n)
|
|
146
|
+
.map((c: any) => typeof c === 'object' ? c.value : c);
|
|
147
|
+
}
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
case 'matrix': {
|
|
151
|
+
if (question.rows?.length && question.columns?.length) {
|
|
152
|
+
value = Object.fromEntries(
|
|
153
|
+
question.rows.map((r: any) => {
|
|
154
|
+
const col = question.columns[Math.floor(uniform(0, question.columns.length))];
|
|
155
|
+
return [typeof r === 'object' ? r.value : r, typeof col === 'object' ? col.value : col];
|
|
156
|
+
}),
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
default: value = null;
|
|
162
|
+
}
|
|
163
|
+
return { value: { answer: value, durationMs: uniform(1000, 5000) } };
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
});
|