Skip to content

Open-source status page, incident response and uptime monitoring

8.5 KB
Raw Blame
"use client";

import type { RouterOutputs } from "@openstatus/api";
import {
  type IncidentStatus,
  incidentSeverity,
} from "@openstatus/db/src/schema/incidents/constants";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectValue,
} from "@openstatus/ui/components/ui/select";
import { personName } from "@openstatus/utils";
import { useMutation, useQuery } from "@tanstack/react-query";
import {
  format,
  formatDistanceStrict,
  formatDistanceToNowStrict,
} from "date-fns";
import { useState } from "react";
import { toast } from "sonner";

import { HoverCardTimestamp } from "@/components/common/hover-card-timestamp";
import { StatusDot } from "@/components/common/status-dot";
import { UserAvatar } from "@/components/common/user-avatar";
import {
  Property,
  PropertyLabel,
  PropertyList,
  PropertySelectTrigger,
  PropertyValue,
} from "@/components/content/property-list";
import {
  incidentEndedAt,
  severityConfig,
  statusConfig,
} from "@/data/managed-incidents.client";
import { useTRPC } from "@/lib/trpc/client";
import { errorMessage } from "@/lib/trpc/error";

import { ConfirmCloseDialog } from "./confirm-close-dialog";
import { useInvalidateIncident } from "./use-invalidate-incident";

type Incident = NonNullable<RouterOutputs["incident"]["get"]>;

const NONE = "none";

