Skip to content

Open-source status page, incident response and uptime monitoring

feat: delete incidents (#713)

Maximilian Kaske3a96ecd49b39
diff --git a/apps/web/src/components/data-table/data-table.tsx b/apps/web/src/components/data-table/data-table.tsx
index 1e59e4bff..9cef74567 100644
--- a/apps/web/src/components/data-table/data-table.tsx
+++ b/apps/web/src/components/data-table/data-table.tsx
@@ -103,13 +103,9 @@ export function DataTable<TData, TValue>({
(row.getIsSelected() || row.getIsExpanded()) && "selected"
}
onClick={() => {
- // FIXME: this is a workaround for single row expansion
- if (row.getIsExpanded()) {
- row.toggleExpanded();
- } else {
- table.resetExpanded();
- row.toggleExpanded();
- }
+ // REMINDER: this is a workaround for single row expansion
+ if (!row.getIsExpanded()) table.resetExpanded();
+ row.toggleExpanded();
}}
className="cursor-pointer"
>
diff --git a/apps/web/src/components/data-table/incident/data-table-row-actions.tsx b/apps/web/src/components/data-table/incident/data-table-row-actions.tsx
index db4908a1c..bf9812702 100644
--- a/apps/web/src/components/data-table/incident/data-table-row-actions.tsx
+++ b/apps/web/src/components/data-table/incident/data-table-row-actions.tsx
@@ -7,6 +7,15 @@ import { MoreHorizontal } from "lucide-react";
import { selectIncidentSchema } from "@openstatus/db/src/schema";
import {
+ AlertDialog,
+ AlertDialogAction,
+ AlertDialogCancel,
+ AlertDialogContent,
+ AlertDialogDescription,
+ AlertDialogFooter,
+ AlertDialogHeader,
+ AlertDialogTitle,
+ AlertDialogTrigger,
Button,
DropdownMenu,
DropdownMenuContent,
@@ -15,7 +24,8 @@ import {
DropdownMenuTrigger,
} from "@openstatus/ui";
-import { toastAction } from "@/lib/toast";
+import { LoadingAnimation } from "@/components/loading-animation";
+import { toast, toastAction } from "@/lib/toast";
import { api } from "@/trpc/client";
interface DataTableRowActionsProps<TData> {
@@ -27,7 +37,8 @@ export function DataTableRowActions<TData>({
}: DataTableRowActionsProps<TData>) {
const incident = selectIncidentSchema.parse(row.original);
const router = useRouter();
- const [_isPending, startTransition] = React.useTransition();
+ const [isPending, startTransition] = React.useTransition();
+ const [alertOpen, setAlertOpen] = React.useState(false);
async function resolved() {
startTransition(async () => {
@@ -45,12 +56,6 @@ export function DataTableRowActions<TData>({
async function acknowledge() {
startTransition(async () => {
try {
- // const { jobType, ...rest } = monitor;
- // if (!monitor.id) return;
- // await api.monitor.update.mutate({
- // ...rest,
- // active: !monitor.active,
- // });
if (!incident.id) return;
await api.incident.acknowledgeIncident.mutate({ id: incident.id });
toastAction("success");
@@ -61,35 +66,77 @@ export function DataTableRowActions<TData>({
});
}
- return (
- <DropdownMenu>
- <DropdownMenuTrigger asChild>
- <Button
- variant="ghost"
- className="data-[state=open]:bg-accent h-8 w-8 p-0"
- >
- <span className="sr-only">Open menu</span>
- <MoreHorizontal className="h-4 w-4" />
- </Button>
- </DropdownMenuTrigger>
- <DropdownMenuContent align="end">
- <DropdownMenuItem
- disabled={incident.acknowledgedAt !== null}
- onClick={acknowledge}
- >
- Acknowledge
- </DropdownMenuItem>
+ async function onDelete() {
+ startTransition(async () => {
+ try {
+ if (!incident.id) return;
+ await api.incident.delete.mutate({ id: incident.id });
+ toastAction("success");
+ setAlertOpen(false);
+ router.refresh();
+ } catch {
+ toastAction("error");
+ }
+ });
+ }
- <DropdownMenuSeparator />
- <DropdownMenuItem
- disabled={
- incident.resolvedAt !== null || incident.acknowledgedAt === null
- }
- onClick={resolved}
- >
- Resolved
- </DropdownMenuItem>
- </DropdownMenuContent>
- </DropdownMenu>
+ return (
+ <AlertDialog open={alertOpen} onOpenChange={(value) => setAlertOpen(value)}>
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button
+ variant="ghost"
+ className="data-[state=open]:bg-accent h-8 w-8 p-0"
+ >
+ <span className="sr-only">Open menu</span>
+ <MoreHorizontal className="h-4 w-4" />
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end">
+ <DropdownMenuItem
+ disabled={incident.acknowledgedAt !== null}
+ onClick={acknowledge}
+ >
+ Acknowledge
+ </DropdownMenuItem>
+ <DropdownMenuItem
+ disabled={
+ incident.resolvedAt !== null || incident.acknowledgedAt === null
+ }
+ onClick={resolved}
+ >
+ Resolved
+ </DropdownMenuItem>
+ <DropdownMenuSeparator />
+ <AlertDialogTrigger asChild>
+ <DropdownMenuItem className="text-destructive focus:bg-destructive focus:text-background">
+ Delete
+ </DropdownMenuItem>
+ </AlertDialogTrigger>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ <AlertDialogContent>
+ <AlertDialogHeader>
+ <AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
+ <AlertDialogDescription>
+ This action cannot be undone. This will permanently delete the
+ monitor.
+ </AlertDialogDescription>
+ </AlertDialogHeader>
+ <AlertDialogFooter>
+ <AlertDialogCancel>Cancel</AlertDialogCancel>
+ <AlertDialogAction
+ onClick={(e) => {
+ e.preventDefault();
+ onDelete();
+ }}
+ disabled={isPending}
+ className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
+ >
+ {!isPending ? "Delete" : <LoadingAnimation />}
+ </AlertDialogAction>
+ </AlertDialogFooter>
+ </AlertDialogContent>
+ </AlertDialog>
);
}
diff --git a/packages/api/src/router/incident.ts b/packages/api/src/router/incident.ts
index 9de7cfde6..52645714b 100644
--- a/packages/api/src/router/incident.ts
+++ b/packages/api/src/router/incident.ts
@@ -112,4 +112,25 @@ export const incidentRouter = createTRPCRouter({
);
return true;
}),
+
+ delete: protectedProcedure
+ .input(z.object({ id: z.number() }))
+ .mutation(async (opts) => {
+ const incidentToDelete = await opts.ctx.db
+ .select()
+ .from(schema.incidentTable)
+ .where(
+ and(
+ eq(schema.incidentTable.id, opts.input.id),
+ eq(schema.incidentTable.workspaceId, opts.ctx.workspace.id),
+ ),
+ )
+ .get();
+ if (!incidentToDelete) return;
+
+ await opts.ctx.db
+ .delete(schema.incidentTable)
+ .where(eq(schema.incidentTable.id, incidentToDelete.id))
+ .run();
+ }),
});