All files / presentation/ui/src/journeys TripOptionPicker.tsx

100% Statements 9/9
97.29% Branches 36/37
100% Functions 7/7
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121                                                                                    107x 107x                                               244x             5x   2x 13x 4x 16x                                                                                  
import type { TripPlannerItinerary } from "@overworld/trimet-client";
import { Button } from "@wintermuted/ui-react";
import React from "react";
import {
  PlannerDisclosure,
  TripOptionSummary,
  transferCount,
  type TransitLegColor,
} from "./ItineraryPresentation";
import "./TripOptionPicker.scss";
 
export interface TripOptionPickerProps {
  itineraries: readonly TripPlannerItinerary[];
  selectedIndex: number;
  onSelect: (index: number) => void;
  editing?: boolean;
  onEdit?: () => void;
  onCancel?: () => void;
  onCollapse?: () => void;
  onHover?: (index?: number) => void;
  onFocus?: (index?: number) => void;
  currentOptionRef?: React.Ref<HTMLButtonElement>;
  editButtonRef?: React.Ref<HTMLButtonElement>;
  transitColor?: TransitLegColor;
  emptyContent?: React.ReactNode;
}
 
export function TripOptionPicker({
  itineraries,
  selectedIndex,
  onSelect,
  editing = false,
  onEdit,
  onCancel,
  onCollapse,
  onHover,
  onFocus,
  currentOptionRef,
  editButtonRef,
  transitColor,
  emptyContent = <p role="status">No trips available.</p>,
}: TripOptionPickerProps) {
  const active = itineraries[selectedIndex];
  return (
    <PlannerDisclosure
      title="Choose a trip"
      onCollapse={onCollapse}
      edit={
        active && !editing && onEdit
          ? {
              label: "Edit trip",
              buttonRef: editButtonRef ?? null,
              onClick: onEdit,
            }
          : undefined
      }
    >
      {!active || editing ? (
        <>
          {!itineraries.length ? (
            emptyContent
          ) : (
            <ul
              className="transit-planner-options"
              aria-label="Available trips"
            >
              {itineraries.map((itinerary, index) => (
                <li key={itinerary.id || index}>
                  <button
                    type="button"
                    className="transit-planner-option"
                    aria-current={selectedIndex === index ? "true" : undefined}
                    ref={selectedIndex === index ? currentOptionRef : undefined}
                    onPointerEnter={(event) => {
                      if (event.pointerType !== "touch") onHover?.(index);
                    }}
                    onPointerLeave={() => onHover?.(undefined)}
                    onFocus={() => onFocus?.(index)}
                    onBlur={() => onFocus?.(undefined)}
                    onClick={() => onSelect(index)}
                  >
                    <TripOptionSummary
                      itinerary={itinerary}
                      index={index}
                      current={selectedIndex === index}
                      transitColor={transitColor}
                    />
                  </button>
                </li>
              ))}
            </ul>
          )}
          {editing && onCancel && (
            <Button type="button" variant="secondary" onClick={onCancel}>
              Cancel
            </Button>
          )}
        </>
      ) : (
        <div className="transit-planner-summary">
          <h4>
            Option {selectedIndex + 1}
            {active.startTime &&
              active.endTime &&
              ` · ${active.startTime}–${active.endTime}`}
          </h4>
          <p>
            {active.duration !== undefined && `${active.duration} min · `}
            {transferCount(active)}{" "}
            {transferCount(active) === 1 ? "transfer" : "transfers"}
            {active.distance !== undefined &&
              ` · ${active.distance.toFixed(2)} mi`}
            {active.fare !== undefined &&
              ` · $${active.fare.toFixed(2)} regular fare`}
          </p>
        </div>
      )}
    </PlannerDisclosure>
  );
}