PositionAbilityArea.tsx 5.18 KB
import { useState } from 'react';
import type { PositionTab, Agent, MediaType } from '../types/home';
import { AgentCard } from './AgentCard';
import { QrcodeModal } from './QrcodeModal';

export interface PositionAbilityAreaProps {
  tab: PositionTab;
  currentAgent: Agent | undefined;
  currentAgentIndex: number;
  onAgentSelect: (index: number) => void;
  onOpenQrcodeModal: () => void;
  qrcodeModalOpen: boolean;
  onCloseQrcodeModal: () => void;
}

export function PositionAbilityArea({
  tab,
  currentAgent,
  currentAgentIndex,
  onAgentSelect,
  onOpenQrcodeModal,
  qrcodeModalOpen,
  onCloseQrcodeModal,
}: PositionAbilityAreaProps) {
  const [mediaError, setMediaError] = useState(false);
  const shouldScrollAgentList = tab.agents.length > 4;
  const [titlePrefix = '', ...titleSuffixParts] = tab.main_title.split('—');
  const titleSuffix = titleSuffixParts.join('—').trim();

  const handleMediaError = () => {
    setMediaError(true);
  };

  const renderMedia = (mediaUrl: string | null, mediaType: MediaType | null) => {
    if (!mediaUrl || !mediaType || mediaError) {
      return (
        <div className="w-full h-full bg-surface-container flex items-center justify-center">
          <span className="text-on-surface-variant">素材不可用</span>
        </div>
      );
    }

    if (mediaType === 'mp4' || mediaType === 'mov') {
      return (
        <video
          src={mediaUrl}
          data-testid="agent-video"
          className="w-full h-full object-contain bg-surface-container"
          autoPlay
          loop
          muted
          playsInline
          onError={handleMediaError}
        />
      );
    }

    return (
      <img
        src={mediaUrl}
        data-testid="agent-image"
        alt="Agent 素材"
        className="w-full h-full object-contain"
        onError={handleMediaError}
      />
    );
  };

  return (
    <section className="pt-stack-md tablet:pt-stack-lg pb-20 bg-surface" data-testid="position-ability-area">
      <div className="max-w-container-max mx-auto px-margin-mobile tablet:px-margin-desktop">
        <div className="grid grid-cols-1 tablet:grid-cols-[minmax(0,1.35fr)_minmax(320px,1fr)] gap-8 tablet:gap-10 items-start">
          <div
            data-testid="left-mockup"
            className="overflow-hidden rounded-[28px] shadow-[0_20px_50px_rgba(27,87,181,0.12)] bg-white"
          >
            <div
              data-testid="agent-media-frame"
              className="aspect-[1.04/0.82] w-full overflow-hidden rounded-[28px] bg-gradient-to-br from-[#5ea0f0] via-[#94c0ff] to-[#d5e6ff]"
            >
              <div className="h-full w-full overflow-hidden rounded-[28px] bg-surface-container relative">
                {currentAgent ? (
                  renderMedia(currentAgent.media_url, currentAgent.media_type)
                ) : (
                  <div className="w-full h-full bg-surface-container flex items-center justify-center">
                    <span className="text-on-surface-variant">暂无 Agent 素材</span>
                  </div>
                )}
              </div>
            </div>
          </div>

          <div className="space-y-6 tablet:pt-1">
            <div className="inline-flex items-start gap-3">
              <span className="mt-2 h-10 w-1 rounded-full bg-primary shrink-0" />
              <div>
                <p className="mb-1 text-label-sm font-medium text-primary">核心能力</p>
                <div className="text-headline-md leading-tight">
                  <span>{titlePrefix.trim()}</span>
                  {titleSuffix ? (
                    <>
                      {' '}
                      <span className="text-primary">— {titleSuffix}</span>
                    </>
                  ) : null}
                </div>
                <p className="mt-3 text-body-md text-on-surface-variant leading-relaxed">
                  {tab.sub_title}
                </p>
              </div>
            </div>

            {tab.agents.length > 0 && (
              <div
                data-testid="agent-card-list"
                className={`space-y-2.5 ${shouldScrollAgentList ? 'max-h-[370px] overflow-y-auto pr-1.5' : ''}`}
              >
                {tab.agents.map((agent, index) => (
                  <AgentCard
                    key={agent.agent_id}
                    agent={agent}
                    isSelected={index === currentAgentIndex}
                    onClick={() => onAgentSelect(index)}
                  />
                ))}
              </div>
            )}
          </div>
        </div>

        <div className="mt-12 flex justify-center">
          <button
            onClick={onOpenQrcodeModal}
            className="inline-flex items-center justify-center gap-2 rounded-full bg-primary px-12 py-4 text-white text-[22px] leading-none shadow-[0_12px_28px_rgba(0,84,220,0.24)] transition-transform active:scale-95"
          >
            立即体验 <span className="text-[22px]">↗</span>
          </button>
        </div>
      </div>

      <QrcodeModal
        isOpen={qrcodeModalOpen}
        onClose={onCloseQrcodeModal}
        positionName={tab.position_name}
        qrcodeUrl={tab.qrcode_url}
        qrcodeStatus={tab.qrcode_status}
      />
    </section>
  );
}