MethodologySection.tsx 8.29 KB
import { useEffect, useRef, useState } from 'react';
import type { TouchEvent } from 'react';
import type { Methodology, MethodologyStep } from '../types/home';

export interface MethodologySectionProps {
  methodology: Methodology;
  currentStep: number;
  onStepChange: (index: number) => void;
}

export function MethodologySection({
  methodology,
  currentStep,
  onStepChange,
}: MethodologySectionProps) {
  const touchStartXRef = useRef<number | null>(null);
  const previewTouchStartXRef = useRef<number | null>(null);
  const blockPreviewClickUntilRef = useRef(0);
  const [isImagePreviewOpen, setIsImagePreviewOpen] = useState(false);

  useEffect(() => {
    if (!isImagePreviewOpen) {
      return;
    }

    const previousBodyOverflow = document.body.style.overflow;
    const previousHtmlOverflow = document.documentElement.style.overflow;

    document.body.style.overflow = 'hidden';
    document.documentElement.style.overflow = 'hidden';

    return () => {
      document.body.style.overflow = previousBodyOverflow;
      document.documentElement.style.overflow = previousHtmlOverflow;
    };
  }, [isImagePreviewOpen]);

  if (!methodology.display_status || methodology.steps.length === 0) {
    return null;
  }

  const currentStepData: MethodologyStep | undefined = methodology.steps[currentStep];

  if (!currentStepData) {
    return null;
  }

  const lastStepIndex = methodology.steps.length - 1;

  const getLoopedStepIndex = (direction: 'next' | 'previous') => {
    if (direction === 'next') {
      return currentStep === lastStepIndex ? 0 : currentStep + 1;
    }

    return currentStep === 0 ? lastStepIndex : currentStep - 1;
  };

  const handleTouchStart = (event: TouchEvent<HTMLDivElement>) => {
    touchStartXRef.current = event.touches[0]?.clientX ?? null;
  };

  const handleTouchEnd = (event: TouchEvent<HTMLDivElement>) => {
    const startX = touchStartXRef.current;
    const endX = event.changedTouches[0]?.clientX;

    touchStartXRef.current = null;

    if (startX === null || typeof endX !== 'number') {
      return;
    }

    const deltaX = startX - endX;

    if (Math.abs(deltaX) < 40) {
      return;
    }

    blockPreviewClickUntilRef.current = Date.now() + 350;

    if (deltaX > 0) {
      onStepChange(getLoopedStepIndex('next'));
    }

    if (deltaX < 0) {
      onStepChange(getLoopedStepIndex('previous'));
    }
  };

  const handleTabClick = (index: number) => {
    onStepChange(index);
  };

  const handleImageClick = () => {
    if (Date.now() < blockPreviewClickUntilRef.current) {
      return;
    }

    setIsImagePreviewOpen(true);
  };

  const handlePreviewTouchStart = (event: TouchEvent<HTMLDivElement>) => {
    previewTouchStartXRef.current = event.touches[0]?.clientX ?? null;
  };

  const handlePreviewTouchEnd = (event: TouchEvent<HTMLDivElement>) => {
    const startX = previewTouchStartXRef.current;
    const endX = event.changedTouches[0]?.clientX;

    previewTouchStartXRef.current = null;

    if (startX === null || typeof endX !== 'number') {
      return;
    }

    const deltaX = startX - endX;

    if (Math.abs(deltaX) < 40) {
      return;
    }

    blockPreviewClickUntilRef.current = Date.now() + 350;

    if (deltaX > 0) {
      onStepChange(getLoopedStepIndex('next'));
    }

    if (deltaX < 0) {
      onStepChange(getLoopedStepIndex('previous'));
    }
  };

  return (
    <section className="relative overflow-hidden bg-surface-container-lowest py-20 tablet:py-24" data-testid="methodology-section">
      <div className="max-w-container-max mx-auto px-margin-mobile tablet:px-margin-desktop relative z-10">
        <div className="mb-12 tablet:mb-14 text-center">
          <h2 className="text-headline-md tablet:text-display-sm mb-3">{methodology.main_title}</h2>
          <p className="text-body-md tablet:text-body-lg text-on-surface-variant">{methodology.sub_title}</p>
        </div>

        <div className="flex flex-col">
          <div
            className="order-1 tablet:order-2 mt-0 mb-10 tablet:mt-8 tablet:mb-0"
            data-testid="methodology-tabs-wrapper"
          >
            <div
              data-testid="methodology-tabs"
              role="tablist"
              aria-label="方法论步骤"
              className="flex flex-nowrap tablet:flex-wrap justify-start tablet:justify-center gap-3 overflow-x-auto tablet:overflow-x-visible pb-1"
            >
              {methodology.steps.map((step, index) => {
                const isActive = index === currentStep;

                return (
                  <button
                    key={step.step_name}
                    type="button"
                    role="tab"
                    aria-selected={isActive}
                    data-testid={`step-${index}`}
                    onClick={() => handleTabClick(index)}
                    className={`inline-flex items-center gap-3 rounded-full px-5 py-3 text-label-lg font-semibold transition-all duration-200 shrink-0 ${
                      isActive
                        ? 'bg-primary text-white shadow-[0_12px_28px_rgba(18,87,255,0.2)]'
                        : 'bg-white text-primary border border-outline-variant/30 hover:border-primary/30 hover:bg-primary/5'
                    }`}
                  >
                    <span
                      className={`inline-flex h-8 w-8 items-center justify-center overflow-hidden rounded-full ${
                        isActive ? 'bg-white' : 'bg-primary/10'
                      }`}
                    >
                      {step.step_icon ? (
                        <img
                          src={step.step_icon}
                          alt=""
                          aria-hidden="true"
                          data-testid={`step-icon-${index}`}
                          className="h-full w-full rounded-full object-cover"
                        />
                      ) : (
                        <span
                          data-testid={`step-icon-${index}`}
                          className="text-sm font-bold leading-none text-primary"
                        >
                          {index + 1}
                        </span>
                      )}
                    </span>
                    <span>{step.step_name}</span>
                  </button>
                );
              })}
            </div>
          </div>

          <div
            className="order-2 tablet:order-1 mx-auto w-full max-w-[1080px]"
            data-testid="methodology-carousel"
            onTouchStart={handleTouchStart}
            onTouchEnd={handleTouchEnd}
          >
            <div className="aspect-video overflow-hidden rounded-[28px] border border-outline-variant/20 bg-[#F4F7FC] shadow-[0_18px_40px_rgba(22,38,74,0.08)]">
              <button
                type="button"
                data-testid="methodology-image-trigger"
                onClick={handleImageClick}
                className="block h-full w-full tablet:hidden"
                aria-label={`放大查看${currentStepData.step_name}`}
              >
                <img
                  src={currentStepData.step_image}
                  alt={currentStepData.step_name}
                  className="h-full w-full object-contain"
                  draggable={false}
                />
              </button>
              <img
                src={currentStepData.step_image}
                alt={currentStepData.step_name}
                className="hidden h-full w-full object-contain tablet:block"
                draggable={false}
              />
            </div>
          </div>
        </div>
      </div>

      {isImagePreviewOpen ? (
        <div
          data-testid="methodology-image-preview"
          className="fixed inset-0 z-50 flex items-center justify-center overflow-hidden bg-black/85 py-8 tablet:hidden"
          onClick={() => setIsImagePreviewOpen(false)}
          onTouchStart={handlePreviewTouchStart}
          onTouchEnd={handlePreviewTouchEnd}
        >
          <div
            className="w-screen max-w-none focus:outline-none"
            onClick={(event) => event.stopPropagation()}
          >
            <img
              src={currentStepData.step_image}
              alt={`${currentStepData.step_name}大图`}
              className="h-auto w-full rounded-[24px] object-contain"
              draggable={false}
            />
          </div>
        </div>
      ) : null}
    </section>
  );
}