<commit_before>import React, {
  useState,
  useEffect,
  useRef,
  useCallback,
  useMemo,
} from 'react';
import { connect } from 'react-redux';
import { Dispatch } from 'redux';
import { Polyline, Tooltip, Marker, CircleMarker } from 'react-leaflet';

import { RichMarker } from 'fm3/components/RichMarker';
import { ElevationChartActivePoint } from 'fm3/components/ElevationChartActivePoint';
import {
  routePlannerSetStart,
  routePlannerSetFinish,
  routePlannerAddMidpoint,
  routePlannerSetMidpoint,
  routePlannerRemoveMidpoint,
  routePlannerSetActiveAlternativeIndex,
  Alternative,
  RouteAlternativeExtra,
  RouteStepExtra,
} from 'fm3/actions/routePlannerActions';
import { Translator, withTranslator } from 'fm3/l10nInjector';
import { RootAction } from 'fm3/actions';
import { RootState } from 'fm3/storeCreator';
import { LatLon } from 'fm3/types/common';
import { divIcon, DragEndEvent, LeafletMouseEvent } from 'leaflet';
import { isSpecial } from 'fm3/transportTypeDefs';
import { lineString, Point, Properties, Feature } from '@turf/helpers';
import along from '@turf/along';
import length from '@turf/length';

type Props = ReturnType<typeof mapStateToProps> &
  ReturnType<typeof mapDispatchToProps> & {
    t: Translator;
  };

