Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,242 @@
|
||||
/**
|
||||
* Admin email-sequence detail + edit view (Phase 05 #80 follow-up).
|
||||
*
|
||||
* GET /admin/email-sequences/:id — render the edit form (name +
|
||||
* trigger + enabled toggle + JSON editors for steps + conditions).
|
||||
* POST /admin/email-sequences/:id — accept the edit form and persist
|
||||
* the updated sequence; supports the special action `toggle` to
|
||||
* flip `isEnabled`.
|
||||
*
|
||||
* No visual rule builder for steps / conditions — operators paste in
|
||||
* the JSON. The shape is validated by `parseSequenceSteps` from the
|
||||
* engine package before save so bad JSON cannot corrupt a run.
|
||||
*/
|
||||
import { Form, Link, type ActionFunctionArgs, type LoaderFunctionArgs, redirect, useActionData, useLoaderData } from "react-router";
|
||||
import { t as i18nT } from "i18next";
|
||||
import { adminPath } from "~/utils/app/CanonicalUrlUtils";
|
||||
import { db, dbRead } from "~/utils/db.server";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
|
||||
type LoaderData = {
|
||||
sequence: {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string | null;
|
||||
triggerEvent: string;
|
||||
isEnabled: boolean;
|
||||
stepsJson: unknown;
|
||||
conditionsJson: unknown;
|
||||
updatedAt: string;
|
||||
};
|
||||
};
|
||||
|
||||
type ActionResponse = { ok: true } | { ok: false; error: string; field?: string };
|
||||
|
||||
export const loader = async ({ params, request }: LoaderFunctionArgs) => {
|
||||
await verifyUserHasPermission(request, "admin.marketing.leads.view");
|
||||
const id = String(params["id"] ?? "");
|
||||
const sequence = await dbRead.emailSequence.findFirst({ where: { id } });
|
||||
if (!sequence) throw new Response(null, { status: 404 });
|
||||
return Response.json({
|
||||
sequence: {
|
||||
id: sequence.id,
|
||||
name: sequence.name,
|
||||
description: sequence.description,
|
||||
triggerEvent: sequence.triggerEvent,
|
||||
isEnabled: sequence.isEnabled,
|
||||
stepsJson: sequence.stepsJson,
|
||||
conditionsJson: sequence.conditionsJson,
|
||||
updatedAt: sequence.updatedAt.toISOString(),
|
||||
},
|
||||
} satisfies LoaderData);
|
||||
};
|
||||
|
||||
export const action = async ({ params, request }: ActionFunctionArgs) => {
|
||||
await verifyUserHasPermission(request, "admin.marketing.leads.view");
|
||||
const { t } = await getTranslations(request);
|
||||
const id = String(params["id"] ?? "");
|
||||
const formData = await request.formData();
|
||||
const intent = String(formData.get("intent") ?? "save");
|
||||
const existing = await db.emailSequence.findFirst({ where: { id } });
|
||||
if (!existing) throw new Response(null, { status: 404 });
|
||||
|
||||
if (intent === "toggle") {
|
||||
await db.emailSequence.update({ where: { id }, data: { isEnabled: !existing.isEnabled } });
|
||||
return redirect(`${adminPath("email-sequences", { mode: "host-aware" })}/${id}`);
|
||||
}
|
||||
|
||||
const name = String(formData.get("name") ?? "").trim();
|
||||
const description = String(formData.get("description") ?? "").trim();
|
||||
const triggerEvent = String(formData.get("triggerEvent") ?? "").trim();
|
||||
const stepsRaw = String(formData.get("stepsJson") ?? "[]");
|
||||
const conditionsRaw = String(formData.get("conditionsJson") ?? "");
|
||||
if (!name) return Response.json({ ok: false, error: t("admin.emailSequences.errors.nameRequired"), field: "name" } satisfies ActionResponse);
|
||||
if (!triggerEvent) {
|
||||
return Response.json({ ok: false, error: t("admin.emailSequences.errors.triggerEventRequired"), field: "triggerEvent" } satisfies ActionResponse);
|
||||
}
|
||||
let stepsJson: unknown;
|
||||
try {
|
||||
stepsJson = JSON.parse(stepsRaw);
|
||||
} catch {
|
||||
return Response.json({ ok: false, error: t("admin.emailSequences.errors.stepsJsonInvalid"), field: "stepsJson" } satisfies ActionResponse);
|
||||
}
|
||||
const { parseSequenceSteps } = await import("@toolctrl/email-sequences");
|
||||
try {
|
||||
parseSequenceSteps(stepsJson);
|
||||
} catch (error) {
|
||||
return Response.json({
|
||||
ok: false,
|
||||
error: error instanceof Error ? error.message : t("admin.emailSequences.errors.stepsJsonValidationFailed"),
|
||||
field: "stepsJson",
|
||||
} satisfies ActionResponse);
|
||||
}
|
||||
let conditionsJson: unknown = null;
|
||||
if (conditionsRaw.trim().length > 0) {
|
||||
try {
|
||||
conditionsJson = JSON.parse(conditionsRaw);
|
||||
} catch {
|
||||
return Response.json({
|
||||
ok: false,
|
||||
error: t("admin.emailSequences.errors.conditionsJsonInvalid"),
|
||||
field: "conditionsJson",
|
||||
} satisfies ActionResponse);
|
||||
}
|
||||
}
|
||||
await db.emailSequence.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name,
|
||||
description: description || null,
|
||||
triggerEvent,
|
||||
stepsJson: stepsJson as never,
|
||||
conditionsJson: conditionsJson as never,
|
||||
},
|
||||
});
|
||||
return redirect(`${adminPath("email-sequences", { mode: "host-aware" })}/${id}`);
|
||||
};
|
||||
|
||||
export default function EmailSequenceDetail() {
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const action = useActionData<ActionResponse>();
|
||||
const stepsPretty = JSON.stringify(data.sequence.stepsJson, null, 2);
|
||||
const conditionsPretty = data.sequence.conditionsJson
|
||||
? JSON.stringify(data.sequence.conditionsJson, null, 2)
|
||||
: "";
|
||||
const statsHref = `${adminPath("email-sequences", { mode: "host-aware" })}/${data.sequence.id}/stats`;
|
||||
|
||||
return (
|
||||
<div className="mx-auto mb-12 max-w-5xl space-y-5 px-4 py-4 sm:px-6 lg:px-8">
|
||||
<div className="border-border flex flex-col gap-2 border-b pb-5 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div>
|
||||
<h1 className="text-lg font-medium leading-6">{data.sequence.name}</h1>
|
||||
<p className="text-muted-foreground mt-1 text-xs">
|
||||
{i18nT("admin.emailSequences.detail.triggerPrefix", { triggerEvent: data.sequence.triggerEvent })}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Link
|
||||
to={statsHref}
|
||||
className="hover:bg-muted rounded-md border px-3 py-1.5 text-sm"
|
||||
>
|
||||
{i18nT("admin.emailSequences.actions.stats")}
|
||||
</Link>
|
||||
<Form method="post">
|
||||
<input type="hidden" name="intent" value="toggle" />
|
||||
<button
|
||||
type="submit"
|
||||
className={`rounded-md px-3 py-1.5 text-sm ${
|
||||
data.sequence.isEnabled
|
||||
? "bg-rose-100 text-rose-800 hover:bg-rose-200"
|
||||
: "bg-emerald-100 text-emerald-800 hover:bg-emerald-200"
|
||||
}`}
|
||||
>
|
||||
{data.sequence.isEnabled ? i18nT("admin.emailSequences.actions.disable") : i18nT("admin.emailSequences.actions.enable")}
|
||||
</button>
|
||||
</Form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{action && !action.ok ? (
|
||||
<div className="rounded-md border border-rose-300 bg-rose-50 px-3 py-2 text-sm text-rose-800">
|
||||
{action.error}
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
<Form method="post" className="space-y-4">
|
||||
<div>
|
||||
<label className="text-foreground mb-1 block text-sm font-medium" htmlFor="name">
|
||||
{i18nT("admin.emailSequences.form.name")}
|
||||
</label>
|
||||
<input
|
||||
id="name"
|
||||
name="name"
|
||||
defaultValue={data.sequence.name}
|
||||
required
|
||||
className="border-border w-full rounded-md border px-3 py-1.5 text-sm"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="text-foreground mb-1 block text-sm font-medium" htmlFor="description">
|
||||
{i18nT("admin.emailSequences.form.description")}
|
||||
</label>
|
||||
<input
|
||||
id="description"
|
||||
name="description"
|
||||
defaultValue={data.sequence.description ?? ""}
|
||||
className="border-border w-full rounded-md border px-3 py-1.5 text-sm"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="text-foreground mb-1 block text-sm font-medium" htmlFor="triggerEvent">
|
||||
{i18nT("admin.emailSequences.form.triggerEvent")}
|
||||
</label>
|
||||
<input
|
||||
id="triggerEvent"
|
||||
name="triggerEvent"
|
||||
defaultValue={data.sequence.triggerEvent}
|
||||
required
|
||||
className="border-border w-full rounded-md border px-3 py-1.5 text-sm"
|
||||
/>
|
||||
<p className="text-muted-foreground mt-1 text-xs">
|
||||
{i18nT("admin.emailSequences.form.triggerHint")} <code>lead.captured</code>, <code>tenant.signup</code>, <code>tenant.trial_expiring</code>.
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<label className="text-foreground mb-1 block text-sm font-medium" htmlFor="stepsJson">
|
||||
{i18nT("admin.emailSequences.form.stepsJson")}
|
||||
</label>
|
||||
<textarea
|
||||
id="stepsJson"
|
||||
name="stepsJson"
|
||||
defaultValue={stepsPretty}
|
||||
rows={14}
|
||||
spellCheck={false}
|
||||
className="border-border w-full rounded-md border px-3 py-1.5 font-mono text-xs"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="text-foreground mb-1 block text-sm font-medium" htmlFor="conditionsJson">
|
||||
{i18nT("admin.emailSequences.form.conditionsJson")}
|
||||
</label>
|
||||
<textarea
|
||||
id="conditionsJson"
|
||||
name="conditionsJson"
|
||||
defaultValue={conditionsPretty}
|
||||
rows={6}
|
||||
spellCheck={false}
|
||||
className="border-border w-full rounded-md border px-3 py-1.5 font-mono text-xs"
|
||||
/>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<Link to={adminPath("email-sequences", { mode: "host-aware" })} className="text-primary text-sm hover:underline">
|
||||
{i18nT("admin.emailSequences.actions.backToList")}
|
||||
</Link>
|
||||
<button type="submit" className="bg-primary text-primary-foreground rounded-md px-3 py-1.5 text-sm">
|
||||
{i18nT("admin.emailSequences.actions.save")}
|
||||
</button>
|
||||
</div>
|
||||
</Form>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user