update usePatch, implement actions

This commit is contained in:
Alex Holliday committed 2025-10-08 14:28:51 -07:00
1 parent aae1fefb31
commit f9d0ab5e07
7 files changed
+93 -43

No files matched your search

@@ -15,11 +15,13 @@ export const ActionsMenu = ({ items }: { items: ActionMenuItem[] }) => {
const [anchorEl, setAnchorEl] = useState<null | any>(null); const [anchorEl, setAnchorEl] = useState<null | any>(null);
const open = Boolean(anchorEl); const open = Boolean(anchorEl);
const handleClick = (event: React.MouseEvent<any>) => { const handleClick = (e: React.MouseEvent<any>) => {
setAnchorEl(event.currentTarget); e.stopPropagation();
setAnchorEl(e.currentTarget);
}; };
const handleClose = () => { const handleClose = (e: React.MouseEvent<any>) => {
e.stopPropagation();
setAnchorEl(null); setAnchorEl(null);
}; };
@@ -36,8 +38,9 @@ export const ActionsMenu = ({ items }: { items: ActionMenuItem[] }) => {
{items.map((item) => ( {items.map((item) => (
<MenuItem <MenuItem
key={item.id} key={item.id}
onClick={() => { onClick={(e: React.MouseEvent<HTMLLIElement>) => {
if (item.closeMenu) handleClose(); e.stopPropagation();
if (item.closeMenu) handleClose(e);
item.action(); item.action();
}} }}
> >
@@ -28,12 +28,16 @@ export type Header<T> = {
type DataTableProps<T extends { id?: string | number; _id?: string | number }> = { type DataTableProps<T extends { id?: string | number; _id?: string | number }> = {
headers: Header<T>[]; headers: Header<T>[];
data: T[]; data: T[];
onRowClick?: (row: T) => void;
}; };
export function DataTable<T extends { id?: string | number; _id?: string | number }>({ export function DataTable<
headers, T extends {
data, id?: string | number;
}: DataTableProps<T>) { _id?: string | number;
onRowClick?: (row: T) => void;
},
>({ headers, data, onRowClick }: DataTableProps<T>) {
const theme = useTheme(); const theme = useTheme();
if (data.length === 0 || headers.length === 0) return <div>No data</div>; if (data.length === 0 || headers.length === 0) return <div>No data</div>;
return ( return (
@@ -77,7 +81,11 @@ export function DataTable<T extends { id?: string | number; _id?: string | numbe
const key = row.id || row._id || Math.random(); const key = row.id || row._id || Math.random();
return ( return (
<TableRow key={key}> <TableRow
key={key}
sx={{ cursor: onRowClick ? "pointer" : "default" }}
onClick={() => (onRowClick ? onRowClick(row) : null)}
>
{headers.map((header, index) => { {headers.map((header, index) => {
return ( return (
<TableCell <TableCell
@@ -50,7 +50,7 @@ export const HeaderControls = ({
<Button <Button
loading={isPatching} loading={isPatching}
onClick={async () => { onClick={async () => {
await patch(); await patch(`/monitors/${monitor._id}/active`);
refetch(); refetch();
}} }}
startIcon={ startIcon={
+6 -2
View File
@@ -58,11 +58,15 @@ export const usePost = <B = any, R = any>(endpoint: string) => {
return { post: postFn, loading, error }; return { post: postFn, loading, error };
}; };
export const usePatch = <B = any, R = any>(endpoint: string) => { export const usePatch = <B = any, R = any>() => {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const patchFn = async (body: B, config?: AxiosRequestConfig): Promise<R | null> => { const patchFn = async (
endpoint: string,
body?: B,
config?: AxiosRequestConfig
): Promise<R | null> => {
setLoading(true); setLoading(true);
setError(null); setError(null);
+1 -5
View File
@@ -52,11 +52,7 @@ const UptimeDetailsPage = () => {
{} {}
); );
const { const { patch, loading: isPatching, error: postError } = usePatch<ApiResponse>();
patch,
loading: isPatching,
error: postError,
} = usePatch<ApiResponse>(`/monitors/${id}/active`);
const monitor: IMonitor = response?.data?.monitor; const monitor: IMonitor = response?.data?.monitor;
+33 -19
View File
@@ -10,20 +10,36 @@ import { useTranslation } from "react-i18next";
import { useMediaQuery } from "@mui/material"; import { useMediaQuery } from "@mui/material";
import { useTheme } from "@mui/material/styles"; import { useTheme } from "@mui/material/styles";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { usePatch } from "@/Hooks/v2/UseApi";
import type { ApiResponse } from "@/Hooks/v2/UseApi";
import type { IMonitor } from "@/Types/Monitor"; import type { IMonitor } from "@/Types/Monitor";
import type { ActionMenuItem } from "@/Components/v2/ActionsMenu"; import type { ActionMenuItem } from "@/Components/v2/ActionsMenu";
const getActions = (
theme: any, export const MonitorTable = ({
monitor: IMonitor, monitors,
navigate: Function refetch,
): ActionMenuItem[] => { }: {
monitors: IMonitor[];
refetch: Function;
}) => {
const { t } = useTranslation();
const theme = useTheme();
const isSmall = useMediaQuery(theme.breakpoints.down("md"));
const navigate = useNavigate();
const {
patch,
// loading: isPatching,
// error: postError,
} = usePatch<ApiResponse>();
const getActions = (monitor: IMonitor): ActionMenuItem[] => {
return [ return [
{ {
id: 1, id: 1,
label: "Open site", label: "Open site",
action: () => { action: () => {
console.log("Open site"); window.open(monitor.url, "_blank", "noreferrer");
}, },
closeMenu: true, closeMenu: true,
}, },
@@ -38,7 +54,7 @@ const getActions = (
id: 3, id: 3,
label: "Incidents", label: "Incidents",
action: () => { action: () => {
console.log("Open incidents"); navigate(`/v2/incidents/${monitor._id}`);
}, },
}, },
{ {
@@ -57,9 +73,10 @@ const getActions = (
}, },
{ {
id: 6, id: 6,
label: "Pause", label: monitor.isActive ? "Pause" : "Resume",
action: () => { action: async () => {
console.log("Open pause"); await patch(`/monitors/${monitor._id}/active`);
refetch();
}, },
closeMenu: true, closeMenu: true,
}, },
@@ -74,7 +91,7 @@ const getActions = (
]; ];
}; };
const getHeaders = (theme: any, t: Function, navigate: Function) => { const getHeaders = () => {
const headers: Header<IMonitor>[] = [ const headers: Header<IMonitor>[] = [
{ {
id: "name", id: "name",
@@ -112,20 +129,14 @@ const getHeaders = (theme: any, t: Function, navigate: Function) => {
id: "actions", id: "actions",
content: t("actions"), content: t("actions"),
render: (row) => { render: (row) => {
return <ActionsMenu items={getActions(theme, row, navigate)} />; return <ActionsMenu items={getActions(row)} />;
}, },
}, },
]; ];
return headers; return headers;
}; };
export const MonitorTable = ({ monitors }: { monitors: IMonitor[] }) => { let headers = getHeaders();
const { t } = useTranslation();
const theme = useTheme();
const isSmall = useMediaQuery(theme.breakpoints.down("md"));
const navigate = useNavigate();
let headers = getHeaders(theme, t, navigate);
if (isSmall) { if (isSmall) {
headers = headers.filter((h) => h.id !== "histogram"); headers = headers.filter((h) => h.id !== "histogram");
@@ -134,6 +145,9 @@ export const MonitorTable = ({ monitors }: { monitors: IMonitor[] }) => {
<Table <Table
headers={headers} headers={headers}
data={monitors} data={monitors}
onRowClick={(row) => {
navigate(row._id);
}}
/> />
); );
}; };
+31 -6
View File
@@ -18,8 +18,30 @@ const UptimeMonitors = () => {
const theme = useTheme(); const theme = useTheme();
const isSmall = useMediaQuery(theme.breakpoints.down("md")); const isSmall = useMediaQuery(theme.breakpoints.down("md"));
const { response, loading } = useGet<ApiResponse>("/monitors?embedChecks=true", {}); const { response, loading, refetch } = useGet<ApiResponse>(
const monitors = response?.data ?? ([] as IMonitor[]); "/monitors?embedChecks=true",
{},
{ refreshInterval: 30000 }
);
const monitors: IMonitor[] = response?.data ?? ([] as IMonitor[]);
const monitorStatuses = monitors.reduce(
(acc, monitor) => {
if (monitor.status === "up") {
acc.up += 1;
} else if (monitor.status === "down") {
acc.down += 1;
} else if (monitor.isActive === false) {
acc.paused += 1;
}
return acc;
},
{
up: 0,
down: 0,
paused: 0,
}
);
if (monitors.length === 0 && !loading) { if (monitors.length === 0 && !loading) {
return "No monitors found"; return "No monitors found";
@@ -35,11 +57,14 @@ const UptimeMonitors = () => {
direction={isSmall ? "column" : "row"} direction={isSmall ? "column" : "row"}
gap={theme.spacing(8)} gap={theme.spacing(8)}
> >
<UpStatusBox n={1} /> <UpStatusBox n={monitorStatuses.up} />
<DownStatusBox n={1} /> <DownStatusBox n={monitorStatuses.down} />
<PausedStatusBox n={1} /> <PausedStatusBox n={monitorStatuses.paused} />
</Stack> </Stack>
<MonitorTable monitors={monitors} /> <MonitorTable
monitors={monitors}
refetch={refetch}
/>
</BasePage> </BasePage>
); );
}; };