const RoutePlannerResultInt: React.FC<Props> = ({
  transportType,
  t,
  language,
  activeAlternativeIndex,
  alternatives,
  mode,
  onFinishSet,
  onAddMidpoint,
  onAlternativeChange,
  onStartSet,
  onMidpointSet,
  onRemoveMidpoint,
  start,
  midpoints,
  finish,
  timestamp,
  zoom,
  showMilestones,
}) => {
  const tRef = useRef<number>();
  const draggingRef = useRef<boolean>();

  const [dragLat, setDragLat] = useState<number>();
  const [dragLon, setDragLon] = useState<number>();
  const [dragSegment, setDragSegment] = useState<number>();
  const [dragAlt, setDragAlt] = useState<number>();

  useEffect(
    () => () => {
      const tim = tRef.current;
      if (typeof tim === 'number') {
        clearTimeout(tim);
      }
    },
    [],
  );

  function getSummary() {
    const { distance = undefined, duration = undefined, extra = undefined } =
      alternatives.find((_, alt) => alt === activeAlternativeIndex) || {};

    const nf = Intl.NumberFormat(language, {
      minimumFractionDigits: 1,
      maximumFractionDigits: 1,
    });

    return isSpecial(transportType) && extra?.numbers ? (
      <Tooltip direction="top" offset={[0, -36]} permanent>
        <div>{imhdSummary(t, language, extra)}</div>
      </Tooltip>
    ) : distance ? (
      <Tooltip direction="top" offset={[0, -36]} permanent>
        <div>
          <div>
            {t('routePlanner.distance', { value: nf.format(distance) })}
          </div>
          {duration !== undefined && (
            <div>
              {t('routePlanner.duration', {
                h: Math.floor(duration / 60),
                m: Math.round(duration % 60),
              })}
            </div>
          )}
        </div>
      </Tooltip>
    ) : null;
  }

  const bringToFront = useCallback(ele => {
    if (ele) {
      ele.leafletElement.bringToFront();
    }
  }, []);

  const maneuverToText = useCallback(
    (name: string, { type, modifier }, extra: RouteStepExtra) => {
      const p = 'routePlanner.maneuver';
      return transportType === 'imhd'
        ? imhdStep(t, language, extra)
        : transportType === 'bikesharing'
        ? bikesharingStep(t, extra)
        : t(`routePlanner.maneuverWith${name ? '' : 'out'}Name`, {
            type: t(`${p}.types.${type}`, {}, type),
            modifier: modifier
              ? ` ${t(`${p}.modifiers.${modifier}`, {}, modifier)}`
              : '',
            name,
          });
    },
    [t, transportType, language],
  );

  const handleStartPointClick = useCallback(() => {
    // prevent default<commit_msg>added useMemo for route-planner summary<commit_after>import React, {
  useState,
  useEffect,
  useRef,
  useCallback,
  useMemo,
} from 'react';
import { connect } from 'react-redux';
import { Dispatch } from 'redux';
import { Polyline, Tooltip, Marker, CircleMarker } from 'react-leaflet';

import { RichMarker } from 'fm3/components/RichMarker';
import { ElevationChartActivePoint } from 'fm3/components/ElevationChartActivePoint';
import {
  routePlannerSetStart,
  routePlannerSetFinish,
  routePlannerAddMidpoint,
  routePlannerSetMidpoint,
  routePlannerRemoveMidpoint,
  routePlannerSetActiveAlternativeIndex,
  Alternative,
  RouteAlternativeExtra,
  RouteStepExtra,
} from 'fm3/actions/routePlannerActions';
import { Translator, withTranslator } from 'fm3/l10nInjector';
import { RootAction } from 'fm3/actions';
import { RootState } from 'fm3/storeCreator';
import { LatLon } from 'fm3/types/common';
import { divIcon, DragEndEvent, LeafletMouseEvent } from 'leaflet';
import { isSpecial } from 'fm3/transportTypeDefs';
import { lineString, Point, Properties, Feature } from '@turf/helpers';
import along from '@turf/along';
import length from '@turf/length';

type Props = ReturnType<typeof mapStateToProps> &
  ReturnType<typeof mapDispatchToProps> & {
    t: Translator;
  };

const RoutePlannerResultInt: React.FC<Props> = ({
  transportType,
  t,
  language,
  activeAlternativeIndex,
  alternatives,
  mode,
  onFinishSet,
  onAddMidpoint,
  onAlternativeChange,
  onStartSet,
  onMidpointSet,
  onRemoveMidpoint,
  start,
  midpoints,
  finish,
  timestamp,
  zoom,
  showMilestones,
}) => {
  const tRef = useRef<number>();
  const draggingRef = useRef<boolean>();

  const [dragLat, setDragLat] = useState<number>();
  const [dragLon, setDragLon] = useState<number>();
  const [dragSegment, setDragSegment] = useState<number>();
  const [dragAlt, setDragAlt] = useState<number>();

  useEffect(
    () => () => {
      const tim = tRef.current;
      if (typeof tim === 'number') {
        clearTimeout(tim);
      }
    },
    [],
  );

  const getSummary = useMemo(() => {
    const { distance = undefined, duration = undefined, extra = undefined } =
      alternatives.find((_, alt) => alt === activeAlternativeIndex) || {};

    const nf = Intl.NumberFormat(language, {
      minimumFractionDigits: 1,
      maximumFractionDigits: 1,
    });

    return isSpecial(transportType) && extra?.numbers ? (
      <Tooltip direction="top" offset={[0, -36]} permanent>
        <div>{imhdSummary(t, language, extra)}</div>
      </Tooltip>
    ) : distance ? (
      <Tooltip direction="top" offset={[0, -36]} permanent>
        <div>
          <div>
            {t('routePlanner.distance', { value: nf.format(distance) })}
          </div>
          {duration !== undefined && (
            <div>
              {t('routePlanner.duration', {
                h: Math.floor(duration / 60),
                m: Math.round(duration % 60),
              })}
            </div>
          )}
        </div>
      </Tooltip>
    ) : null;
  }, [alternatives, activeAlternativeIndex, language, t, transportType]);

  const bringToFront = useCallback(ele => {
    if (ele) {
      ele.leafletElement.bringToFront();
    }
  }, []);

  const maneuverToText = useCallback(
    (name: string, { type, modifier }, extra: RouteStepExtra) => {
      const p = 'routePlanner.maneuver';
      return transportType === 'imhd'
        ? imhdStep(t, language, extra)
        : transportType === 'bikesharing'
        ? bikesharingStep(t, extra)
        : t(`routePlanner.maneuverWith${name ? '' : 'out'}Name`, {
            type: t(`${p}.types.${type}`, {}, type),
            modifier: modifier
              ? ` ${t(`${p}.modifiers.${modifier}`, {}, modifier)}`
              : '',
            name,
          });
    },
    [t, transportType, language],
  );

  const handleStartPointClick = useCallback(() => {
    // prevent default
{'fraction_non_alphanumeric': 0.14188056236295626, 'fraction_numerical': 0.005675222494518251, 'mean_word_length': 3.068205666316894, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5434599', 'n_tokens_mistral': 2492, 'n_tokens_neox': 2410, 'n_words': 509}