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,166 @@
|
||||
/**
|
||||
* Admin email-sequence list view (Phase 05 #80 follow-up).
|
||||
*
|
||||
* GET /admin/email-sequences — DataTable with name, trigger event,
|
||||
* enabled badge, step-count and recent-run statistics (active,
|
||||
* completed, unsubscribed counts) per sequence. Backed by a single
|
||||
* groupBy query on EmailSequenceRun for performance — see the
|
||||
* read-replica usage via `dbRead` (#41).
|
||||
*/
|
||||
import { Link, LoaderFunctionArgs, useLoaderData } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { adminPath } from "~/utils/app/CanonicalUrlUtils";
|
||||
import { dbRead } from "~/utils/db.server";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { useLocaleFormatters } from "~/locale/format";
|
||||
|
||||
type SequenceRow = {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string | null;
|
||||
triggerEvent: string;
|
||||
isEnabled: boolean;
|
||||
stepCount: number;
|
||||
updatedAt: string;
|
||||
stats: {
|
||||
active: number;
|
||||
completed: number;
|
||||
unsubscribed: number;
|
||||
failed: number;
|
||||
};
|
||||
};
|
||||
|
||||
type LoaderData = {
|
||||
items: SequenceRow[];
|
||||
total: number;
|
||||
};
|
||||
|
||||
function countSteps(stepsJson: unknown): number {
|
||||
return Array.isArray(stepsJson) ? stepsJson.length : 0;
|
||||
}
|
||||
|
||||
export const loader = async ({ request }: LoaderFunctionArgs) => {
|
||||
await verifyUserHasPermission(request, "admin.marketing.leads.view");
|
||||
const sequences = await dbRead.emailSequence.findMany({
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
description: true,
|
||||
triggerEvent: true,
|
||||
isEnabled: true,
|
||||
stepsJson: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
});
|
||||
const sequenceIds = sequences.map((sequence) => sequence.id);
|
||||
const runStats =
|
||||
sequenceIds.length > 0
|
||||
? await dbRead.emailSequenceRun.groupBy({
|
||||
by: ["sequenceId", "status"],
|
||||
where: { sequenceId: { in: sequenceIds } },
|
||||
_count: { _all: true },
|
||||
})
|
||||
: [];
|
||||
const statsMap = new Map<string, SequenceRow["stats"]>();
|
||||
for (const row of runStats) {
|
||||
const bucket = statsMap.get(row.sequenceId) ?? { active: 0, completed: 0, unsubscribed: 0, failed: 0 };
|
||||
if (row.status === "running" || row.status === "pending") bucket.active += row._count._all;
|
||||
else if (row.status === "completed") bucket.completed = row._count._all;
|
||||
else if (row.status === "unsubscribed") bucket.unsubscribed = row._count._all;
|
||||
else if (row.status === "failed" || row.status === "cancelled") bucket.failed += row._count._all;
|
||||
statsMap.set(row.sequenceId, bucket);
|
||||
}
|
||||
const items: SequenceRow[] = sequences.map((s) => ({
|
||||
id: s.id,
|
||||
name: s.name,
|
||||
description: s.description,
|
||||
triggerEvent: s.triggerEvent,
|
||||
isEnabled: s.isEnabled,
|
||||
stepCount: countSteps(s.stepsJson),
|
||||
updatedAt: s.updatedAt.toISOString(),
|
||||
stats: statsMap.get(s.id) ?? { active: 0, completed: 0, unsubscribed: 0, failed: 0 },
|
||||
}));
|
||||
return Response.json({ items, total: items.length } satisfies LoaderData);
|
||||
};
|
||||
|
||||
export default function EmailSequencesIndex() {
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const { t } = useTranslation();
|
||||
const formatters = useLocaleFormatters();
|
||||
return (
|
||||
<div className="mx-auto mb-12 max-w-7xl 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">{t("admin.emailSequences.title")}</h1>
|
||||
<p className="text-muted-foreground mt-1 text-sm">{t("admin.emailSequences.subtitle")}</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="text-muted-foreground text-sm">
|
||||
{t("admin.emailSequences.totalSequences", { count: data.total, formattedCount: formatters.number(data.total) })}
|
||||
</div>
|
||||
<Link
|
||||
to={`${adminPath("email-sequences", { mode: "host-aware" })}/new`}
|
||||
className="bg-primary text-primary-foreground rounded-md px-3 py-1.5 text-sm"
|
||||
>
|
||||
{t("admin.emailSequences.createNew")}
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="overflow-hidden rounded-md border">
|
||||
<table className="w-full text-sm">
|
||||
<thead className="bg-muted/40 text-muted-foreground text-xs uppercase">
|
||||
<tr>
|
||||
<th className="px-3 py-2 text-left">{t("admin.emailSequences.col.name")}</th>
|
||||
<th className="px-3 py-2 text-left">{t("admin.emailSequences.col.trigger")}</th>
|
||||
<th className="px-3 py-2 text-left">{t("admin.emailSequences.col.status")}</th>
|
||||
<th className="px-3 py-2 text-right">{t("admin.emailSequences.col.steps")}</th>
|
||||
<th className="px-3 py-2 text-right">{t("admin.emailSequences.col.active")}</th>
|
||||
<th className="px-3 py-2 text-right">{t("admin.emailSequences.col.completed")}</th>
|
||||
<th className="px-3 py-2 text-right">{t("admin.emailSequences.col.unsubscribed")}</th>
|
||||
<th className="px-3 py-2 text-left">{t("admin.emailSequences.col.updated")}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{data.items.length === 0 ? (
|
||||
<tr>
|
||||
<td colSpan={8} className="text-muted-foreground px-3 py-6 text-center">
|
||||
{t("admin.emailSequences.empty")}
|
||||
</td>
|
||||
</tr>
|
||||
) : (
|
||||
data.items.map((row) => (
|
||||
<tr key={row.id} className="border-border hover:bg-muted/30 border-t">
|
||||
<td className="px-3 py-2">
|
||||
<Link to={`${adminPath("email-sequences", { mode: "host-aware" })}/${row.id}`} className="text-primary hover:underline">
|
||||
{row.name}
|
||||
</Link>
|
||||
{row.description ? <p className="text-muted-foreground mt-0.5 truncate text-xs">{row.description}</p> : null}
|
||||
</td>
|
||||
<td className="text-muted-foreground px-3 py-2 text-xs">{row.triggerEvent}</td>
|
||||
<td className="px-3 py-2">
|
||||
<span
|
||||
className={`rounded px-1.5 py-0.5 text-xs ${
|
||||
row.isEnabled ? "bg-emerald-100 text-emerald-800" : "bg-gray-100 text-gray-700"
|
||||
}`}
|
||||
>
|
||||
{row.isEnabled ? t("shared.enabled") : t("shared.disabled")}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-3 py-2 text-right">{formatters.number(row.stepCount)}</td>
|
||||
<td className="px-3 py-2 text-right">{formatters.number(row.stats.active)}</td>
|
||||
<td className="px-3 py-2 text-right">{formatters.number(row.stats.completed)}</td>
|
||||
<td className="px-3 py-2 text-right">{formatters.number(row.stats.unsubscribed)}</td>
|
||||
<td className="text-muted-foreground px-3 py-2 text-xs">
|
||||
{formatters.dateTime(row.updatedAt, { dateStyle: "medium", timeStyle: "short" })}
|
||||
</td>
|
||||
</tr>
|
||||
))
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user