# All files / src/components/analytics SteamMoveAlert.tsx

0% Statements 0/211
100% Branches 1/1
100% Functions 1/1
0% Lines 0/211

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

     /**
     * SteamMoveAlert Component
     * Displays live steam moves in a dashboard banner/widget
     * Shows most recent steam moves with game context and movement details
     */

    import React, { useEffect, useState } from 'react';
    import { useDispatch, useSelector } from 'react-redux';
    import { AppDispatch, RootState } from '../../store';
    import { fetchSteamMoves, selectSteamMoves, selectMovementLoading } from '../../store/movementSlice';
    import { LineMovement } from '../../types/movements.types';
    import { useDarkMode } from '../../contexts/DarkModeContext';
    import { useNavigate } from 'react-router-dom';

    interface SteamMoveAlertProps {
      limit?: number;
      compact?: boolean;
      refreshInterval?: number;
    }

    export default function SteamMoveAlert({
      limit = 10,
      compact = false,
      refreshInterval = 60000 // 1 minute default
    }: SteamMoveAlertProps) {
      const dispatch = useDispatch<AppDispatch>();
      const navigate = useNavigate();
      const { isDarkMode } = useDarkMode();
      const movements = useSelector(selectSteamMoves);
      const loading = useSelector(selectMovementLoading);
      const [autoRefresh, setAutoRefresh] = useState(true);

      // Fetch steam moves on mount
      useEffect(() => {
        dispatch(fetchSteamMoves(limit));
      }, [dispatch, limit]);

      // Auto-refresh logic
      useEffect(() => {
        if (!autoRefresh) return;

        const interval = setInterval(() => {
          dispatch(fetchSteamMoves(limit));
        }, refreshInterval);

        return () => clearInterval(interval);
      }, [dispatch, limit, refreshInterval, autoRefresh]);

      const getSeverityColor = (bookmakerCount: number, avgMovement: number): string => {
        if (bookmakerCount >= 5 && avgMovement >= 2.5) {
          return isDarkMode ? 'bg-red-900/30 border-red-600' : 'bg-red-100 border-red-400';
        }
        if (bookmakerCount >= 4 && avgMovement >= 2.0) {
          return isDarkMode ? 'bg-orange-900/30 border-orange-600' : 'bg-orange-100 border-orange-400';
        }
        return isDarkMode ? 'bg-amber-900/30 border-amber-600' : 'bg-amber-100 border-amber-400';
      };

      const getSeverityBadge = (bookmakerCount: number, avgMovement: number): string => {
        if (bookmakerCount >= 5 && avgMovement >= 2.5) return 'CRITICAL';
        if (bookmakerCount >= 4 && avgMovement >= 2.0) return 'HIGH';
        return 'MEDIUM';
      };

      const formatTime = (dateString: string): string => {
        const date = new Date(dateString);
        const now = new Date();
        const seconds = Math.floor((now.getTime() - date.getTime()) / 1000);

        if (seconds < 60) return `${seconds}s ago`;
        if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`;
        if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ago`;
        return date.toLocaleDateString();
      };

      const marketLabel = (market: string): string => {
        return { h2h: 'Moneyline', spreads: 'Spreads', totals: 'Totals' }[market] || market;
      };

      if (compact && movements.length === 0) {
        return null;
      }

      return (
        <div
          className={`${
            isDarkMode ? 'bg-gray-800 border-gray-700' : 'bg-white border-gray-200'
          } rounded-lg border shadow-sm overflow-hidden`}
        >
          {/* Header */}
          <div
            className={`px-4 py-3 flex items-center justify-between border-b ${
              isDarkMode ? 'border-gray-700 bg-gray-750' : 'border-gray-200 bg-gray-50'
            }`}
          >
            <div className="flex items-center gap-2">
              <span className="text-2xl">🔥</span>
              <h3 className={`font-semibold ${isDarkMode ? 'text-white' : 'text-gray-900'}`}>
                Live Steam Moves
              </h3>
              {movements.length > 0 && (
                <span
                  className={`px-2 py-1 rounded text-xs font-medium ${
                    isDarkMode ? 'bg-red-900/50 text-red-200' : 'bg-red-100 text-red-800'
                  }`}
                >
                  {movements.length} detected
                </span>
              )}
            </div>
            <button
              onClick={() => setAutoRefresh(!autoRefresh)}
              className={`px-2 py-1 rounded text-xs font-medium transition ${
                autoRefresh
                  ? isDarkMode
                    ? 'bg-green-900/50 text-green-200'
                    : 'bg-green-100 text-green-800'
                  : isDarkMode
                  ? 'bg-gray-700 text-gray-300'
                  : 'bg-gray-200 text-gray-600'
              }`}
            >
              {autoRefresh ? '⏱ Live' : '⏸ Paused'}
            </button>
          </div>

          {/* Content */}
          <div className="max-h-96 overflow-y-auto">
            {loading && movements.length === 0 ? (
              <div className="px-4 py-8 text-center">
                <div className="inline-block animate-spin">⌛</div>
                <p className={`mt-2 ${isDarkMode ? 'text-gray-400' : 'text-gray-600'}`}>
                  Loading steam moves...
                </p>
              </div>
            ) : movements.length === 0 ? (
              <div className="px-4 py-8 text-center">
                <p className={`${isDarkMode ? 'text-gray-400' : 'text-gray-600'}`}>
                  No steam moves detected in the last 24 hours
                </p>
              </div>
            ) : (
              <div className="divide-y divide-gray-200 dark:divide-gray-700">
                {movements.map((movement: LineMovement) => (
                  <div
                    key={movement.id}
                    className={`p-4 border-l-4 cursor-pointer transition hover:${
                      isDarkMode ? 'bg-gray-750' : 'bg-gray-50'
                    } ${getSeverityColor(
                      movement.bookmakerCount,
                      typeof movement.averageMovement === 'string' ? parseFloat(movement.averageMovement) : movement.averageMovement
                    )}`}
                    onClick={() => {
                      if (movement.game?.id) {
                        navigate(`/game/${movement.game.id}`);
                      }
                    }}
                  >
                    {/* Matchup and severity */}
                    <div className="flex items-center justify-between mb-2">
                      <div className="flex-1">
                        <p className={`font-semibold ${isDarkMode ? 'text-white' : 'text-gray-900'}`}>
                          {movement.game?.homeTeamName || 'Unknown'} vs{' '}
                          {movement.game?.awayTeamName || 'Unknown'}
                        </p>
                        <p className={`text-xs ${isDarkMode ? 'text-gray-400' : 'text-gray-600'}`}>
                          {movement.game?.commenceTime
                            ? new Date(movement.game.commenceTime).toLocaleString()
                            : 'N/A'}
                        </p>
                      </div>
                      <div className="text-right">
                        <span
                          className={`inline-block px-2 py-1 rounded text-xs font-bold ${
                            getSeverityBadge(
                              movement.bookmakerCount,
                              typeof movement.averageMovement === 'string' ? parseFloat(movement.averageMovement) : movement.averageMovement
                            ) === 'CRITICAL'
                              ? isDarkMode
                                ? 'bg-red-600 text-white'
                                : 'bg-red-600 text-white'
                              : getSeverityBadge(
                                  movement.bookmakerCount,
                                  typeof movement.averageMovement === 'string' ? parseFloat(movement.averageMovement) : movement.averageMovement
                                ) === 'HIGH'
                              ? isDarkMode
                                ? 'bg-orange-600 text-white'
                                : 'bg-orange-600 text-white'
                              : isDarkMode
                              ? 'bg-amber-600 text-white'
                              : 'bg-amber-600 text-white'
                          }`}
                        >
                          {getSeverityBadge(
                            movement.bookmakerCount,
                            typeof movement.averageMovement === 'string' ? parseFloat(movement.averageMovement) : movement.averageMovement
                          )}
                        </span>
                      </div>
                    </div>

                    {/* Movement details */}
                    <div className="grid grid-cols-3 gap-3 text-sm mb-2">
                      <div>
                        <p className={`text-xs font-medium ${isDarkMode ? 'text-gray-400' : 'text-gray-600'}`}>
                          Market
                        </p>
                        <p className={`font-semibold ${isDarkMode ? 'text-white' : 'text-gray-900'}`}>
                          {marketLabel(movement.marketType)}
                        </p>
                      </div>
                      <div>
                        <p className={`text-xs font-medium ${isDarkMode ? 'text-gray-400' : 'text-gray-600'}`}>
                          Avg Movement
                        </p>
                        <p className={`font-semibold ${isDarkMode ? 'text-white' : 'text-gray-900'}`}>
                          {(typeof movement.averageMovement === 'string' ? parseFloat(movement.averageMovement) : movement.averageMovement).toFixed(2)} pts
                        </p>
                      </div>
                      <div>
                        <p className={`text-xs font-medium ${isDarkMode ? 'text-gray-400' : 'text-gray-600'}`}>
                          Books
                        </p>
                        <p className={`font-semibold ${isDarkMode ? 'text-white' : 'text-gray-900'}`}>
                          {movement.bookmakerCount} moved
                        </p>
                      </div>
                    </div>

                    {/* Footer */}
                    <div className="flex items-center justify-between text-xs">
                      <span className={isDarkMode ? 'text-gray-400' : 'text-gray-600'}>
                        {movement.timeToMove}s to move
                      </span>
                      <span className={isDarkMode ? 'text-gray-500' : 'text-gray-500'}>
                        {formatTime(movement.detectedAt)}
                      </span>
                    </div>

                    {movement.suspectedCause && (
                      <p className={`text-xs mt-2 italic ${isDarkMode ? 'text-gray-400' : 'text-gray-600'}`}>
                        Cause: {movement.suspectedCause}
                      </p>
                    )}
                  </div>
                ))}
              </div>
            )}
          </div>

          {/* Footer action */}
          {movements.length > 0 && !compact && (
            <div
              className={`px-4 py-3 border-t ${isDarkMode ? 'border-gray-700 bg-gray-750' : 'border-gray-200 bg-gray-50'}`}
            >
              <button
                onClick={() => navigate('/analytics/movements')}
                className={`w-full px-3 py-2 rounded font-medium text-sm transition ${
                  isDarkMode
                    ? 'bg-blue-600 hover:bg-blue-700 text-white'
                    : 'bg-blue-600 hover:bg-blue-700 text-white'
                }`}
              >
                View All Movements →
              </button>
            </div>
          )}
        </div>
      );
    }