|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { |
| 4 | + DotsThreeIcon, |
| 5 | + GlobeIcon, |
| 6 | + HeartbeatIcon, |
| 7 | + PencilIcon, |
| 8 | + TrashIcon, |
| 9 | +} from "@phosphor-icons/react"; |
| 10 | +import { useMutation } from "@tanstack/react-query"; |
| 11 | +import Link from "next/link"; |
| 12 | +import { useState } from "react"; |
| 13 | +import { toast } from "sonner"; |
| 14 | +import { Badge } from "@/components/ui/badge"; |
| 15 | +import { Button } from "@/components/ui/button"; |
| 16 | +import { |
| 17 | + DropdownMenu, |
| 18 | + DropdownMenuContent, |
| 19 | + DropdownMenuItem, |
| 20 | + DropdownMenuTrigger, |
| 21 | +} from "@/components/ui/dropdown-menu"; |
| 22 | +import { orpc } from "@/lib/orpc"; |
| 23 | + |
| 24 | +const granularityLabels: Record<string, string> = { |
| 25 | + minute: "Every minute", |
| 26 | + ten_minutes: "Every 10 minutes", |
| 27 | + thirty_minutes: "Every 30 minutes", |
| 28 | + hour: "Hourly", |
| 29 | + six_hours: "Every 6 hours", |
| 30 | + twelve_hours: "Every 12 hours", |
| 31 | + day: "Daily", |
| 32 | +}; |
| 33 | + |
| 34 | +type MonitorRowProps = { |
| 35 | + schedule: { |
| 36 | + id: string; |
| 37 | + websiteId: string | null; |
| 38 | + url: string | null; |
| 39 | + name: string | null; |
| 40 | + granularity: string; |
| 41 | + cron: string; |
| 42 | + isPaused: boolean; |
| 43 | + createdAt: Date | string; |
| 44 | + updatedAt: Date | string; |
| 45 | + website: { |
| 46 | + id: string; |
| 47 | + name: string | null; |
| 48 | + domain: string; |
| 49 | + } | null; |
| 50 | + }; |
| 51 | + onEdit: () => void; |
| 52 | + onDelete: () => void; |
| 53 | + onRefetch: () => void; |
| 54 | +}; |
| 55 | + |
| 56 | +export function MonitorRow({ |
| 57 | + schedule, |
| 58 | + onEdit, |
| 59 | + onDelete, |
| 60 | + onRefetch, |
| 61 | +}: MonitorRowProps) { |
| 62 | + const [isPausing, setIsPausing] = useState(false); |
| 63 | + |
| 64 | + const pauseMutation = useMutation({ |
| 65 | + ...orpc.uptime.pauseSchedule.mutationOptions(), |
| 66 | + }); |
| 67 | + const resumeMutation = useMutation({ |
| 68 | + ...orpc.uptime.resumeSchedule.mutationOptions(), |
| 69 | + }); |
| 70 | + const deleteMutation = useMutation({ |
| 71 | + ...orpc.uptime.deleteSchedule.mutationOptions(), |
| 72 | + }); |
| 73 | + |
| 74 | + const handleTogglePause = async () => { |
| 75 | + setIsPausing(true); |
| 76 | + try { |
| 77 | + if (schedule.isPaused) { |
| 78 | + await resumeMutation.mutateAsync({ scheduleId: schedule.id }); |
| 79 | + toast.success("Monitor resumed"); |
| 80 | + } else { |
| 81 | + await pauseMutation.mutateAsync({ scheduleId: schedule.id }); |
| 82 | + toast.success("Monitor paused"); |
| 83 | + } |
| 84 | + onRefetch(); |
| 85 | + } catch (error) { |
| 86 | + const errorMessage = |
| 87 | + error instanceof Error ? error.message : "Failed to update monitor"; |
| 88 | + toast.error(errorMessage); |
| 89 | + } finally { |
| 90 | + setIsPausing(false); |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + const handleDelete = async () => { |
| 95 | + try { |
| 96 | + await deleteMutation.mutateAsync({ scheduleId: schedule.id }); |
| 97 | + toast.success("Monitor deleted"); |
| 98 | + onDelete(); |
| 99 | + } catch (error) { |
| 100 | + const errorMessage = |
| 101 | + error instanceof Error ? error.message : "Failed to delete monitor"; |
| 102 | + toast.error(errorMessage); |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + const isWebsiteMonitor = !!schedule.websiteId; |
| 107 | + const displayName = isWebsiteMonitor |
| 108 | + ? schedule.website?.name || schedule.website?.domain || "Unknown" |
| 109 | + : schedule.name || schedule.url || "Unknown"; |
| 110 | + const displayUrl = isWebsiteMonitor ? schedule.website?.domain : schedule.url; |
| 111 | + |
| 112 | + return ( |
| 113 | + <div className="flex items-center gap-4 border-b p-4 transition-colors last:border-b-0 hover:bg-accent/50"> |
| 114 | + <div className="flex size-10 shrink-0 items-center justify-center rounded border bg-secondary-brighter"> |
| 115 | + <HeartbeatIcon |
| 116 | + className="text-accent-foreground" |
| 117 | + size={20} |
| 118 | + weight="duotone" |
| 119 | + /> |
| 120 | + </div> |
| 121 | + <div className="min-w-0 flex-1"> |
| 122 | + {isWebsiteMonitor ? ( |
| 123 | + <Link |
| 124 | + className="group block" |
| 125 | + href={`/websites/${schedule.websiteId}/pulse`} |
| 126 | + > |
| 127 | + <h3 className="truncate font-semibold text-base text-foreground transition-colors group-hover:text-primary"> |
| 128 | + {displayName} |
| 129 | + </h3> |
| 130 | + </Link> |
| 131 | + ) : ( |
| 132 | + <h3 className="truncate font-semibold text-base text-foreground"> |
| 133 | + {displayName} |
| 134 | + </h3> |
| 135 | + )} |
| 136 | + <div className="mt-1 flex items-center gap-4 text-muted-foreground text-sm"> |
| 137 | + <div className="flex items-center gap-1.5"> |
| 138 | + <GlobeIcon className="size-3.5 shrink-0" weight="duotone" /> |
| 139 | + <span className="truncate">{displayUrl}</span> |
| 140 | + </div> |
| 141 | + <span>•</span> |
| 142 | + <span> |
| 143 | + {granularityLabels[schedule.granularity] || schedule.granularity} |
| 144 | + </span> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + <Badge |
| 148 | + className={ |
| 149 | + schedule.isPaused |
| 150 | + ? "border-amber-500/20 bg-amber-500/10 text-amber-600" |
| 151 | + : "border-green-500/20 bg-green-500/10 text-green-600" |
| 152 | + } |
| 153 | + variant={schedule.isPaused ? "secondary" : "default"} |
| 154 | + > |
| 155 | + {schedule.isPaused ? "Paused" : "Active"} |
| 156 | + </Badge> |
| 157 | + <DropdownMenu> |
| 158 | + <DropdownMenuTrigger asChild> |
| 159 | + <Button size="sm" variant="ghost"> |
| 160 | + <DotsThreeIcon size={20} /> |
| 161 | + </Button> |
| 162 | + </DropdownMenuTrigger> |
| 163 | + <DropdownMenuContent align="end"> |
| 164 | + <DropdownMenuItem onClick={onEdit}> |
| 165 | + <PencilIcon size={16} /> |
| 166 | + Edit |
| 167 | + </DropdownMenuItem> |
| 168 | + <DropdownMenuItem |
| 169 | + disabled={ |
| 170 | + isPausing || pauseMutation.isPending || resumeMutation.isPending |
| 171 | + } |
| 172 | + onClick={handleTogglePause} |
| 173 | + > |
| 174 | + <HeartbeatIcon size={16} /> |
| 175 | + {schedule.isPaused ? "Resume" : "Pause"} |
| 176 | + </DropdownMenuItem> |
| 177 | + <DropdownMenuItem |
| 178 | + className="text-destructive focus:text-destructive" |
| 179 | + disabled={deleteMutation.isPending} |
| 180 | + onClick={handleDelete} |
| 181 | + > |
| 182 | + <TrashIcon size={16} /> |
| 183 | + Delete |
| 184 | + </DropdownMenuItem> |
| 185 | + </DropdownMenuContent> |
| 186 | + </DropdownMenu> |
| 187 | + </div> |
| 188 | + ); |
| 189 | +} |
0 commit comments