ListingDetailOverview.tsx 5.14 KB
import { CloseOutlined } from '@ant-design/icons';
import { UButton } from '@uj/ui';
import type { StoreListingDetail } from '@/api';
import { STORE_STATUS, type StoreStatus } from '@/config/storeStatus';
import type { StoreDictionaries } from './dictionaries';
import { dictionaryLabel } from './formatters';
import './listingDetail.css';

const EMPTY = '—';

function categoryCount(detail: StoreListingDetail): string {
  if (
    typeof detail.categoryCount === 'number' &&
    Number.isSafeInteger(detail.categoryCount) &&
    detail.categoryCount >= 0
  ) {
    return `${detail.categoryCount} 个`;
  }
  if (!detail.items) return EMPTY;
  const categories = detail.items
    .map((item) => item.productCategory?.trim())
    .filter((category): category is string => !!category);
  return `${new Set(categories).size} 个`;
}

function currentVersion(detail: StoreListingDetail): string {
  const versions = (detail.changeHistory || [])
    .map((change) => change.versionNo)
    .filter(
      (version): version is number =>
        typeof version === 'number' && Number.isSafeInteger(version) && version > 0,
    );
  return versions.length
    ? `v${versions.reduce((latest, version) => Math.max(latest, version))}`
    : EMPTY;
}

/** Format the API's calendar fields as text, preserving its original time zone. */
function effectiveDate(detail: StoreListingDetail, includeTime = false): string {
  const value = detail.effectiveTime?.trim() || detail.approveTime?.trim();
  const match = value?.match(/^(\d{4}-\d{2}-\d{2})(?:[T ](\d{2}:\d{2}))?/);
  if (!match) return EMPTY;
  return includeTime && match[2] ? `${match[1]} ${match[2]}` : match[1];
}

function ListingStatus({ detail }: { detail: StoreListingDetail }) {
  const knownStatus = Object.hasOwn(STORE_STATUS, detail.status || '')
    ? STORE_STATUS[detail.status as StoreStatus]
    : undefined;
  const tone =
    detail.status === 'APPROVED'
      ? 'approved'
      : detail.status === 'PENDING'
        ? 'pending'
        : detail.status === 'REJECTED'
          ? 'rejected'
          : 'neutral';

  return (
    <span className={`listing-detail-status listing-detail-status--${tone}`}>
      <span className="listing-detail-status__dot" aria-hidden="true" />
      {detail.statusLabel?.trim() || knownStatus?.label || EMPTY}
    </span>
  );
}

export function ListingDetailHeader({
  detail,
  spec,
  onClose,
}: {
  detail: StoreListingDetail;
  spec?: StoreDictionaries;
  onClose: () => void;
}) {
  return (
    <section className="listing-detail-header" aria-label="门店概览">
      <div className="listing-detail-header__top">
        <h2>{detail.storeName?.trim() || EMPTY}</h2>
        <div className="listing-detail-header__actions">
          <ListingStatus detail={detail} />
          <UButton
            type="text"
            className="listing-detail-header__close"
            aria-label="关闭"
            icon={<CloseOutlined />}
            onClick={onClose}
          />
        </div>
      </div>
      <dl className="listing-detail-meta">
        <div>
          <dt>商户号</dt>
          <dd>{detail.merchantNo?.trim() || EMPTY}</dd>
        </div>
        <div>
          <dt>业务场景</dt>
          <dd>{dictionaryLabel(spec?.businessScenes, detail.businessScene?.trim())}</dd>
        </div>
        <div>
          <dt>产品分类</dt>
          <dd>{categoryCount(detail)}</dd>
        </div>
        <div>
          <dt>上架时间</dt>
          <dd>{effectiveDate(detail)}</dd>
        </div>
        <div>
          <dt>当前版本</dt>
          <dd>{currentVersion(detail)}</dd>
        </div>
      </dl>
    </section>
  );
}

export function ListingStoreInfo({
  detail,
  spec,
}: {
  detail: StoreListingDetail;
  spec?: StoreDictionaries;
}) {
  const scene = detail.businessScene?.trim();

  return (
    <section className="listing-store-info" aria-label="门店信息">
      <h3 className="listing-store-info__title">门店信息</h3>
      <dl className="listing-store-info__fields">
        <div>
          <dt>门店名称</dt>
          <dd>{detail.storeName?.trim() || EMPTY}</dd>
        </div>
        <div>
          <dt>门店商户号</dt>
          <dd>{detail.merchantNo?.trim() || EMPTY}</dd>
        </div>
        <div>
          <dt>业务场景</dt>
          <dd>
            {scene ? (
              <span className="listing-store-info__scene">
                {dictionaryLabel(spec?.businessScenes, scene)}
              </span>
            ) : (
              EMPTY
            )}
            <span className="listing-store-info__hint">
              同一门店的每个业务场景均覆盖全部产品分类
            </span>
          </dd>
        </div>
        <div>
          <dt>产品分类数</dt>
          <dd>
            <span>{categoryCount(detail)}</span>
            <span className="listing-store-info__hint">完整清单见「产品与场景」页签</span>
          </dd>
        </div>
        <div>
          <dt>上架状态</dt>
          <dd>
            <ListingStatus detail={detail} />
          </dd>
        </div>
        <div>
          <dt>生效时间</dt>
          <dd>{effectiveDate(detail, true)}</dd>
        </div>
      </dl>
    </section>
  );
}