export function IncidentProperties({
  incident,
  lastUpdateAt,
  onStatusChanged,
}: {
  incident: Incident;
  lastUpdateAt?: Date;
  onStatusChanged: (status: IncidentStatus, note: string) => void;
}) {
  const trpc = useTRPC();
  const { data: members } = useQuery(trpc.member.list.queryOptions());
  const [confirmCancel, setConfirmCancel] = useState(false);
  const closed = incident.closedAt !== null;

  const invalidate = useInvalidateIncident(incident.id);
  const onError = (error: { message: string }) => {
    toast.error(errorMessage(error, "Failed to save"));
  };
  const update = useMutation(
    trpc.incident.update.mutationOptions({
      onSuccess: invalidate,
      onError,
    }),
  );
  const setStatus = useMutation(
    trpc.incident.setStatus.mutationOptions({
      onSuccess: async (row) => {
        await invalidate();
        setConfirmCancel(false);
        if (row) onStatusChanged(row.status, "");
      },
      onError,
    }),
  );

  const severity = severityConfig[incident.severity];
  const status = statusConfig[incident.status];
  const commander = personName(incident.commander);
  const endedAt = incidentEndedAt(incident);

  return (
    <PropertyList>
      <ConfirmCloseDialog
        kind="cancel"
        open={confirmCancel}
        onOpenChange={setConfirmCancel}
        pending={setStatus.isPending}
        onConfirm={() =>
          setStatus.mutate({ id: incident.id, status: "canceled" })
        }
      />
      <Property>
        <PropertyLabel>Severity</PropertyLabel>
        <PropertyValue>
          {closed ? (
            <>
              <StatusDot variant={severity.variant} className="rounded-xs" />
              {severity.label}
            </>
          ) : (
            <Select
              value={incident.severity}
              onValueChange={(value) => {
                const next = incidentSeverity.find((s) => s === value);
                if (next) update.mutate({ id: incident.id, severity: next });
              }}
            >
              <PropertySelectTrigger aria-label="Severity">
                <SelectValue />
              </PropertySelectTrigger>
              <SelectContent>
                {incidentSeverity.map((s) => (
                  <SelectItem key={s} value={s} className="font-mono">
                    <StatusDot
                      variant={severityConfig[s].variant}
                      className="rounded-xs"
                    />
                    {severityConfig[s].label}
                  </SelectItem>
                ))}
              </SelectContent>
            </Select>
          )}
        </PropertyValue>
      </Property>
      <Property>
        <PropertyLabel>Status</PropertyLabel>
        <PropertyValue>
          {incident.allowedTransitions.length === 0 ? (
            <>
              <StatusDot variant={status.variant} />
              {status.label}
              {closed && incident.status !== "canceled" ? " · closed" : null}
            </>
          ) : (
            <Select
              value={incident.status}
              onValueChange={(value) => {
                const next = incident.allowedTransitions.find(
                  (s) => s === value,
                );
                if (!next) return;
                // Canceling closes the incident for good; ask first.
                if (next === "canceled") setConfirmCancel(true);
                else setStatus.mutate({ id: incident.id, status: next });
              }}
            >
              <PropertySelectTrigger aria-label="Status">
                <SelectValue />
              </PropertySelectTrigger>
              <SelectContent>
                {[incident.status, ...incident.allowedTransitions].map((s) => (
                  <SelectItem key={s} value={s} className="font-mono">
                    <StatusDot variant={statusConfig[s].variant} />
                    {statusConfig[s].label}
                  </SelectItem>
                ))}
              </SelectContent>
            </Select>
          )}
        </PropertyValue>
      </Property>
      <Property>
        <PropertyLabel>Commander</PropertyLabel>
        <PropertyValue>
          {closed ? (
            commander ? (
              <>
                <UserAvatar
                  name={commander}
                  src={incident.commander?.photoUrl}
                />
                <span className="truncate">{commander}</span>
              </>
            ) : (
              <span className="text-muted-foreground">No commander</span>
            )
          ) : (
            <Select
              value={incident.commanderId ? String(incident.commanderId) : NONE}
              onValueChange={(value) =>
                update.mutate({
                  id: incident.id,
                  commanderId: value === NONE ? null : Number(value),
                })
              }
            >
              <PropertySelectTrigger aria-label="Commander">
                <SelectValue />
              </PropertySelectTrigger>
              <SelectContent>
                <SelectItem value={NONE} className="font-mono">
                  <span className="text-muted-foreground">No commander</span>
                </SelectItem>
                {(members ?? []).map((member) => {
                  const name =
                    personName(member.user) ?? `User ${member.user.id}`;
                  return (
                    <SelectItem
                      key={member.user.id}
                      value={String(member.user.id)}
                      className="font-mono"
                    >
                      <UserAvatar name={name} src={member.user.photoUrl} />
                      <span className="truncate">{name}</span>
                    </SelectItem>
                  );
                })}
              </SelectContent>
            </Select>
          )}
        </PropertyValue>
      </Property>
      <Property>
        <PropertyLabel>Started at</PropertyLabel>
        <PropertyValue>
          <HoverCardTimestamp date={incident.startedAt} side="left">
            <span>{format(incident.startedAt, "LLL dd, y HH:mm")}</span>
          </HoverCardTimestamp>
        </PropertyValue>
      </Property>
      <Property>
        <PropertyLabel>Duration</PropertyLabel>
        <PropertyValue>
          {formatDistanceStrict(incident.startedAt, endedAt ?? new Date())}
          {endedAt ? null : (
            <>
              <StatusDot variant="destructive" className="size-1.5" />
              <span className="sr-only">ongoing</span>
            </>
          )}
        </PropertyValue>
      </Property>
      {lastUpdateAt ? (
        <Property>
          <PropertyLabel>Last update</PropertyLabel>
          <PropertyValue>
            <HoverCardTimestamp date={lastUpdateAt} side="left">
              <span>
                {formatDistanceToNowStrict(lastUpdateAt, { addSuffix: true })}
              </span>
            </HoverCardTimestamp>
          </PropertyValue>
        </Property>
      ) : null}
      <Property>
        <PropertyLabel>Declared by</PropertyLabel>
        <PropertyValue>
          <span className="truncate">
            {personName(incident.declaredByUser) ?? "System"}
          </span>
        </PropertyValue>
      </Property>
    </PropertyList>
  );
}