aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/app/components/arrivals/ReducedArrivalCard.tsx
blob: 4ba08b1f273f243ed72204dc11fa14d6de77784e (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { AlertTriangle, BusFront, LocateIcon } from "lucide-react";
import React, { useMemo } from "react";
import { useTranslation } from "react-i18next";
import RouteIcon from "~/components/RouteIcon";
import { type Arrival } from "../../api/schema";
import "./ArrivalCard.css";

interface ArrivalCardProps {
  arrival: Arrival;
  onClick?: () => void;
}

export const ReducedArrivalCard: React.FC<ArrivalCardProps> = ({
  arrival,
  onClick,
}) => {
  const { t } = useTranslation();
  const { route, headsign, estimate, delay, shift, vehicleInformation } =
    arrival;

  const etaValue = estimate.minutes.toString();
  const etaUnit = t("estimates.minutes", "min");

  const timeClass = useMemo(() => {
    switch (estimate.precision) {
      case "confident":
        return "time-running";
      case "unsure":
        return "time-delayed";
      case "past":
        return "time-past";
      default:
        return "time-scheduled";
    }
  }, [estimate.precision]);

  const metaChips = useMemo(() => {
    const chips: Array<{
      label: string;
      tone?: string;
      kind?: "regular" | "gps" | "delay" | "warning" | "vehicle";
    }> = [];

    // Badge/Shift info as a chip
    if (headsign.badge) {
      chips.push({
        label: headsign.badge,
        kind: "regular",
      });
    }

    // Delay chip
    if (delay) {
      const delta = Math.round(delay.minutes);
      const absDelta = Math.abs(delta);

      if (delta === 0) {
        chips.push({
          label: "OK",
          tone: "delay-ok",
          kind: "delay",
        });
      } else if (delta > 0) {
        const tone =
          delta <= 2
            ? "delay-ok"
            : delta <= 10
              ? "delay-warn"
              : "delay-critical";
        chips.push({
          label: `R${delta}`,
          tone,
          kind: "delay",
        });
      } else {
        const tone = absDelta <= 2 ? "delay-ok" : "delay-early";
        chips.push({
          label: `A${absDelta}`,
          tone,
          kind: "delay",
        });
      }
    }

    // Shift chip
    if (shift) {
      chips.push({
        label: `${shift.shiftName} · ${shift.shiftTrip}`,
        kind: "regular",
      });
    }

    // Precision chips
    if (estimate.precision === "unsure") {
      chips.push({
        label: "!",
        tone: "warning",
        kind: "warning",
      });
    } else if (
      estimate.precision === "confident" &&
      arrival.currentPosition !== null
    ) {
      chips.push({
        label: "", // Just the icon for reduced
        kind: "gps",
      });
    }

    // Vehicle information if available
    if (vehicleInformation) {
      let label = vehicleInformation.identifier;
      if (vehicleInformation.make) {
        label += ` (${vehicleInformation.make}`;
        if (vehicleInformation.kind) {
          const kindLabel =
            vehicleInformation.kind.charAt(0).toUpperCase() +
            vehicleInformation.kind.slice(1).toLowerCase();
          label += ` ${kindLabel}.`;
        }
        if (vehicleInformation.year) {
          label += ` ${vehicleInformation.year}`;
        }
        label += `)`;
      }
      chips.push({
        label,
        kind: "vehicle",
      });
    }

    return chips;
  }, [delay, shift, estimate.precision, headsign.badge, vehicleInformation]);

  const isClickable = !!onClick && estimate.precision !== "past";
  const Tag = isClickable ? "button" : "div";

  return (
    <Tag
      onClick={isClickable ? onClick : undefined}
      className={`w-full text-left flex-none flex items-center gap-3 min-h-12 rounded px-3 py-2.5 transition-all bg-slate-50 dark:bg-slate-800 border border-gray-200 dark:border-gray-700 ${
        isClickable
          ? "hover:border-blue-400 dark:hover:border-blue-500 active:scale-[0.99] cursor-pointer"
          : ""
      }`}
    >
      <div className="shrink-0 min-w-[7ch] mt-0.5">
        <RouteIcon
          line={route.shortName}
          colour={route.colour}
          textColour={route.textColour}
          mode="pill"
        />
      </div>
      <div className="flex-1 min-w-0 flex flex-col gap-0.5">
        <span
          className={`text-base font-medium overflow-hidden text-ellipsis line-clamp-2 leading-tight ${estimate.precision == "past" ? "line-through opacity-60" : ""}`}
        >
          {headsign.destination}
        </span>
        {metaChips.length > 0 && (
          <div className="flex items-center gap-0.5 flex-wrap">
            {metaChips.map((chip, idx) => {
              let chipColourClasses = "";
              switch (chip.tone) {
                case "delay-ok":
                  chipColourClasses =
                    "bg-green-600/10 dark:bg-green-600/20 text-green-700 dark:text-green-300";
                  break;
                case "delay-warn":
                  chipColourClasses =
                    "bg-amber-600/10 dark:bg-yellow-600/20 text-amber-700 dark:text-yellow-300";
                  break;
                case "delay-critical":
                  chipColourClasses =
                    "bg-red-400/10 dark:bg-red-600/20 text-red-600 dark:text-red-300";
                  break;
                case "delay-early":
                  chipColourClasses =
                    "bg-blue-400/10 dark:bg-blue-600/20 text-blue-700 dark:text-blue-300";
                  break;
                case "warning":
                  chipColourClasses =
                    "bg-orange-400/10 dark:bg-orange-600/20 text-orange-700 dark:text-orange-300";
                  break;
                default:
                  chipColourClasses =
                    "bg-black/[0.04] dark:bg-white/[0.08] text-slate-500 dark:text-slate-400";
              }

              return (
                <span
                  key={`${chip.label}-${idx}`}
                  className={`text-xs px-2.5 py-0.5 rounded-full flex items-center justify-center gap-1 shrink-0 font-medium tracking-wide ${chipColourClasses}`}
                >
                  {chip.kind === "gps" && (
                    <LocateIcon className="w-3 h-3 my-0.5 inline-block" />
                  )}
                  {chip.kind === "warning" && (
                    <AlertTriangle className="w-3 h-3 my-0.5 inline-block" />
                  )}
                  {chip.kind === "vehicle" && (
                    <BusFront className="w-3 h-3 my-0.5 inline-block" />
                  )}
                  {chip.label}
                </span>
              );
            })}
          </div>
        )}
      </div>
      <div
        className={`
          inline-flex items-center justify-center px-2 py-1.5 rounded-xl shrink-0
          ${timeClass}
        `.trim()}
      >
        <div className="flex flex-col items-center leading-none">
          <span className="text-lg font-bold leading-none">{etaValue}</span>
          <span className="text-[0.55rem] uppercase tracking-wider mt-0.5 opacity-90">
            {etaUnit}
          </span>
        </div>
      </div>
    </Tag>
  );
};