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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | 1x 1x 1x 1x 1x 1x 1x 1x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 1x 1x 1x 37x 1x 1x 1x 37x 1x 1x 37x 1x 1x 37x 73x 73x 73x 73x 73x 73x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 3x 3x 3x 3x 3x 3x 3x 3x 3x 37x 3x 3x 3x 3x 3x 3x 37x 1x 1x 1x 1x 1x 1x 1x 1x 37x 1x 1x 1x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 2x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 37x 37x 37x | import { useState } from "react";
import DashboardShell from "../components/layout/DashboardShell";
import MonitorList from "../components/monitors/MonitorList";
import MonitorForm from "../components/monitors/MonitorForm";
import EditMonitorModal from "../components/monitors/EditMonitorModal";
import {
useMonitors,
useCreateMonitor,
useUpdateMonitor,
useDeleteMonitor,
} from "../hooks/useMonitors";
import type {
MonitorCreate,
MonitorUpdate,
MonitorRead,
} from "../types/monitor";
import { useMonitorStore } from "../stores/monitorStore";
const PAGE_SIZE = 10;
export default function Dashboard() {
const [showForm, setShowForm] = useState(false);
const [editingMonitor, setEditingMonitor] = useState<MonitorRead | null>(
null,
);
const [page, setPage] = useState(0);
const {
data: response,
isLoading,
error,
} = useMonitors(page * PAGE_SIZE, PAGE_SIZE);
const createMonitor = useCreateMonitor();
const updateMonitor = useUpdateMonitor();
const deleteMonitor = useDeleteMonitor();
const { searchQuery, statusFilter, setSearchQuery, setStatusFilter } =
useMonitorStore();
const monitors = response?.items ?? [];
const totalItems = response?.total ?? 0;
const totalPages = Math.max(1, Math.ceil(totalItems / PAGE_SIZE));
const handleCreate = async (data: MonitorCreate) => {
await createMonitor.mutateAsync(data);
setShowForm(false);
};
const handleEdit = (id: string, data: MonitorUpdate) => {
updateMonitor.mutate({ id, data });
setEditingMonitor(null);
};
const handleToggle = (id: string, isActive: boolean) => {
updateMonitor.mutate({ id, data: { is_active: isActive } });
};
const handleDelete = async (id: string) => {
await deleteMonitor.mutateAsync(id);
};
const filteredMonitors = monitors.filter((m) => {
const matchesSearch = m.url
.toLowerCase()
.includes(searchQuery.toLowerCase());
const matchesStatus =
statusFilter === "ALL" || m.alert_status === statusFilter;
return matchesSearch && matchesStatus;
});
const upCount = monitors.filter((m) => m.alert_status === "UP").length;
const downCount = monitors.filter((m) => m.alert_status === "DOWN").length;
return (
<DashboardShell>
<div className="space-y-6">
<div className="flex items-center justify-between">
<div>
<h1 className="text-2xl font-bold text-gray-900">Monitors</h1>
<div className="flex items-center gap-3 mt-1">
<span className="text-sm font-medium text-green-600">
{upCount} UP
</span>
<span className="text-sm font-medium text-red-600">
{downCount} DOWN
</span>
</div>
</div>
<button
onClick={() => setShowForm(true)}
className="rounded-md bg-blue-600 px-4 py-2 text-sm font-medium text-white hover:bg-blue-700 cursor-pointer"
>
+ Add Monitor
</button>
</div>
{showForm && (
<div className="rounded-lg border border-gray-200 bg-white p-6 shadow-sm">
<h2 className="text-lg font-semibold text-gray-900 mb-4">
New Monitor
</h2>
<MonitorForm
onSubmit={handleCreate}
onCancel={() => setShowForm(false)}
isLoading={createMonitor.isPending}
/>
</div>
)}
{editingMonitor && (
<EditMonitorModal
monitor={editingMonitor}
onSave={handleEdit}
onClose={() => setEditingMonitor(null)}
isLoading={updateMonitor.isPending}
/>
)}
{error && (
<div className="rounded-lg bg-red-50 border border-red-200 p-4 text-sm text-red-700">
Failed to load monitors.{" "}
<button
onClick={() => window.location.reload()}
className="underline"
>
Retry
</button>
</div>
)}
{isLoading ? (
<div className="flex justify-center py-12">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600" />
</div>
) : (
<>
<div className="flex gap-4 mb-4">
<input
type="text"
placeholder="Search URLs..."
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
className="rounded-md border border-gray-300 px-3 py-1.5 text-sm w-64 focus:ring-2 focus:ring-blue-500 focus:outline-none"
/>
<select
value={statusFilter}
onChange={(e) =>
setStatusFilter(e.target.value as "ALL" | "UP" | "DOWN")
}
className="rounded-md border border-gray-300 px-3 py-1.5 text-sm focus:ring-2 focus:ring-blue-500 focus:outline-none cursor-pointer"
>
<option value="ALL">All Statuses</option>
<option value="UP">Up Only</option>
<option value="DOWN">Down Only</option>
</select>
</div>
<MonitorList
monitors={filteredMonitors}
onDelete={handleDelete}
onToggle={handleToggle}
onEdit={(m) => setEditingMonitor(m)}
/>
{/* Pagination Controls */}
<div className="flex justify-between items-center mt-4">
<button
onClick={() => setPage((p) => Math.max(0, p - 1))}
disabled={page === 0}
className="px-3 py-1 border rounded text-sm disabled:opacity-50 enabled:hover:bg-gray-100 enabled:cursor-pointer cursor-default transition-colors"
>
Previous
</button>
<span className="text-sm text-gray-500">
Page {page + 1} of {totalPages}
</span>
<button
onClick={() => setPage((p) => p + 1)}
disabled={page >= totalPages - 1}
className="px-3 py-1 border rounded text-sm disabled:opacity-50 enabled:hover:bg-gray-100 enabled:cursor-pointer cursor-default transition-colors"
>
Next
</button>
</div>
</>
)}
</div>
</DashboardShell>
);
}
|