All files / web/src/components/flowchart FlowchartDecision.tsx

0% Statements 0/309
0% Branches 0/1
0% Functions 0/1
0% Lines 0/309

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
'use client'

import { useState, useEffect } from 'react'
import * as RadioGroup from '@radix-ui/react-radio-group'
import type { DecisionOption } from '@/lib/flowcharts/schema'
import { css, cx } from '../../../styled-system/css'
import { hstack, vstack } from '../../../styled-system/patterns'

interface DecisionOptionWithPath extends DecisionOption {
  /** Where this option leads (next node title) */
  leadsTo?: string
}

interface FlowchartDecisionProps {
  options: DecisionOptionWithPath[]
  onSelect: (value: string) => void
  /** Wrong answer value to highlight and shake briefly */
  wrongAnswer?: string
  /** Correct answer to highlight after wrong answer */
  correctAnswer?: string
}

/**
 * Decision node UI using Radix RadioGroup for accessibility.
 * Each option card shows where it leads in the flowchart.
 */
export function FlowchartDecision({
  options,
  onSelect,
  wrongAnswer,
  correctAnswer,
}: FlowchartDecisionProps) {
  const [isShaking, setIsShaking] = useState(false)
  const [showFeedback, setShowFeedback] = useState(false)
  const [selectedValue, setSelectedValue] = useState<string | undefined>()

  // When wrongAnswer changes, trigger shake animation
  useEffect(() => {
    if (wrongAnswer) {
      setIsShaking(true)
      setShowFeedback(true)
      setSelectedValue(undefined)

      // Reset shake after animation
      const shakeTimer = setTimeout(() => setIsShaking(false), 500)
      // Reset feedback after a brief delay so they can try again
      const feedbackTimer = setTimeout(() => setShowFeedback(false), 1500)

      return () => {
        clearTimeout(shakeTimer)
        clearTimeout(feedbackTimer)
      }
    }
  }, [wrongAnswer])

  const handleSelect = (value: string) => {
    if (showFeedback) return // Don't allow selection during feedback
    setSelectedValue(value)
    onSelect(value)
  }

  return (
    <RadioGroup.Root
      data-testid="decision-radio-group"
      data-option-count={options.length}
      data-is-shaking={isShaking}
      data-showing-feedback={showFeedback}
      value={selectedValue}
      onValueChange={handleSelect}
      className={cx(
        hstack({ gap: '4', justifyContent: 'center', flexWrap: 'wrap', alignItems: 'stretch' }),
        isShaking ? 'shake-animation' : ''
      )}
    >
      {options.map((option, idx) => {
        const isCorrect = showFeedback && correctAnswer === option.value
        const isWrong = showFeedback && wrongAnswer === option.value

        return (
          <RadioGroup.Item
            key={option.value}
            data-testid={`decision-option-${idx}`}
            data-option-value={option.value}
            data-is-correct={isCorrect}
            data-is-wrong={isWrong}
            value={option.value}
            disabled={showFeedback}
            className={css({
              all: 'unset',
              boxSizing: 'border-box',
              display: 'flex',
              flexDirection: 'column',
              padding: '0',
              borderRadius: 'xl',
              border: '3px solid',
              cursor: showFeedback ? 'not-allowed' : 'pointer',
              transition: 'all 0.2s',
              minWidth: '160px',
              maxWidth: '200px',
              flex: '1',
              position: 'relative',
              overflow: 'hidden',

              // Base styles based on state
              borderColor: isCorrect
                ? { base: 'green.500', _dark: 'green.400' }
                : isWrong
                  ? { base: 'red.500', _dark: 'red.400' }
                  : { base: 'gray.300', _dark: 'gray.600' },

              // Hover
              _hover: showFeedback
                ? {}
                : {
                    borderColor: { base: 'blue.500', _dark: 'blue.400' },
                    transform: 'scale(1.02)',
                    boxShadow: 'lg',
                  },

              // Focus visible
              _focusVisible: {
                outline: '3px solid',
                outlineColor: { base: 'blue.400', _dark: 'blue.500' },
                outlineOffset: '2px',
              },

              // Active/pressed
              _active: showFeedback
                ? {}
                : {
                    transform: 'scale(0.98)',
                  },

              // Selected (checked)
              '&[data-state="checked"]': {
                borderColor: { base: 'blue.500', _dark: 'blue.400' },
              },
            })}
          >
            {/* Choice label header */}
            <div
              className={css({
                paddingY: '3',
                paddingX: '4',
                fontSize: 'lg',
                fontWeight: 'bold',
                textAlign: 'center',
                backgroundColor: isCorrect
                  ? { base: 'green.100', _dark: 'green.800' }
                  : isWrong
                    ? { base: 'red.100', _dark: 'red.800' }
                    : { base: 'gray.100', _dark: 'gray.700' },
                color: isCorrect
                  ? { base: 'green.800', _dark: 'green.200' }
                  : isWrong
                    ? { base: 'red.800', _dark: 'red.200' }
                    : { base: 'gray.800', _dark: 'gray.200' },
                borderBottom: '1px solid',
                borderColor: isCorrect
                  ? { base: 'green.300', _dark: 'green.600' }
                  : isWrong
                    ? { base: 'red.300', _dark: 'red.600' }
                    : { base: 'gray.200', _dark: 'gray.600' },
              })}
            >
              {/* Correct/wrong indicator */}
              {isCorrect && (
                <span
                  className={css({
                    position: 'absolute',
                    top: '8px',
                    right: '8px',
                    width: '24px',
                    height: '24px',
                    backgroundColor: 'green.500',
                    borderRadius: 'full',
                    display: 'flex',
                    alignItems: 'center',
                    justifyContent: 'center',
                    fontSize: 'sm',
                    color: 'white',
                    fontWeight: 'bold',
                  })}
                >

                </span>
              )}
              {isWrong && (
                <span
                  className={css({
                    position: 'absolute',
                    top: '8px',
                    right: '8px',
                    width: '24px',
                    height: '24px',
                    backgroundColor: 'red.500',
                    borderRadius: 'full',
                    display: 'flex',
                    alignItems: 'center',
                    justifyContent: 'center',
                    fontSize: 'sm',
                    color: 'white',
                    fontWeight: 'bold',
                  })}
                >

                </span>
              )}
              {option.label}
            </div>

            {/* Path preview - where this leads */}
            {option.leadsTo && (
              <div
                className={css({
                  paddingY: '2',
                  paddingX: '3',
                  backgroundColor: { base: 'white', _dark: 'gray.800' },
                  flex: '1',
                  display: 'flex',
                  flexDirection: 'column',
                  alignItems: 'center',
                  justifyContent: 'center',
                  gap: '1',
                })}
              >
                <span
                  className={css({
                    fontSize: 'lg',
                    color: { base: 'gray.400', _dark: 'gray.500' },
                  })}
                >

                </span>
                <span
                  className={css({
                    fontSize: 'xs',
                    color: { base: 'gray.600', _dark: 'gray.400' },
                    textAlign: 'center',
                    lineHeight: 'tight',
                  })}
                >
                  {option.leadsTo}
                </span>
              </div>
            )}
          </RadioGroup.Item>
        )
      })}

      {/* Inline styles for shake animation */}
      <style
        dangerouslySetInnerHTML={{
          __html: `
            @keyframes shake {
              0%, 100% { transform: translateX(0); }
              10%, 30%, 50%, 70%, 90% { transform: translateX(-8px); }
              20%, 40%, 60%, 80% { transform: translateX(8px); }
            }
            .shake-animation {
              animation: shake 0.5s ease-in-out;
            }
          `,
        }}
      />
    </RadioGroup.Root>
  )
}

interface WrongAnswerFeedbackProps {
  message: string
}

/**
 * Inline feedback message shown briefly after wrong answer
 */
export function FlowchartWrongAnswerFeedback({ message }: WrongAnswerFeedbackProps) {
  return (
    <div
      data-testid="wrong-answer-feedback"
      className={css({
        paddingY: '3',
        paddingX: '4',
        backgroundColor: { base: 'amber.100', _dark: 'amber.900' },
        borderRadius: 'lg',
        border: '2px solid',
        borderColor: { base: 'amber.400', _dark: 'amber.600' },
        fontSize: 'md',
        color: { base: 'amber.800', _dark: 'amber.200' },
        textAlign: 'center',
        animation: 'fadeInOut 2s ease-in-out forwards',
      })}
    >
      {message}
      <style
        dangerouslySetInnerHTML={{
          __html: `
            @keyframes fadeInOut {
              0% { opacity: 0; transform: translateY(-10px); }
              15% { opacity: 1; transform: translateY(0); }
              85% { opacity: 1; transform: translateY(0); }
              100% { opacity: 0; transform: translateY(-10px); }
            }
          `,
        }}
      />
    </div>
  )
}