Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+105
-82
@@ -1,15 +1,48 @@
|
||||
import { type ActionFunctionArgs, type LoaderFunctionArgs, redirect, useActionData, useLoaderData } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
// R-P7 (P7 Lead-Inbox) — lead triage inbox route (`/leads`). Loader builds the
|
||||
// LeadListVM server-side (filter/sort/cursor, SSE realtime); action runs the
|
||||
// single/bulk status changes. Module off -> locked; missing leads.read_own -> 404.
|
||||
// UI is the AD-28 interim LeadInboxPage (composed from ~/ui); the route owns
|
||||
// navigation (filter/page) + the status POSTs.
|
||||
|
||||
import { type ActionFunctionArgs, type LoaderFunctionArgs, redirect, useActionData, useLoaderData, useNavigate, useNavigation, useParams, useSubmit } from "react-router";
|
||||
import { buildLeadListVM, type LeadListVM } from "@hivegrid/view-models";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { badRequestResponse, leadActionErrorResponse, loadLeadContext, notFoundResponse, requireLeadPermission } from "~/utils/integrations/leads/leadAccess.server";
|
||||
import {
|
||||
badRequestResponse,
|
||||
leadActionErrorResponse,
|
||||
loadLeadContext,
|
||||
notFoundResponse,
|
||||
requireLeadPermission,
|
||||
} from "~/utils/integrations/leads/leadAccess.server";
|
||||
import { auditLeadEvent, logLeadActivity } from "~/utils/integrations/leads/leadAudit.server";
|
||||
import { leadListQueryToParams, parseLeadListQuery } from "~/utils/integrations/leads/leadListQuery";
|
||||
import { leadDetailPath, leadEventsPath, leadNewPath, leadStatisticsPath, leadsPath, LEADS_MODULE_KEY } from "~/utils/integrations/leads/leadRoutePaths";
|
||||
import { bulkSetLeadStatus, getVisibleLeadOrThrow, listLeadsForBrowser, resolveUserLabels, setLeadStatus, type LeadStatus } from "~/utils/integrations/leads/leadService.server";
|
||||
import { leadListQueryToParams, parseLeadListQuery, type LeadListQuery } from "~/utils/integrations/leads/leadListQuery";
|
||||
import {
|
||||
leadBoardPath,
|
||||
leadEventsPath,
|
||||
leadNewPath,
|
||||
leadsPath,
|
||||
LEADS_MODULE_KEY,
|
||||
} from "~/utils/integrations/leads/leadRoutePaths";
|
||||
import {
|
||||
bulkSetLeadStatus,
|
||||
getVisibleLeadOrThrow,
|
||||
listLeadsForBrowser,
|
||||
resolveUserLabels,
|
||||
setLeadStatus,
|
||||
type LeadStatus,
|
||||
} from "~/utils/integrations/leads/leadService.server";
|
||||
import { LeadInboxPage } from "~/components/screens/leads/LeadInboxPage";
|
||||
import ServerError from "~/components/ui/errors/ServerError";
|
||||
|
||||
type LoaderData = { title: string; moduleActive: boolean; vm: LeadListVM; nextHref: string | null; sseHref: string | null };
|
||||
type LoaderData = {
|
||||
title: string;
|
||||
/** Shell breadcrumb: the leads workspace in front of this screen's own crumb. */
|
||||
moduleActive: boolean;
|
||||
vm: LeadListVM;
|
||||
query: LeadListQuery;
|
||||
nextHref: string | null;
|
||||
sseHref: string | null;
|
||||
};
|
||||
|
||||
const STATUS_VALUES = new Set<LeadStatus>(["new", "qualified", "converted", "lost", "spam"]);
|
||||
|
||||
@@ -17,7 +50,12 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
const { t } = await getTranslations(request);
|
||||
const ctx = await loadLeadContext({ request, params });
|
||||
const tenantSlug = params.tenant ?? "";
|
||||
// Breadcrumb: <tenant> > Leads > Lead-Inbox, like the sibling leads screens.
|
||||
// The shell only injects the responsible nav entry when it is NOT the current
|
||||
// page, and here it is — so the workspace crumb is provided explicitly.
|
||||
const title = `${t("leads.list.title")} | ${process.env.APP_NAME}`;
|
||||
const url = new URL(request.url);
|
||||
const query = parseLeadListQuery(url.searchParams);
|
||||
if (!ctx.hasModule) {
|
||||
return Response.json({
|
||||
title,
|
||||
@@ -30,8 +68,8 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
perms: { canWrite: false, canDelete: false, canReadAll: false },
|
||||
tenantSlug,
|
||||
createHref: leadNewPath(tenantSlug),
|
||||
statisticsHref: leadStatisticsPath(tenantSlug),
|
||||
}),
|
||||
query,
|
||||
nextHref: null,
|
||||
sseHref: null,
|
||||
} satisfies LoaderData);
|
||||
@@ -41,8 +79,6 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
} catch {
|
||||
throw notFoundResponse();
|
||||
}
|
||||
const url = new URL(request.url);
|
||||
const query = parseLeadListQuery(url.searchParams);
|
||||
const result = await listLeadsForBrowser({ tenantId: ctx.tenantId, ctx, filter: query.filter, sort: query.sort, cursor: query.cursor });
|
||||
const userLabels = await resolveUserLabels(result.rows.map((row) => row.zustaendigerUserId).filter((id): id is string => Boolean(id)));
|
||||
const vm = buildLeadListVM({
|
||||
@@ -57,16 +93,27 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
},
|
||||
tenantSlug,
|
||||
createHref: leadNewPath(tenantSlug),
|
||||
statisticsHref: leadStatisticsPath(tenantSlug),
|
||||
});
|
||||
const nextHref = result.nextCursor
|
||||
? `${leadsPath(tenantSlug)}?${leadListQueryToParams({ ...query, cursor: result.nextCursor }).toString()}`
|
||||
: null;
|
||||
return Response.json({ title, moduleActive: true, vm, nextHref, sseHref: leadEventsPath(tenantSlug) } satisfies LoaderData);
|
||||
const nextHref = result.nextCursor ? `${leadsPath(tenantSlug)}?${leadListQueryToParams({ ...query, cursor: result.nextCursor }).toString()}` : null;
|
||||
return Response.json({
|
||||
title,
|
||||
moduleActive: true,
|
||||
vm,
|
||||
query,
|
||||
nextHref,
|
||||
sseHref: leadEventsPath(tenantSlug),
|
||||
} satisfies LoaderData);
|
||||
};
|
||||
|
||||
function parseIds(form: FormData): string[] {
|
||||
return [...new Set(form.getAll("leadId").map((value) => value.toString().trim()).filter(Boolean))];
|
||||
return [
|
||||
...new Set(
|
||||
form
|
||||
.getAll("leadId")
|
||||
.map((value) => value.toString().trim())
|
||||
.filter(Boolean)
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
@@ -80,11 +127,16 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
const leadId = form.get("leadId")?.toString().trim();
|
||||
const status = form.get("status")?.toString() as LeadStatus | undefined;
|
||||
if (!leadId || !status || !STATUS_VALUES.has(status)) throw badRequestResponse();
|
||||
const lead = await getVisibleLeadOrThrow(ctx.tenantId, leadId, ctx);
|
||||
await getVisibleLeadOrThrow(ctx.tenantId, leadId, ctx);
|
||||
await setLeadStatus({ tenantId: ctx.tenantId, leadId, status, ctx });
|
||||
await auditLeadEvent({ ctx, action: "lead.status_changed", targetType: "lead", targetId: leadId, metadata: { status } });
|
||||
await logLeadActivity({ ctx, leadId, activityType: status === "qualified" ? "lead_qualified" : "status_changed", summaryText: `lead.status.${status}` });
|
||||
return redirect(leadDetailPath(tenantSlug, lead.id));
|
||||
await logLeadActivity({
|
||||
ctx,
|
||||
leadId,
|
||||
activityType: status === "qualified" ? "lead_qualified" : "status_changed",
|
||||
summaryText: "activity.runtime.leads.statusChanged",
|
||||
});
|
||||
return redirect(`${leadsPath(tenantSlug)}?toast=${encodeURIComponent("leads.toast.statusChanged")}`);
|
||||
}
|
||||
if (intent === "bulkStatus") {
|
||||
requireLeadPermission(ctx, "leads.write");
|
||||
@@ -95,7 +147,7 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
if (ids[0]) {
|
||||
await auditLeadEvent({ ctx, action: "lead.bulk_updated", targetType: "lead", targetId: ids[0], metadata: { status, count } });
|
||||
}
|
||||
return redirect(leadsPath(tenantSlug));
|
||||
return redirect(`${leadsPath(tenantSlug)}?toast=${encodeURIComponent("leads.toast.bulkStatusChanged")}`);
|
||||
}
|
||||
throw badRequestResponse();
|
||||
} catch (error) {
|
||||
@@ -104,70 +156,41 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
};
|
||||
|
||||
export default function LeadsIndexRoute() {
|
||||
const { t, i18n } = useTranslation();
|
||||
const { moduleActive, vm, nextHref, sseHref } = useLoaderData<LoaderData>();
|
||||
const { moduleActive, vm, query, nextHref, sseHref } = useLoaderData<LoaderData>();
|
||||
const actionData = useActionData<{ error?: string }>();
|
||||
const rows = vm.status === "ready" && vm.data ? vm.data.rows : [];
|
||||
const dateFormatter = new Intl.DateTimeFormat(i18n.language, { dateStyle: "medium", timeStyle: "short" });
|
||||
const params = useParams();
|
||||
const navigate = useNavigate();
|
||||
const submit = useSubmit();
|
||||
const navigation = useNavigation();
|
||||
|
||||
const statusForm = (leadId: string | string[], status: LeadStatus, intent: "setStatus" | "bulkStatus") => {
|
||||
const form = new FormData();
|
||||
form.set("intent", intent);
|
||||
form.set("status", status);
|
||||
(Array.isArray(leadId) ? leadId : [leadId]).forEach((id) => form.append("leadId", id));
|
||||
submit(form, { method: "post" });
|
||||
};
|
||||
|
||||
return (
|
||||
<main className="mx-auto max-w-6xl px-4 py-10" data-ui-id="ui.leads.list" data-sse-href={sseHref ?? undefined}>
|
||||
<div className="flex items-start justify-between gap-4">
|
||||
<div>
|
||||
<h1 className="text-foreground text-xl font-semibold">{t("leads.list.title")}</h1>
|
||||
<p className="text-muted-foreground mt-1 text-sm">{t(moduleActive ? "leads.list.subtitle" : "leads.list.locked")}</p>
|
||||
</div>
|
||||
<div className="flex gap-3 text-sm">
|
||||
{vm.status === "ready" && vm.data?.canWrite ? <a className="text-theme-600 font-medium" href={vm.data.createHref}>{t("leads.actions.new")}</a> : null}
|
||||
{vm.status === "ready" && vm.data ? <a className="text-theme-600 font-medium" href={vm.data.statisticsHref}>{t("leads.statistics.title")}</a> : null}
|
||||
</div>
|
||||
</div>
|
||||
{actionData?.error ? <p className="mt-4 text-sm text-red-600">{t(actionData.error)}</p> : null}
|
||||
{rows.length === 0 ? (
|
||||
<section className="mt-10 rounded-md border p-6 text-center" data-ui-id="ui.leads.list.empty">
|
||||
<p className="font-medium">{t(vm.status === "ready" && vm.data ? vm.data.emptyHeadlineKey : "leads.list.empty.headline")}</p>
|
||||
<p className="text-muted-foreground mt-1 text-sm">{t(vm.status === "ready" && vm.data ? vm.data.emptyBodyKey : "leads.list.empty.body")}</p>
|
||||
</section>
|
||||
) : (
|
||||
<form method="post" className="mt-6" data-ui-id="ui.leads.list.table">
|
||||
<table className="w-full text-left text-sm">
|
||||
<thead>
|
||||
<tr className="text-muted-foreground border-b">
|
||||
<th className="py-2">{t("leads.list.column.select")}</th>
|
||||
<th className="py-2">{t("leads.list.column.identity")}</th>
|
||||
<th className="py-2">{t("leads.list.column.source")}</th>
|
||||
<th className="py-2">{t("leads.list.column.status")}</th>
|
||||
<th className="py-2">{t("leads.list.column.owner")}</th>
|
||||
<th className="py-2">{t("leads.list.column.createdAt")}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{rows.map((row) => (
|
||||
<tr key={row.id} className="border-b" data-row-id={row.id} data-markers={row.markers.join(",")}>
|
||||
<td className="py-2"><input type="checkbox" name="leadId" value={row.id} aria-label={t("leads.list.column.select")} /></td>
|
||||
<td className="py-2">
|
||||
<a href={row.detailHref} className="text-theme-600 font-medium">{row.displayName}</a>
|
||||
{row.preview ? <p className="text-muted-foreground mt-1 line-clamp-1">{row.preview}</p> : null}
|
||||
</td>
|
||||
<td className="py-2">{t(row.quelleLabelKey)}</td>
|
||||
<td className="py-2">{t(row.statusLabelKey)}</td>
|
||||
<td className="py-2">{row.zustaendigerLabel ?? ""}</td>
|
||||
<td className="py-2">{dateFormatter.format(new Date(row.createdAtIso))}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
{vm.status === "ready" && vm.data?.canWrite ? (
|
||||
<div className="mt-4 flex items-center gap-3">
|
||||
<select name="status" className="rounded border px-2 py-1 text-sm">
|
||||
{Array.from(STATUS_VALUES).map((status) => <option key={status} value={status}>{t(`leads.status.${status}`)}</option>)}
|
||||
</select>
|
||||
<button type="submit" name="intent" value="bulkStatus" className="text-theme-600 text-sm font-medium">{t("leads.actions.applyBulk")}</button>
|
||||
</div>
|
||||
) : null}
|
||||
</form>
|
||||
)}
|
||||
{nextHref ? <a className="text-theme-600 mt-4 inline-block text-sm font-medium" href={nextHref}>{t("leads.actions.nextPage")}</a> : null}
|
||||
</main>
|
||||
<LeadInboxPage
|
||||
vm={vm}
|
||||
moduleActive={moduleActive}
|
||||
query={query}
|
||||
hasNextPage={Boolean(nextHref)}
|
||||
sseHref={sseHref}
|
||||
submitting={navigation.state !== "idle"}
|
||||
errorKey={actionData?.error ?? null}
|
||||
actions={{
|
||||
onApplyFilter: (nextQuery) => {
|
||||
const qs = leadListQueryToParams(nextQuery).toString();
|
||||
navigate(qs ? `?${qs}` : ".");
|
||||
},
|
||||
onNextPage: () => nextHref && navigate(nextHref),
|
||||
onSetStatus: (leadId, status) => statusForm(leadId, status, "setStatus"),
|
||||
onBulkStatus: (leadIds, status) => statusForm(leadIds, status, "bulkStatus"),
|
||||
onOpenBoard: () => navigate(leadBoardPath(params.tenant ?? "")),
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user