Files
Checkmate/client/src/Hooks/useStatusPageForm.ts
T

50 lines
1.7 KiB
TypeScript
Raw Normal View History

2026-02-02 23:46:15 +00:00
import { useMemo } from "react";
import { statusPageSchema, type StatusPageFormData } from "@/Validation/statusPage";
import type { StatusPage } from "@/Types/StatusPage";
import {
DEFAULT_STATUS_PAGE_THEME,
DEFAULT_STATUS_PAGE_THEME_MODE,
} from "@/Types/StatusPage";
2026-02-03 19:59:42 +00:00
import type { Monitor } from "@/Types/Monitor";
2026-02-02 23:46:15 +00:00
interface UseStatusPageFormOptions {
data?: StatusPage | null;
2026-02-03 19:59:42 +00:00
monitors?: Monitor[] | null;
2026-02-02 23:46:15 +00:00
}
2026-02-02 23:57:14 +00:00
const generateDefaultUrl = () => Math.floor(Math.random() * 1000000).toString();
2026-02-03 19:59:42 +00:00
const transformLogo = (logo: StatusPage["logo"]): StatusPageFormData["logo"] => {
if (!logo || !logo.data) return null;
return {
data: `data:${logo.contentType};base64,${logo.data}`,
contentType: logo.contentType,
};
};
export const useStatusPageForm = ({
data = null,
monitors = null,
}: UseStatusPageFormOptions = {}) => {
2026-02-02 23:46:15 +00:00
return useMemo(() => {
const defaults: StatusPageFormData = {
companyName: data?.companyName || "",
2026-02-02 23:57:14 +00:00
url: data?.url || generateDefaultUrl(),
2026-02-02 23:46:15 +00:00
timezone: data?.timezone || Intl.DateTimeFormat().resolvedOptions().timeZone,
2026-03-16 17:46:13 -04:00
type: data?.type || ["uptime"],
color: data?.color || "#13715B",
2026-02-03 22:07:59 +00:00
monitors: data?.monitors || [],
2026-02-02 23:46:15 +00:00
isPublished: data?.isPublished ?? false,
showCharts: data?.showCharts ?? true,
showUptimePercentage: data?.showUptimePercentage ?? true,
showAdminLoginLink: data?.showAdminLoginLink ?? false,
2026-03-16 17:46:13 -04:00
showInfrastructure: data?.showInfrastructure ?? false,
2026-02-02 23:46:15 +00:00
customCSS: data?.customCSS || "",
theme: data?.theme ?? DEFAULT_STATUS_PAGE_THEME,
themeMode: data?.themeMode ?? DEFAULT_STATUS_PAGE_THEME_MODE,
2026-02-03 19:59:42 +00:00
logo: transformLogo(data?.logo),
2026-02-02 23:46:15 +00:00
};
return { schema: statusPageSchema, defaults };
2026-02-03 19:59:42 +00:00
}, [data, monitors]);
2026-02-02 23:46:15 +00:00
};