SpotlightEntry.tsx 4.95 KB
import { Tooltip } from 'antd';
import { useHeatmapStore, type SpotlightKind } from '@/store/useHeatmapStore';
import { placeNameOf } from '@/utils/format';
import {
  buildSpotlight,
  SPOTLIGHT_TOP_N,
  spotlightBlockReason,
  spotlightEntryLabel,
  spotlightEntryTip,
} from './spotlightView';

/**
 * 「在地图上看」那颗入口:把当前这张榜的前几名铺到地图上。
 *
 * 面板的分类标题上有一颗、「查看全部」抽屉的标题右侧也有一颗,两处共用这一个组件,
 * 因为三件事必须一模一样:能不能铺开(spotlightBlockReason)、怎么说清这一屏的画法
 * (spotlightEntryTip)、点开的是哪几档(buildSpotlight)。各写一遍迟早会出现
 * 「面板上点得开、抽屉里同一张榜点不开」这种半截状态。
 *
 * 按钮文字带着这一屏真铺得出的档数(spotlightEntryLabel):抽屉里那份榜有几百行,
 * 只写「在地图上看」会被读成「把整张榜摊在地图上」,而屏上只有前几名。
 *
 * 铺不开时按钮禁用、悬浮说明写清原因,而不是点了没反应:这里三种「铺不开」是三件不同的事
 * —— 数据没到、地址读不出、这一筛就没记录 —— 只有写出来人才知道下一步该做什么。
 */
export function SpotlightEntry({
  kind,
  variant = 'section',
}: {
  kind: SpotlightKind;
  /** 'section':面板分类标题行右端;'board':榜单抽屉标题右侧 */
  variant?: 'section' | 'board';
}) {
  const stats = useHeatmapStore((s) => s.stats);
  const meta = useHeatmapStore((s) => s.meta);
  const openSpotlight = useHeatmapStore((s) => s.openSpotlight);
  const setMapScope = useHeatmapStore((s) => s.setMapScope);

  const reason = spotlightBlockReason(kind, stats, meta);
  /**
   * 这一刀收窄叫什么 —— 名字要具体到点上,不能只写「前 6」。
   *
   * 这句话会被三处念出来:地图上那块收窄名牌、明细抽屉的「只看…」、以及本面板。
   * 只写「高频街道 · 前 6」时,地图上那一圈范围框与名牌等于白画 —— 读的人还是不知道
   * 眼前这片热力属于粤海还是南头。所以把真点到的名字写进去(最多三个,其余折算成条数),
   * 名牌上再配一个「×」取消,整件事就闭合了。
   */
  const scopeLabel = (prefix: string, names: string[], unit: string) => {
    const shown = names.filter(Boolean).slice(0, 3).join('、');
    if (!shown) return `${prefix} · ${names.length} ${unit}`;
    return names.length > 3
      ? `${prefix} · ${shown} 等 ${names.length} ${unit}`
      : `${prefix} · ${shown}`;
  };
  const open = () => {
    if (!stats || !meta) return;
    // 区域分布:保留「铺形状」的画法(区界按起数深浅着色,见 SpotlightLayer),
    // 其余三类(街道 / 道路 / 反复出险位置)统一改成「在地图上只看这一类」——
    // 保留热力图 + 聚合点,与单项点击的口径完全一致,而不是换画法铺形状。
    if (kind === 'districts') {
      const view = buildSpotlight(kind, stats, meta);
      if (view) openSpotlight(view);
      return;
    }
    if (kind === 'streets') {
      const top = stats.topStreets.slice(0, SPOTLIGHT_TOP_N.streets);
      const idxs = top.map(([i]) => i);
      if (idxs.length) {
        // 街道字典形如「福田区|华强北街道」,牌子与抽屉里只念街道那一截(placeNameOf 负责拆)
        const names = idxs.map((i) => placeNameOf(meta.dict.streets[i]));
        setMapScope({ label: scopeLabel('高频街道', names, '条'), streets: idxs });
      }
      return;
    }
    if (kind === 'roads') {
      // 道路维度可能整列为空(老产物没有道路字典),此时不铺任何收窄
      const idxs = (stats.road?.counts ?? []).slice(0, SPOTLIGHT_TOP_N.roads).map(([i]) => i);
      if (idxs.length) {
        const names = idxs.map((i) => stats.road?.names?.[i] ?? '');
        setMapScope({ label: scopeLabel('高频道路', names, '条'), roads: idxs });
      }
      return;
    }
    // places:反复出险位置没有字典下标,用每处的地理方框收窄(见 MapScope.bboxes)
    const bboxes = stats.place.top.map((p) => ({
      minLng: p.minLng,
      minLat: p.minLat,
      maxLng: p.maxLng,
      maxLat: p.maxLat,
    }));
    if (bboxes.length) {
      setMapScope({ label: `反复出险位置 · ${bboxes.length} 处`, bboxes });
    }
  };

  return (
    <Tooltip title={reason ?? spotlightEntryTip(kind, stats, meta)}>
      {/* 禁用按钮在浏览器里不触发鼠标事件,外层包一层才念得出那句「为什么点不动」 */}
      <span className={`hm-spot-entry-wrap hm-spot-entry-wrap--${variant}`}>
        <button
          type="button"
          className={`hm-spot-entry hm-spot-entry--${variant}`}
          disabled={!!reason}
          onClick={open}
        >
          {spotlightEntryLabel(kind, stats, meta)}
        </button>
      </span>
    </Tooltip>
  );
}