All files / presentation/ui/src/alerts AllAlertsCard.tsx

100% Statements 17/17
92.85% Branches 26/28
100% Functions 10/10
100% Lines 17/17

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 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                                              2x                     2x                                 2x                   1x                         2x 1x                 2x 2x   2x                                                 18x 9x   9x 7x                                                 2x                                       2x                                                       18x 18x                  
import {
  Accordion,
  Badge,
  Card,
  CardBody,
  CardHeader,
  RefreshAction,
  Table,
} from "@wintermuted/ui-react";
import React from "react";
import type {
  Alert,
  AlertLocation,
  RouteConfigRoute,
} from "@overworld/trimet-client";
import { useTimeDisplay } from "../TimeDisplay";
import type { AlertFeedState } from "./types";
import "./Alerts.scss";
 
function getStopClosureTable(
  locations: AlertLocation[],
  renderLink: AllAlertsCardProps["renderStopLink"],
) {
  return (
    <Table responsive={true}>
      <thead>
        <tr>
          <th>Stop ID</th>
          <th>Desc</th>
          <th>Direction</th>
        </tr>
      </thead>
      <tbody>
        {locations.map((loc) => {
          return (
            <tr key={loc.id}>
              <td>{renderLink(loc.id)}</td>
              <td>{loc.desc}</td>
              <td>{loc.dir}</td>
            </tr>
          );
        })}
      </tbody>
    </Table>
  );
}
 
function getRouteAlertTable(
  routes: RouteConfigRoute[],
  renderLink: AllAlertsCardProps["renderRouteLink"],
) {
  return (
    <Table responsive={true}>
      <thead>
        <tr>
          <th>Route ID</th>
          <th>Desc</th>
        </tr>
      </thead>
      <tbody>
        {routes.map((route) => {
          return (
            <tr key={route.id}>
              <td>{renderLink(route.id)}</td>
              <td>{route.desc}</td>
            </tr>
          );
        })}
      </tbody>
    </Table>
  );
}
 
function getImpactedRoutes(routes: RouteConfigRoute[]) {
  return routes.map((route) => {
    return (
      <Badge tone="info" key={route.id}>
        {route.id}
      </Badge>
    );
  });
}
 
function getHeader(alert: Alert, formatDate: (timestamp: number) => string) {
  const startDate = formatDate(alert.begin);
  const endDate = alert.end ? formatDate(alert.end) : undefined;
 
  return (
    <>
      {startDate} to {endDate ? endDate : "Unknown"}
    </>
  );
}
 
export interface AllAlertsCardProps extends AlertFeedState {
  alertsData: Alert[] | undefined;
  renderStopLink: (stopId: number) => React.ReactNode;
  renderRouteLink: (routeId: number) => React.ReactNode;
}
 
function getAlertsAccordion(
  {
    alertsData,
    loading,
    error,
    onRefresh,
    loadingContent,
    renderStopLink,
    renderRouteLink,
  }: AllAlertsCardProps,
  formatDate: (timestamp: number) => string,
) {
  if (loading && !alertsData) {
    return loadingContent;
  }
  if (!alertsData?.length) {
    return (
      <>
        {error && (
          <p role="alert">
            {alertsData !== undefined
              ? "Could not refresh alerts. Showing the last received alerts."
              : "Could not load alerts. Try again."}
          </p>
        )}
        {!error && <p role="status">No route or stop alerts right now.</p>}
        <RefreshAction
          size="sm"
          onRefresh={onRefresh}
          loading={loading}
          error={error}
          labels={{
            idle: "Refresh",
            loading: "Refreshing...",
            retry: "Try again",
          }}
        />
      </>
    );
  }
 
  return (
    <div>
      {error && (
        <p role="alert">
          Could not refresh alerts. Showing the last received alerts.
        </p>
      )}
      <RefreshAction
        size="sm"
        onRefresh={onRefresh}
        loading={loading}
        error={error}
        labels={{
          idle: "Refresh",
          loading: "Refreshing...",
          retry: "Try again",
        }}
      />
      {alertsData &&
        alertsData.map((alert) => {
          return (
            <Accordion
              key={alert.id}
              open={String(alert.id) === "0"}
              title={
                <span className="transit-alert-heading">
                  {alert.location && <Badge tone="warning">Stop Closure</Badge>}
                  {alert.route && getImpactedRoutes(alert.route)}
                  <span>
                    {alert.id} - {alert.header_text}
                  </span>
                  <small>{getHeader(alert, formatDate)}</small>
                </span>
              }
            >
              {alert.desc}
              <br />
              {alert.location &&
                getStopClosureTable(alert.location, renderStopLink)}
              {alert.route && getRouteAlertTable(alert.route, renderRouteLink)}
            </Accordion>
          );
        })}
    </div>
  );
}
 
function AllAlertsCard(props: AllAlertsCardProps) {
  const { formatDate } = useTimeDisplay();
  return (
    <Card>
      <CardHeader>Route & Stop Alerts</CardHeader>
      <CardBody>{getAlertsAccordion(props, formatDate)}</CardBody>
    </Card>
  );
}
 
export default AllAlertsCard;