Merge pull request #3692 from bluewave-labs/feat/fe/progress-indicator-status-page
Feat: Progress indicator status page
This commit is contained in:
2 files changed
+107
-12
No files matched your search
@@ -1,4 +1,4 @@
|
|||||||
import { BasePage, ConfigBox } from "@/Components/design-elements";
|
import { BasePage, ConfigBox, StepProgress } from "@/Components/design-elements";
|
||||||
import Stack from "@mui/material/Stack";
|
import Stack from "@mui/material/Stack";
|
||||||
import { logger } from "@/Utils/logger";
|
import { logger } from "@/Utils/logger";
|
||||||
import { SPACING, LAYOUT } from "@/Utils/Theme/constants";
|
import { SPACING, LAYOUT } from "@/Utils/Theme/constants";
|
||||||
@@ -26,6 +26,7 @@ import { useForm, Controller } from "react-hook-form";
|
|||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
import { useStatusPageForm } from "@/Hooks/useStatusPageForm";
|
import { useStatusPageForm } from "@/Hooks/useStatusPageForm";
|
||||||
import type { StatusPageFormData } from "@/Validation/statusPage";
|
import type { StatusPageFormData } from "@/Validation/statusPage";
|
||||||
|
import { statusPageStepCount, statusPageStepFieldsFor } from "@/Validation/statusPage";
|
||||||
import { useGet, usePost, usePut, useDelete } from "@/Hooks/UseApi";
|
import { useGet, usePost, usePut, useDelete } from "@/Hooks/UseApi";
|
||||||
import type { Monitor } from "@/Types/Monitor";
|
import type { Monitor } from "@/Types/Monitor";
|
||||||
import type { MonitorDisplayType, StatusPageResponse } from "@/Types/StatusPage";
|
import type { MonitorDisplayType, StatusPageResponse } from "@/Types/StatusPage";
|
||||||
@@ -86,13 +87,27 @@ const CreateStatusPage = () => {
|
|||||||
defaultValues: defaults,
|
defaultValues: defaults,
|
||||||
});
|
});
|
||||||
|
|
||||||
const { control, reset, handleSubmit } = form;
|
const { control, reset, handleSubmit, trigger } = form;
|
||||||
|
|
||||||
// Reset form when defaults change (from fetched data)
|
// Reset form when defaults change (from fetched data)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
reset(defaults);
|
reset(defaults);
|
||||||
}, [defaults, reset]);
|
}, [defaults, reset]);
|
||||||
|
|
||||||
|
// Multi-step wizard, create only. The configure (edit) flow keeps the flat
|
||||||
|
// form, where showStep() is always true.
|
||||||
|
const [currentStep, setCurrentStep] = useState(0);
|
||||||
|
const showStep = (step: number) => !isCreate || currentStep === step;
|
||||||
|
const totalSteps = statusPageStepCount();
|
||||||
|
|
||||||
|
const handleNext = async () => {
|
||||||
|
const isValid = await trigger(statusPageStepFieldsFor(currentStep));
|
||||||
|
if (isValid) {
|
||||||
|
setCurrentStep((step) => step + 1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const handleBack = () => setCurrentStep((step) => step - 1);
|
||||||
|
|
||||||
const watchedMonitorIds: string[] = form.watch("monitors") ?? [];
|
const watchedMonitorIds: string[] = form.watch("monitors") ?? [];
|
||||||
const computedTypes: MonitorDisplayType[] = useMemo(() => {
|
const computedTypes: MonitorDisplayType[] = useMemo(() => {
|
||||||
const selectedMonitors = (watchedMonitorIds ?? [])
|
const selectedMonitors = (watchedMonitorIds ?? [])
|
||||||
@@ -200,6 +215,13 @@ const CreateStatusPage = () => {
|
|||||||
onSubmit={handleSubmit(onSubmit, onError)}
|
onSubmit={handleSubmit(onSubmit, onError)}
|
||||||
>
|
>
|
||||||
{!isCreate && <HeaderConfigStatusControls onDelete={handleDeleteClick} />}
|
{!isCreate && <HeaderConfigStatusControls onDelete={handleDeleteClick} />}
|
||||||
|
{isCreate && (
|
||||||
|
<StepProgress
|
||||||
|
steps={totalSteps}
|
||||||
|
current={currentStep}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{showStep(0) && (
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.statusPages.form.access.title")}
|
title={t("pages.statusPages.form.access.title")}
|
||||||
subtitle={t("pages.statusPages.form.access.description")}
|
subtitle={t("pages.statusPages.form.access.description")}
|
||||||
@@ -225,6 +247,8 @@ const CreateStatusPage = () => {
|
|||||||
</Stack>
|
</Stack>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
|
{showStep(0) && (
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.statusPages.form.basicInfo.title")}
|
title={t("pages.statusPages.form.basicInfo.title")}
|
||||||
subtitle={t("pages.statusPages.form.basicInfo.description")}
|
subtitle={t("pages.statusPages.form.basicInfo.description")}
|
||||||
@@ -263,6 +287,8 @@ const CreateStatusPage = () => {
|
|||||||
</Stack>
|
</Stack>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
|
{showStep(0) && (
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.statusPages.form.monitors.title")}
|
title={t("pages.statusPages.form.monitors.title")}
|
||||||
subtitle={t("pages.statusPages.form.monitors.description")}
|
subtitle={t("pages.statusPages.form.monitors.description")}
|
||||||
@@ -375,6 +401,8 @@ const CreateStatusPage = () => {
|
|||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
|
{showStep(0) && (
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.statusPages.form.timezone.title")}
|
title={t("pages.statusPages.form.timezone.title")}
|
||||||
subtitle={t("pages.statusPages.form.timezone.description")}
|
subtitle={t("pages.statusPages.form.timezone.description")}
|
||||||
@@ -406,6 +434,8 @@ const CreateStatusPage = () => {
|
|||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
|
{showStep(1) && (
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.statusPages.form.appearance.title")}
|
title={t("pages.statusPages.form.appearance.title")}
|
||||||
subtitle={t("pages.statusPages.form.appearance.description")}
|
subtitle={t("pages.statusPages.form.appearance.description")}
|
||||||
@@ -447,6 +477,8 @@ const CreateStatusPage = () => {
|
|||||||
</Stack>
|
</Stack>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
|
{showStep(1) && (
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.statusPages.form.theme.title")}
|
title={t("pages.statusPages.form.theme.title")}
|
||||||
subtitle={t("pages.statusPages.form.theme.description")}
|
subtitle={t("pages.statusPages.form.theme.description")}
|
||||||
@@ -471,6 +503,8 @@ const CreateStatusPage = () => {
|
|||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
|
{showStep(1) && (
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.statusPages.form.features.title")}
|
title={t("pages.statusPages.form.features.title")}
|
||||||
subtitle={t("pages.statusPages.form.features.description")}
|
subtitle={t("pages.statusPages.form.features.description")}
|
||||||
@@ -545,11 +579,35 @@ const CreateStatusPage = () => {
|
|||||||
</Stack>
|
</Stack>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
<Stack
|
<Stack
|
||||||
direction="row"
|
direction="row"
|
||||||
justifyContent="flex-end"
|
justifyContent={isCreate ? "space-between" : "flex-end"}
|
||||||
>
|
>
|
||||||
|
{isCreate && (
|
||||||
<Button
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="outlined"
|
||||||
|
color="secondary"
|
||||||
|
disabled={currentStep === 0}
|
||||||
|
onClick={handleBack}
|
||||||
|
>
|
||||||
|
{t("common.buttons.back")}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
{isCreate && currentStep < totalSteps - 1 ? (
|
||||||
|
<Button
|
||||||
|
key="wizard-next"
|
||||||
|
type="button"
|
||||||
|
variant="contained"
|
||||||
|
color="primary"
|
||||||
|
onClick={handleNext}
|
||||||
|
>
|
||||||
|
{t("common.buttons.next")}
|
||||||
|
</Button>
|
||||||
|
) : (
|
||||||
|
<Button
|
||||||
|
key="wizard-save"
|
||||||
loading={isSubmitting}
|
loading={isSubmitting}
|
||||||
type="submit"
|
type="submit"
|
||||||
variant="contained"
|
variant="contained"
|
||||||
@@ -557,6 +615,7 @@ const CreateStatusPage = () => {
|
|||||||
>
|
>
|
||||||
{t("common.buttons.save")}
|
{t("common.buttons.save")}
|
||||||
</Button>
|
</Button>
|
||||||
|
)}
|
||||||
</Stack>
|
</Stack>
|
||||||
<Dialog
|
<Dialog
|
||||||
open={isDeleteDialogOpen}
|
open={isDeleteDialogOpen}
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import type { FieldPath } from "react-hook-form";
|
||||||
import { STATUS_PAGE_THEMES, STATUS_PAGE_THEME_MODES } from "@/Types/StatusPage";
|
import { STATUS_PAGE_THEMES, STATUS_PAGE_THEME_MODES } from "@/Types/StatusPage";
|
||||||
|
|
||||||
|
// Wizard step a field is validated on. Attached inline to each field below so
|
||||||
|
// the grouping lives next to the field definition; unannotated fields default
|
||||||
|
// to step 0. Read via `statusPageStepFieldsFor`. Mirrors the monitor schema's
|
||||||
|
// `monitorStepRegistry` so the create-status-page wizard can't drift from the
|
||||||
|
// field definitions.
|
||||||
|
export const statusPageStepRegistry = z.registry<{ step: number }>();
|
||||||
|
|
||||||
export const statusPageSchema = z.object({
|
export const statusPageSchema = z.object({
|
||||||
companyName: z
|
companyName: z
|
||||||
.string()
|
.string()
|
||||||
@@ -18,23 +26,51 @@ export const statusPageSchema = z.object({
|
|||||||
type: z
|
type: z
|
||||||
.array(z.enum(["uptime", "infrastructure"]))
|
.array(z.enum(["uptime", "infrastructure"]))
|
||||||
.min(1, "At least one type is required"),
|
.min(1, "At least one type is required"),
|
||||||
color: z.string().min(1, "Color is required"),
|
color: z.string().min(1, "Color is required").register(statusPageStepRegistry, {
|
||||||
|
step: 1,
|
||||||
|
}),
|
||||||
monitors: z.array(z.string()).min(1, "At least one monitor is required"),
|
monitors: z.array(z.string()).min(1, "At least one monitor is required"),
|
||||||
isPublished: z.boolean(),
|
isPublished: z.boolean(),
|
||||||
showCharts: z.boolean(),
|
showCharts: z.boolean().register(statusPageStepRegistry, { step: 1 }),
|
||||||
showUptimePercentage: z.boolean(),
|
showUptimePercentage: z.boolean().register(statusPageStepRegistry, { step: 1 }),
|
||||||
showAdminLoginLink: z.boolean(),
|
showAdminLoginLink: z.boolean().register(statusPageStepRegistry, { step: 1 }),
|
||||||
showInfrastructure: z.boolean(),
|
showInfrastructure: z.boolean().register(statusPageStepRegistry, { step: 1 }),
|
||||||
customCSS: z.string().optional(),
|
customCSS: z.string().optional().register(statusPageStepRegistry, { step: 1 }),
|
||||||
theme: z.enum(STATUS_PAGE_THEMES).optional(),
|
theme: z
|
||||||
themeMode: z.enum(STATUS_PAGE_THEME_MODES).optional(),
|
.enum(STATUS_PAGE_THEMES)
|
||||||
|
.optional()
|
||||||
|
.register(statusPageStepRegistry, { step: 1 }),
|
||||||
|
themeMode: z
|
||||||
|
.enum(STATUS_PAGE_THEME_MODES)
|
||||||
|
.optional()
|
||||||
|
.register(statusPageStepRegistry, { step: 1 }),
|
||||||
logo: z
|
logo: z
|
||||||
.object({
|
.object({
|
||||||
data: z.string(),
|
data: z.string(),
|
||||||
contentType: z.string(),
|
contentType: z.string(),
|
||||||
})
|
})
|
||||||
.nullable()
|
.nullable()
|
||||||
.optional(),
|
.optional()
|
||||||
|
.register(statusPageStepRegistry, { step: 1 }),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type StatusPageFormData = z.infer<typeof statusPageSchema>;
|
export type StatusPageFormData = z.infer<typeof statusPageSchema>;
|
||||||
|
|
||||||
|
// The step each field is validated on, from the per-field
|
||||||
|
// `statusPageStepRegistry` metadata (unannotated fields default to step 0).
|
||||||
|
const fieldSteps = (): [name: FieldPath<StatusPageFormData>, step: number][] =>
|
||||||
|
Object.entries(statusPageSchema.shape).map(([name, field]) => [
|
||||||
|
name as FieldPath<StatusPageFormData>,
|
||||||
|
statusPageStepRegistry.get(field)?.step ?? 0,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// The fields that belong to a given wizard step. Derived from the schema so it
|
||||||
|
// can't drift from the field definitions.
|
||||||
|
export const statusPageStepFieldsFor = (step: number): FieldPath<StatusPageFormData>[] =>
|
||||||
|
fieldSteps()
|
||||||
|
.filter(([, fieldStep]) => fieldStep === step)
|
||||||
|
.map(([name]) => name);
|
||||||
|
|
||||||
|
// Number of wizard steps, derived from the highest step its fields declare.
|
||||||
|
export const statusPageStepCount = (): number =>
|
||||||
|
Math.max(0, ...fieldSteps().map(([, step]) => step)) + 1;
|
||||||
Reference in new issue
Block a user