Banner.tsx 5.09 KB
import { useEffect, useState } from 'react';
import type { BannerCapsuleTag } from '../types/home';

const LOCAL_BANNER_IMAGE = '/banner.png';
const LOCAL_BANNER_BLUR_IMAGE = '/banner-blur.png';
const LOCAL_BANNER_MOBILE_IMAGE = '/banner2.png';
const MOBILE_MEDIA_QUERY = '(max-width: 767px)';
const HOVER_MEDIA_QUERY = '(hover: hover) and (pointer: fine)';
const DEFAULT_CAPSULE_TEXT = '新一代智能协作平台';
const SITE_BASE_URL = '/gl-agent-position/';

type BannerDisplayMode = {
  isMobile: boolean;
  canHoverBlur: boolean;
};

export function resolveLocalAssetPath(assetPath: string, base = SITE_BASE_URL) {
  const normalizedBase = base.endsWith('/') ? base : `${base}/`;
  const normalizedAssetPath = assetPath.startsWith('/') ? assetPath.slice(1) : assetPath;

  return `${normalizedBase}${normalizedAssetPath}`;
}

function getBannerDisplayMode(): BannerDisplayMode {
  if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') {
    return {
      isMobile: false,
      canHoverBlur: true,
    };
  }

  const isMobile = window.matchMedia(MOBILE_MEDIA_QUERY).matches;
  const canHoverBlur = !isMobile && window.matchMedia(HOVER_MEDIA_QUERY).matches;

  return {
    isMobile,
    canHoverBlur,
  };
}

export interface BannerProps {
  title: string;
  subtitle: string;
  backgroundImage: string | null;
  backgroundBlurImage: string | null;
  capsuleTag: BannerCapsuleTag | null;
}

export function Banner({
  title,
  subtitle,
  backgroundImage,
  backgroundBlurImage,
  capsuleTag,
}: BannerProps) {
  const [isHovering, setIsHovering] = useState(false);
  const [imageError, setImageError] = useState(false);
  const [displayMode, setDisplayMode] = useState<BannerDisplayMode>(() => getBannerDisplayMode());
  void backgroundImage;
  void backgroundBlurImage;

  useEffect(() => {
    if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') {
      return;
    }

    const mobileMedia = window.matchMedia(MOBILE_MEDIA_QUERY);
    const hoverMedia = window.matchMedia(HOVER_MEDIA_QUERY);
    const updateDisplayMode = () => {
      setDisplayMode(getBannerDisplayMode());
    };

    updateDisplayMode();
    mobileMedia.addEventListener('change', updateDisplayMode);
    hoverMedia.addEventListener('change', updateDisplayMode);

    return () => {
      mobileMedia.removeEventListener('change', updateDisplayMode);
      hoverMedia.removeEventListener('change', updateDisplayMode);
    };
  }, []);

  const shouldShowBlur = displayMode.canHoverBlur && isHovering;
  const effectiveBackground = imageError
    ? null
    : displayMode.isMobile
      ? resolveLocalAssetPath(LOCAL_BANNER_MOBILE_IMAGE)
      : shouldShowBlur
      ? resolveLocalAssetPath(LOCAL_BANNER_BLUR_IMAGE)
      : resolveLocalAssetPath(LOCAL_BANNER_IMAGE);
  const capsuleText = capsuleTag?.text?.trim() || DEFAULT_CAPSULE_TEXT;

  const handleMouseEnter = () => {
    if (displayMode.canHoverBlur) {
      setIsHovering(true);
    }
  };

  const handleMouseLeave = () => {
    setIsHovering(false);
  };

  const handleImageError = () => {
    setImageError(true);
  };

  return (
    <section
      data-testid="banner-section"
      className="relative min-h-[500px] tablet:min-h-[582px] flex items-center justify-center overflow-hidden pt-16 pb-20 tablet:pt-24 tablet:pb-28 px-margin-mobile tablet:px-margin-desktop"
      onMouseEnter={handleMouseEnter}
      onMouseLeave={handleMouseLeave}
    >
      <div className="absolute inset-0 z-0">
        {effectiveBackground ? (
          <>
            <img
              src={effectiveBackground}
              alt="Banner 背景"
              className="w-full h-full object-cover"
              onError={handleImageError}
            />
            <div className="absolute inset-0 bg-white/20 tablet:bg-white/16" />
          </>
        ) : (
          <div className="absolute inset-0 bg-gradient-to-br from-primary/5 to-surface" />
        )}
      </div>

      <div className="relative z-10 text-center max-w-container-max mx-auto">
        <div className="inline-flex items-center gap-3 px-8 py-3 rounded-full bg-[#E8EFFD] border border-[#AFC4EF] mb-6 tablet:mb-8 shadow-[0_0_0_1px_rgba(255,255,255,0.35)_inset]">
          <svg className="w-7 h-7 text-[#2A5AA6] shrink-0" viewBox="0 0 32 32" fill="currentColor" aria-hidden="true">
            <path d="M14 2.5L16.1 8.4L22 10.5L16.1 12.6L14 18.5L11.9 12.6L6 10.5L11.9 8.4L14 2.5Z" />
            <path d="M24.5 10L25.8 13.5L29.3 14.8L25.8 16.1L24.5 19.6L23.2 16.1L19.7 14.8L23.2 13.5L24.5 10Z" />
          </svg>
          <span className="text-[18px] leading-none text-[#1E4F8B] font-bold tracking-[0.02em]">{capsuleText}</span>
        </div>

        <h1 className="text-display-lg-mobile tablet:text-display-lg font-bold mb-4 tablet:mb-6 leading-tight text-on-surface">
          {title.split(' ').map((word, index) => (
            index === 1 ? <span key={index} className="text-primary">{word} </span> : word + ' '
          ))}
        </h1>

        <p className="text-body-md tablet:text-body-lg text-on-surface-variant mb-8 tablet:mb-10 max-w-2xl mx-auto font-medium">
          {subtitle}
        </p>
      </div>
    </section>
  );
}