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

90% Statements 18/20
78.57% Branches 22/28
100% Functions 8/8
90% Lines 18/20

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                                2x 1x     1x       2x             2x     2x                 2x 2x   2x               2x                                     5x 5x       5x                     2x                               1x                   2x                                                             5x 5x              
import {
  Badge,
  Card,
  CardBody,
  CardFooter,
  CardHeader,
  CardText,
  CardTitle,
  RefreshAction,
} from "@wintermuted/ui-react";
import React from "react";
import type { Alert } from "@overworld/trimet-client";
import { useTimeDisplay } from "../TimeDisplay";
import type { AlertFeedState } from "./types";
 
function getLink(alert: Alert) {
  if (!alert.info_link_url) {
    return null;
  }
 
  return <a href={alert.info_link_url}>{alert.info_link_url}</a>;
}
 
function getHeader(alert: Alert) {
  const title = alert.header_text ? (
    alert.header_text
  ) : (
    <span>
      <strong>No Service:</strong> {alert.id}
    </span>
  );
  const systemAlert = alert.system_wide_flag ? (
    <strong>System Alert:</strong>
  ) : null;
  return (
    <>
      <span>{systemAlert}</span>
      <span>{title}</span>
    </>
  );
}
 
function getDates(alert: Alert, formatDate: (timestamp: number) => string) {
  const startDate = formatDate(alert.begin);
  const endDate = alert.end ? formatDate(alert.end) : undefined;
 
  Iif (endDate) {
    return (
      <>
        {startDate} to {endDate}
      </>
    );
  }
 
  return <>Starting: {startDate}</>;
}
 
export interface StopAlertsCardProps extends AlertFeedState {
  alertsData: Alert[] | undefined;
  renderRouteIndicator: (routeId: number) => React.ReactNode;
}
 
function getAlertsContent(
  {
    alertsData,
    loading,
    error,
    onRefresh,
    loadingContent,
    renderRouteIndicator,
  }: StopAlertsCardProps,
  formatDate: (timestamp: number) => string,
) {
  const scopedAlerts = alertsData || [];
  Iif (loading && !alertsData) {
    return loadingContent;
  }
 
  return (
    <>
      {error && (
        <p role="alert">
          {alertsData !== undefined
            ? "Could not refresh alerts. Showing the last received alerts."
            : "Could not load alerts for this stop. Try again."}
        </p>
      )}
      {scopedAlerts.length ? (
        scopedAlerts.map((alert: Alert) => (
          <Card
            key={alert.id}
            style={{
              background: "var(--wm-color-warning-soft)",
              marginBottom: ".25rem",
            }}
          >
            <CardHeader>{getHeader(alert)}</CardHeader>
            <CardBody>
              <CardText>{alert.desc}</CardText>
              {getLink(alert)}
            </CardBody>
            {alert.route && (
              <CardBody>
                <CardTitle>Impacted Routes</CardTitle>
                {alert.route.map((route) => (
                  <React.Fragment key={route.route}>
                    {renderRouteIndicator(route.route)}
                  </React.Fragment>
                ))}
              </CardBody>
            )}
            {alert.location && (
              <CardBody>
                <CardTitle>Impacted Stops</CardTitle>
                {alert.location.map((location) => (
                  <span key={location.id}>
                    <Badge tone="info">{location.id}</Badge>
                    {location.desc} - {location.dir}
                  </span>
                ))}
              </CardBody>
            )}
            <CardFooter style={{ color: "var(--wm-color-text-muted)" }}>
              {getDates(alert, formatDate)}
            </CardFooter>
          </Card>
        ))
      ) : !error ? (
        <p role="status">No active alerts for this stop.</p>
      ) : null}
      <RefreshAction
        size="sm"
        onRefresh={onRefresh}
        loading={loading}
        error={error}
        labels={{
          idle: "Refresh",
          loading: "Refreshing...",
          retry: "Try again",
        }}
      />
    </>
  );
}
 
export function StopAlertsCard(props: StopAlertsCardProps) {
  const { formatDate } = useTimeDisplay();
  return (
    <div>
      <h5>Alerts</h5>
      <div>{getAlertsContent(props, formatDate)}</div>
    </div>
  );
}