# All files / src/components/odds DisagreementBreakdown.tsx

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

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

     /**
     * DisagreementBreakdown - Modal showing full consensus detail for a game.
     *
     * Displays:
     *  - All bookmaker lines per market type
     *  - Consensus line and standard deviation
     *  - Outlier bookmakers highlighted
     *  - Best value indicator
     */
    import React, { useEffect, useState } from 'react';
    import api from '../../services/api';
    import {
      ConsensusResult,
      HighDisagreementGame,
      getDisagreementBadgeColor,
      getDisagreementCategory,
    } from '../../types/disagreement.types';
    import { useDarkMode } from '../../contexts/DarkModeContext';

    interface Props {
      game: HighDisagreementGame | null;
      onClose: () => void;
    }

    const MARKET_LABELS: Record<string, string> = {
      h2h: 'Moneyline',
      spreads: 'Spread',
      totals: 'Total (O/U)',
    };

    function formatLine(marketType: string, line: number): string {
      if (marketType === 'h2h') {
        return line >= 0 ? `+${line}` : `${line}`;
      }
      if (marketType === 'spreads') {
        return line >= 0 ? `+${line.toFixed(1)}` : `${line.toFixed(1)}`;
      }
      return line.toFixed(1);
    }

    export default function DisagreementBreakdown({ game, onClose }: Props) {
      const { isDarkMode } = useDarkMode();
      const [consensus, setConsensus] = useState<ConsensusResult[]>([]);
      const [loading, setLoading] = useState(false);
      const [error, setError] = useState<string | null>(null);

      useEffect(() => {
        if (!game) return;

        // Always fetch the full breakdown from the dedicated endpoint so all
        // markets for this game are shown, regardless of which markets passed the
        // threshold filter on the live list. Using the pre-loaded game.consensus
        // would silently omit any market whose score is below the current filter.
        setLoading(true);
        setError(null);
        api
          .get(`/analytics/disagreement/game/${game.gameId}`)
          .then((res) => {
            setConsensus(res.data.data ?? []);
          })
          .catch((err) => {
            setError(err.response?.data?.error ?? 'Failed to load breakdown');
          })
          .finally(() => setLoading(false));
      }, [game]);

      if (!game) return null;

      const overlayClass = 'fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4';
      const panelClass = `relative w-full max-w-2xl max-h-[90vh] overflow-y-auto rounded-2xl shadow-xl ${
        isDarkMode ? 'bg-gray-800 text-white' : 'bg-white text-gray-900'
      }`;

      return (
        <div className={overlayClass} onClick={onClose}>
          <div className={panelClass} onClick={(e) => e.stopPropagation()}>
            {/* Header */}
            <div className={`sticky top-0 px-6 py-4 border-b ${isDarkMode ? 'border-gray-700 bg-gray-800' : 'border-gray-100 bg-white'}`}>
              <button
                onClick={onClose}
                className="absolute right-4 top-4 text-gray-400 hover:text-gray-600 text-xl leading-none"
                aria-label="Close"
              >
                ✕
              </button>
              <h2 className="text-lg font-bold pr-8">
                {game.awayTeamName} <span className="font-normal text-sm">@</span> {game.homeTeamName}
              </h2>
              <p className={`text-xs mt-1 ${isDarkMode ? 'text-gray-400' : 'text-gray-500'}`}>
                {new Date(game.commenceTime).toLocaleString(undefined, {
                  weekday: 'short',
                  month: 'short',
                  day: 'numeric',
                  hour: 'numeric',
                  minute: '2-digit',
                })}
                {' · '}
                <span className="uppercase tracking-wide">{game.sportKey}</span>
              </p>
            </div>

            {/* Body */}
            <div className="px-6 py-4 space-y-6">
              {loading && (
                <div className="space-y-3">
                  {[...Array(3)].map((_, i) => (
                    <div
                      key={i}
                      className={`h-20 rounded-lg animate-pulse ${isDarkMode ? 'bg-gray-700' : 'bg-gray-100'}`}
                    />
                  ))}
                </div>
              )}

              {error && <p className="text-red-500 text-sm">{error}</p>}

              {!loading &&
                !error &&
                consensus.map((c) => {
                  const badgeColor = getDisagreementBadgeColor(c.disagreementScore);
                  const category = getDisagreementCategory(c.disagreementScore);
                  const outlierKeys = new Set(c.outlierBookmakers.map((o) => o.bookmaker));

                  return (
                    <div
                      key={c.marketType}
                      className={`rounded-xl border p-4 ${isDarkMode ? 'border-gray-700' : 'border-gray-200'}`}
                    >
                      {/* Market header */}
                      <div className="flex items-center justify-between mb-3">
                        <h3 className="font-semibold text-sm">
                          {MARKET_LABELS[c.marketType] ?? c.marketType}
                        </h3>
                        <div className="flex items-center gap-2">
                          <span className={`text-xs ${isDarkMode ? 'text-gray-400' : 'text-gray-500'}`}>
                            {c.bookmakerCount} books · σ {c.standardDeviation}
                          </span>
                          <span className={`rounded-full px-2 py-0.5 text-xs font-medium ${badgeColor}`}>
                            {c.disagreementScore} · {category}
                          </span>
                        </div>
                      </div>

                      {/* Consensus line */}
                      <div className={`flex items-center gap-2 mb-3 text-sm ${isDarkMode ? 'text-gray-300' : 'text-gray-700'}`}>
                        <span className="font-medium">Consensus:</span>
                        <span>{formatLine(c.marketType, c.consensusLine)}</span>
                      </div>

                      {/* Outliers */}
                      {c.outlierBookmakers.length > 0 && (
                        <div className="mb-3">
                          <p className="text-xs font-medium text-orange-500 mb-1">⚠ Outliers</p>
                          <div className="flex flex-wrap gap-1.5">
                            {c.outlierBookmakers.map((o) => (
                              <span
                                key={o.bookmaker}
                                className="rounded-full bg-orange-100 text-orange-800 dark:bg-orange-900/30 dark:text-orange-400 px-2 py-0.5 text-xs"
                              >
                                {o.bookmaker}: {formatLine(c.marketType, o.line)} ({o.deviation > 0 ? '+' : ''}{o.deviation}σ)
                              </span>
                            ))}
                          </div>
                        </div>
                      )}

                      {/* Best value */}
                      <div className={`flex items-center gap-2 rounded-lg px-3 py-2 text-xs ${isDarkMode ? 'bg-green-900/20 text-green-400' : 'bg-green-50 text-green-800'}`}>
                        <span>✅</span>
                        <span>
                          <span className="font-medium">Best value:</span>{' '}
                          {c.bestValue.side.toUpperCase()} at{' '}
                          <span className="font-semibold">{c.bestValue.bookmaker}</span>{' '}
                          ({formatLine(c.marketType, c.bestValue.line)}, {(c.bestValue.impliedProb * 100).toFixed(1)}% implied)
                        </span>
                      </div>
                    </div>
                  );
                })}

              {!loading && !error && consensus.length === 0 && (
                <p className={`text-sm ${isDarkMode ? 'text-gray-400' : 'text-gray-500'}`}>
                  No consensus data available for this game yet.
                </p>
              )}
            </div>
          </div>
        </div>
      );
    }