Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: on delete cascade #1205

Merged
merged 6 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions apps/web/src/app/status-page/[domain]/badge/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ export async function GET(
const { status } = await getStatus(params.domain);
const theme = req.nextUrl.searchParams.get("theme");
const size = req.nextUrl.searchParams.get("size");
let s = SIZE.sm;
if (size) {
if (SIZE[size]) {
s = SIZE[size];
}
}
const s = SIZE[size ?? "sm"] ?? SIZE.sm;
const { label, color } = statusDictionary[status];
const light = "border-gray-200 text-gray-700 bg-white";
const dark = "border-gray-800 text-gray-300 bg-gray-900";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,34 @@ import {
AlertDialogTitle,
AlertDialogTrigger,
Button,
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
Label,
RadioGroup,
RadioGroupItem,
} from "@openstatus/ui";

import { LoadingAnimation } from "@/components/loading-animation";
import { useCopyToClipboard } from "@/hooks/use-copy-to-clipboard";
import { toastAction } from "@/lib/toast";
import { api } from "@/trpc/client";

export const SIZE: Record<string, { width: number; height: number }> = {
sm: { width: 120, height: 34 },
md: { width: 160, height: 46 },
lg: { width: 200, height: 56 },
xl: { width: 240, height: 68 },
};

interface DataTableRowActionsProps<TData> {
row: Row<TData>;
}
Expand All @@ -40,6 +57,9 @@ export function DataTableRowActions<TData>({
const router = useRouter();
const [alertOpen, setAlertOpen] = React.useState(false);
const [isPending, startTransition] = React.useTransition();
const [size, setSize] = React.useState<"sm" | "md" | "lg" | "xl">("sm");
const [theme, setTheme] = React.useState<"light" | "dark">("light");
const { copy } = useCopyToClipboard();

async function onDelete() {
startTransition(async () => {
Expand All @@ -56,65 +76,140 @@ export function DataTableRowActions<TData>({
}

return (
<AlertDialog open={alertOpen} onOpenChange={(value) => setAlertOpen(value)}>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="ghost"
className="h-8 w-8 p-0 data-[state=open]:bg-accent"
>
<span className="sr-only">Open menu</span>
<MoreHorizontal className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<Link href={`./status-pages/${page.id}/edit`}>
<DropdownMenuItem>Edit</DropdownMenuItem>
</Link>
<Link
href={
process.env.NODE_ENV === "production"
? `https://${page.slug}.openstatus.dev`
: `/status-page/${page.slug}`
}
target="_blank"
>
<DropdownMenuItem>Visit</DropdownMenuItem>
</Link>
<DropdownMenuSeparator />
<Link href={`./status-pages/${page.id}/reports/new`}>
<DropdownMenuItem>Create Report</DropdownMenuItem>
</Link>
<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"
<Dialog>
<AlertDialog
open={alertOpen}
onOpenChange={(value) => setAlertOpen(value)}
>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="ghost"
className="h-8 w-8 p-0 data-[state=open]:bg-accent"
>
<span className="sr-only">Open menu</span>
<MoreHorizontal className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<Link href={`./status-pages/${page.id}/edit`}>
<DropdownMenuItem>Edit</DropdownMenuItem>
</Link>
<Link
href={
process.env.NODE_ENV === "production"
? `https://${page.slug}.openstatus.dev`
: `/status-page/${page.slug}`
}
target="_blank"
>
<DropdownMenuItem>Visit</DropdownMenuItem>
</Link>
<DropdownMenuSeparator />
<Link href={`./status-pages/${page.id}/reports/new`}>
<DropdownMenuItem>Create Report</DropdownMenuItem>
</Link>
<DialogTrigger asChild>
<DropdownMenuItem>Create Badge</DropdownMenuItem>
</DialogTrigger>
<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>
<DialogContent>
<DialogHeader>
<DialogTitle>Create Badge</DialogTitle>
</DialogHeader>
<DialogDescription>
Create an uptime badge for your status page.
</DialogDescription>
<div className="flex items-center justify-center">
<div className="flex items-center justify-center p-4 border rounded-md w-full">
<img
src={`/status-page/${page.slug}/badge?size=${size}&theme=${theme}`}
alt="Badge"
width={SIZE[size].width}
height={SIZE[size].height}
/>
</div>
</div>
<div className="flex items-center justify-between">
<RadioGroup
className="flex"
onValueChange={(value) =>
setSize(value as "sm" | "md" | "lg" | "xl")
}
value={size}
>
{Object.keys(SIZE).map((size) => (
<div className="flex items-center space-x-2" key={size}>
<RadioGroupItem value={size} id={size} />
<Label htmlFor={size}>{size}</Label>
</div>
))}
</RadioGroup>
<div>
<span className="text-sm text-muted-foreground font-mono">
{SIZE[size].width}x{SIZE[size].height}
</span>
</div>
</div>
<RadioGroup
className="flex"
onValueChange={(value) => setTheme(value as "light" | "dark")}
value={theme}
>
{!isPending ? "Delete" : <LoadingAnimation />}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
{["light", "dark"].map((theme) => (
<div className="flex items-center space-x-2" key={theme}>
<RadioGroupItem value={theme} id={theme} />
<Label htmlFor={theme}>{theme}</Label>
</div>
))}
</RadioGroup>
<div className="flex items-center justify-center">
<Button
variant="outline"
className="w-full"
size="sm"
onClick={() =>
copy(
`https://openstatus.dev/status-page/${page.slug}/badge?size=${size}&theme=${theme}`,
{ withToast: true },
)
}
>
https://openstatus.dev/status-page/{page.slug}/badge?size={size}
&theme={theme}
</Button>
</div>
</DialogContent>
</AlertDialog>
</Dialog>
);
}
9 changes: 9 additions & 0 deletions apps/web/src/components/forms/status-page/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { api } from "@/trpc/client";
import { SaveButton } from "../shared/save-button";
import { General } from "./general";
import { SectionAdvanced } from "./section-advanced";
import { SectionDanger } from "./section-danger";
import { SectionMonitor } from "./section-monitor";
import { SectionVisibility } from "./section-visibility";

Expand Down Expand Up @@ -183,6 +184,9 @@ export function StatusPageForm({
</TabsTrigger>
<TabsTrigger value="advanced">Advanced</TabsTrigger>
<TabsTrigger value="visibility">Visibility</TabsTrigger>
{defaultValues?.id ? (
<TabsTrigger value="danger">Danger</TabsTrigger>
) : null}
</TabsList>
<TabsContent value="monitors">
<SectionMonitor form={form} monitors={allMonitors} />
Expand All @@ -193,6 +197,11 @@ export function StatusPageForm({
<TabsContent value="visibility">
<SectionVisibility {...{ form, plan, workspaceSlug }} />
</TabsContent>
{defaultValues?.id ? (
<TabsContent value="danger">
<SectionDanger pageId={defaultValues.id} />
</TabsContent>
) : null}
</Tabs>
<SaveButton
isPending={isPending}
Expand Down
92 changes: 92 additions & 0 deletions apps/web/src/components/forms/status-page/section-danger.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"use client";

import { useRouter } from "next/navigation";

import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
Button,
FormDescription,
} from "@openstatus/ui";

import { LoadingAnimation } from "@/components/loading-animation";
import { toastAction } from "@/lib/toast";
import { api } from "@/trpc/client";
import React from "react";
import { SectionHeader } from "../shared/section-header";

interface Props {
pageId: number;
}

export function SectionDanger({ pageId }: Props) {
const router = useRouter();
const [open, setOpen] = React.useState(false);
const [isPending, startTransition] = React.useTransition();

async function onDelete() {
startTransition(async () => {
try {
await api.page.delete.mutate({ id: pageId });
toastAction("deleted");
setOpen(false);
router.push("../");
} catch {
toastAction("error");
}
});
}

return (
<div className="grid w-full gap-4">
<SectionHeader
title="Danger Zone"
description="Be aware of the changes you are about to make."
/>
<div className="grid gap-4 sm:grid-cols-3">
<div className="col-start-1 flex flex-col items-center gap-4 sm:col-span-2 sm:flex-row">
<AlertDialog open={open} onOpenChange={setOpen}>
<AlertDialogTrigger asChild>
<Button variant="destructive" className="w-full sm:w-auto">
Delete
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
<AlertDialogDescription>
This action cannot be undone. This will permanently delete the
status page.
</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>
<FormDescription className="order-1 text-red-500 sm:order-2">
This action cannot be undone. This will permanently delete the
status page.
</FormDescription>
</div>
</div>
</div>
);
}
Loading
Loading