PricingChart.jsx 6.52 KB
Newer Older
1
// PricingChart.jsx
2
import React, { useState, useEffect, useRef } from 'react';
fisherdaddy's avatar
fisherdaddy committed
3
import { useScrollToTop } from '../hooks/useScrollToTop';
4 5
import '../styles/PricingChart.css';

6 7 8
const ChartLegend = ({ onLegendClick, highlightedBarTypes, showPricing = true }) => {
  if (!showPricing) return null;
  
fisherdaddy's avatar
fisherdaddy committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
  return (
    <div className="legend">
      <div
        className="legend-item"
        onClick={() => onLegendClick('input')}
        style={{ cursor: 'pointer', opacity: highlightedBarTypes.input ? 1 : 0.5 }}
      >
        <div className="legend-color input-color"></div>
        <span>Input Price</span>
      </div>
      <div
        className="legend-item"
        onClick={() => onLegendClick('output')}
        style={{ cursor: 'pointer', opacity: highlightedBarTypes.output ? 1 : 0.5 }}
      >
        <div className="legend-color output-color"></div>
        <span>Output Price</span>
      </div>
27
    </div>
fisherdaddy's avatar
fisherdaddy committed
28 29
  );
};
30

31
const ChartBar = ({ price, type, maxPrice, highlighted, score }) => {
32
  const getBarHeight = () => {
33 34 35
    if (score !== undefined) {
      return (score / maxPrice) * 200;
    }
36
    return (price / maxPrice) * 200;
37 38 39 40
  };

  return (
    <div
41
      className={`bar ${score !== undefined ? 'score-bar' : `${type}-bar`}`}
42 43 44 45 46
      style={{
        height: `${getBarHeight()}px`,
        opacity: highlighted ? 1 : 0.3,
      }}
    >
47
      <span className="price-label">{score !== undefined ? score : price}</span>
48 49 50 51
    </div>
  );
};

52
const ProviderColumn = ({ provider, maxPrice, highlightedBarTypes, showPricing = true }) => (
53 54
  <div className="chart-column">
    <div className="bars-container">
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
      {showPricing ? (
        <>
          <ChartBar
            price={provider.inputPrice}
            type="input"
            maxPrice={maxPrice}
            highlighted={highlightedBarTypes.input}
          />
          <ChartBar
            price={provider.outputPrice}
            type="output"
            maxPrice={maxPrice}
            highlighted={highlightedBarTypes.output}
          />
        </>
      ) : (
        <ChartBar
          score={provider.score}
          maxPrice={maxPrice}
          highlighted={true}
        />
      )}
77 78 79
    </div>
    <div className="provider-info">
      <img
80
        src={`${provider.logo}`}
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
        alt={`${provider.name} logo`}
        className="provider-logo"
      />
      <span className="provider-name">{provider.name}</span>
    </div>
  </div>
);

const YAxis = ({ maxPrice }) => {
  const numberOfTicks = 5;
  const tickValues = [];

  for (let i = 0; i <= numberOfTicks; i++) {
    const value = ((maxPrice / numberOfTicks) * i).toFixed(2);
    tickValues.push(value);
  }

  return (
    <div className="y-axis">
      {tickValues.reverse().map((value, index) => (
        <div key={index} className="y-axis-label">
          {value}
        </div>
      ))}
    </div>
  );
};

const GridLines = () => (
  <div className="grid-lines">
    {[...Array(5)].map((_, index) => (
      <div key={index} className="grid-line" style={{ bottom: `${(index / 4) * 100}%` }}></div>
    ))}
  </div>
);

117
const PricingChart = ({ data, showPricing = true }) => {
fisherdaddy's avatar
fisherdaddy committed
118
  useScrollToTop();
119 120 121 122
  const [highlightedBarTypes, setHighlightedBarTypes] = useState({
    input: true,
    output: true,
  });
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
  const chartAreaRef = useRef(null);
  const [hasScroll, setHasScroll] = useState(false);
  const [showScrollHint, setShowScrollHint] = useState(false);

  useEffect(() => {
    const checkScroll = () => {
      if (chartAreaRef.current) {
        const { scrollWidth, clientWidth, scrollLeft } = chartAreaRef.current;
        setHasScroll(scrollWidth > clientWidth);
        // 只在滚动到最左侧时显示提示
        setShowScrollHint(scrollWidth > clientWidth && scrollLeft === 0);
      }
    };

    const handleScroll = () => {
      if (chartAreaRef.current) {
        const { scrollLeft } = chartAreaRef.current;
        // 当用户开始滚动时隐藏提示
        if (scrollLeft > 0) {
          setShowScrollHint(false);
        }
      }
    };

    checkScroll();
    window.addEventListener('resize', checkScroll);
    if (chartAreaRef.current) {
      chartAreaRef.current.addEventListener('scroll', handleScroll);
    }

    return () => {
      window.removeEventListener('resize', checkScroll);
      if (chartAreaRef.current) {
        chartAreaRef.current.removeEventListener('scroll', handleScroll);
      }
    };
  }, [data]);

  const handleScrollHintClick = () => {
    if (chartAreaRef.current) {
      const { scrollWidth, clientWidth } = chartAreaRef.current;
      chartAreaRef.current.scrollTo({
        left: scrollWidth - clientWidth,
        behavior: 'smooth'
      });
      setShowScrollHint(false);
    }
  };
171 172 173 174 175 176 177 178 179

  const handleLegendClick = (barType) => {
    setHighlightedBarTypes((prevState) => ({
      ...prevState,
      [barType]: !prevState[barType],
    }));
  };

  const getMaxPrice = () => {
180 181 182
    if (!showPricing) {
      return Math.max(...data.providers.map(provider => provider.score));
    }
183 184 185 186 187 188 189 190 191 192 193 194 195 196
    const prices = data.providers.flatMap((provider) => [
      provider.inputPrice,
      provider.outputPrice,
    ]);
    return Math.max(...prices);
  };

  const maxPrice = getMaxPrice();

  return (
    <div className="pricing-chart">
      <h1 className="chart-title">{data.title}</h1>
      <h2 className="chart-subtitle">{data.subtitle}</h2>

197 198 199 200 201
      <ChartLegend 
        onLegendClick={handleLegendClick} 
        highlightedBarTypes={highlightedBarTypes}
        showPricing={showPricing}
      />
202

203
      <div className={`chart-area ${hasScroll ? 'has-scroll' : ''}`} ref={chartAreaRef}>
204 205 206 207 208 209 210 211 212
        <YAxis maxPrice={maxPrice} />
        <div className="chart-container">
          <GridLines />
          {data.providers.map((provider) => (
            <ProviderColumn
              key={provider.name}
              provider={provider}
              maxPrice={maxPrice}
              highlightedBarTypes={highlightedBarTypes}
213
              showPricing={showPricing}
214 215 216
            />
          ))}
        </div>
217 218 219 220 221 222 223 224 225 226 227 228 229
        {showScrollHint && (
          <div 
            className="scroll-hint-container"
            onClick={handleScrollHintClick}
            style={{ cursor: 'pointer' }}
          >
            <div className="scroll-hint">
              <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
                <path d="M9.29 15.88L13.17 12 9.29 8.12c-.39-.39-.39-1.02 0-1.41.39-.39 1.02-.39 1.41 0l4.59 4.59c.39.39.39 1.02 0 1.41l-4.59 4.59c-.39.39-1.02.39-1.41 0-.38-.39-.39-1.03 0-1.42z"/>
              </svg>
            </div>
          </div>
        )}
230 231 232 233 234 235
      </div>
    </div>
  );
};

export default PricingChart;