File size: 8,278 Bytes
96a945a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import {
  Box, Card, CardContent, Typography, Button, FormControl,
  InputLabel, Select, MenuItem, CircularProgress, Fade
} from '@mui/material';

const PLATES = [
  {
    src: '/tutorial/colorblind/plate_demo.png',
    question: 'What number do you see?',
    options: ['12', '4', '10', "I can't tell"],
    correctAnswer: '12',
  },
  {
    src: '/tutorial/colorblind/plate_redgreen.png',
    question: 'What number do you see?',
    options: ['74', '21', "I can't tell", 'Something else'],
    correctAnswer: '74',
    field: 'cb_redgreen',
  },
  {
    src: '/tutorial/colorblind/plate_blueyellow.png',
    question: 'What number do you see?',
    options: ['6', "I can't tell", '8', 'I see nothing'],
    correctAnswer: '6',
    field: 'cb_blueyellow',
  },
];

function classifyAnswer(plate, answer) {
  if (answer === plate.correctAnswer) return 'normal';
  if (answer === "I can't tell" || answer === 'I see nothing' || answer === 'Something else')
    return 'unsure';
  return 'deficient';
}

function detectDevice() {
  const ua = navigator.userAgent;
  if (/tablet|ipad|playbook|silk/i.test(ua)) return 'tablet';
  if (/mobile|iphone|ipod|android|blackberry|opera mini|iemobile/i.test(ua)) return 'mobile';
  return 'desktop';
}

export default function Welcome() {
  const navigate = useNavigate();
  const [plateStep, setPlateStep] = useState(0);
  const [plateAnswers, setPlateAnswers] = useState(['', '', '']);
  const [expertise, setExpertise] = useState('');
  const [loading, setLoading] = useState(false);

  const allPlatesDone = plateStep >= PLATES.length;

  const handlePlateAnswer = (answer) => {
    const updated = [...plateAnswers];
    updated[plateStep] = answer;
    setPlateAnswers(updated);
  };

  const handlePlateNext = () => {
    setPlateStep((s) => s + 1);
  };

  const handleStart = async () => {
    if (!allPlatesDone || !expertise) return;
    setLoading(true);

    // Classify colorblindness results
    const cbRedgreen = classifyAnswer(PLATES[1], plateAnswers[1]);
    const cbBlueyellow = classifyAnswer(PLATES[2], plateAnswers[2]);
    const colorblind =
      cbRedgreen !== 'normal' || cbBlueyellow !== 'normal' ? 1 : 0;
    const device = detectDevice();

    try {
      const res = await fetch('/api/session/start', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          expertise,
          colorblind,
          device,
          cb_redgreen: cbRedgreen,
          cb_blueyellow: cbBlueyellow,
        }),
      });
      const data = await res.json();

      localStorage.setItem('session_id', data.session_id);
      localStorage.setItem('trials', JSON.stringify(data.trials));
      localStorage.setItem('current_index', '0');

      navigate('/tutorial');
    } catch (err) {
      console.error('Failed to start session:', err);
      setLoading(false);
    }
  };

  const currentPlate = !allPlatesDone ? PLATES[plateStep] : null;
  const currentAnswer = !allPlatesDone ? plateAnswers[plateStep] : '';

  return (
    <Box

      sx={{

        minHeight: '100vh',

        display: 'flex',

        alignItems: 'center',

        justifyContent: 'center',

        p: 2,

      }}

    >

      <Card sx={{ maxWidth: 520, width: '100%' }}>

        <CardContent

          sx={{

            p: 4,

            display: 'flex',

            flexDirection: 'column',

            alignItems: 'center',

            gap: 3,

          }}

        >

          <Typography

            variant="h3"

            textAlign="center"

            sx={{ fontSize: { xs: '1.8rem', sm: '2.4rem' } }}

          >

            Can you spot colorization artifacts?

          </Typography>

          <Typography variant="body1" color="text.secondary" textAlign="center">

            Look at 50 images and decide: are colorization artifacts present or absent?

            Takes about 5 minutes.

          </Typography>



          {/* Colorblindness plates — sequential */}

          {!allPlatesDone && currentPlate && (

            <Fade in key={plateStep} timeout={300}>

              <Box

                sx={{

                  display: 'flex',

                  flexDirection: 'column',

                  alignItems: 'center',

                  gap: 1.5,

                  width: '100%',

                }}

              >

                <Typography

                  variant="caption"

                  color="text.secondary"

                  sx={{ opacity: 0.6 }}

                >

                  Vision check {plateStep + 1} of {PLATES.length}

                </Typography>



                <Box

                  component="img"

                  src={currentPlate.src}

                  alt="Color vision test plate"

                  sx={{

                    width: 300,

                    height: 300,

                    objectFit: 'contain',

                    borderRadius: '50%',

                  }}

                />



                <Typography variant="body2" color="text.secondary">

                  {currentPlate.question}

                </Typography>



                <Box

                  sx={{

                    display: 'flex',

                    gap: 1,

                    flexWrap: 'wrap',

                    justifyContent: 'center',

                  }}

                >

                  {currentPlate.options.map((opt) => (

                    <Button

                      key={opt}

                      variant={currentAnswer === opt ? 'contained' : 'outlined'}

                      size="small"

                      onClick={() => handlePlateAnswer(opt)}

                      sx={{ minWidth: 80 }}

                    >

                      {opt}

                    </Button>

                  ))}

                </Box>



                <Button

                  variant="contained"

                  size="small"

                  disabled={!currentAnswer}

                  onClick={handlePlateNext}

                  sx={{ mt: 0.5 }}

                >

                  Next &rarr;

                </Button>

              </Box>

            </Fade>

          )}



          {/* Expertise + Start — shown after all plates */}

          {allPlatesDone && (

            <Fade in timeout={300}>

              <Box

                sx={{

                  display: 'flex',

                  flexDirection: 'column',

                  alignItems: 'center',

                  gap: 3,

                  width: '100%',

                }}

              >

                <FormControl fullWidth>

                  <InputLabel>Your background</InputLabel>

                  <Select

                    value={expertise}

                    label="Your background"

                    onChange={(e) => setExpertise(e.target.value)}

                  >

                    <MenuItem value="General public">General public</MenuItem>

                    <MenuItem value="Photographer / Artist">

                      Photographer / Artist

                    </MenuItem>

                    <MenuItem value="Researcher / Academic">

                      Researcher / Academic

                    </MenuItem>

                    <MenuItem value="Computer Vision / ML expert">

                      Computer Vision / ML expert

                    </MenuItem>

                  </Select>

                </FormControl>



                <Button

                  variant="contained"

                  size="large"

                  fullWidth

                  disabled={!expertise || loading}

                  onClick={handleStart}

                  sx={{ mt: 1 }}

                >

                  {loading ? (

                    <CircularProgress size={24} color="inherit" />

                  ) : (

                    'Start \u2192'

                  )}

                </Button>

              </Box>

            </Fade>

          )}

        </CardContent>

      </Card>

    </Box>
  );
}