105 lines
4.9 KiB
TypeScript
105 lines
4.9 KiB
TypeScript
// P17 HR-Hub workflows (AK-P17b) — announcement detail. Opening marks it read;
|
|
// shows the markdown body, grouped reactions (toggle), single-level comments
|
|
// (add), an acknowledgement button when required, and a read-receipt table for
|
|
// managers (hr.write). Transitional UI.
|
|
|
|
import { type ActionFunctionArgs, type LoaderFunctionArgs, useFetcher, useLoaderData } from "react-router";
|
|
import { buildAnnouncementDetailVM, type HrAnnouncementDetailVM } from "@hivegrid/view-models";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { badRequestResponse, hrActionErrorResponse, hrHasPermission, loadHrContext, notFoundResponse, requireHrPermission } from "~/utils/integrations/hrAccess.server";
|
|
import { resolveUserLabels } from "~/utils/integrations/hrService.server";
|
|
import { acknowledge, addComment, getAnnouncementOrThrow, getReactionGroups, getUserAckState, listComments, listReadReceipts, markRead, toggleReaction } from "~/utils/integrations/hrAnnouncementService.server";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
import { HrAnnouncementDetailPage } from "~/components/screens/hr/HrAnnouncementDetailPage";
|
|
|
|
type LoaderData = { title: string; vm: HrAnnouncementDetailVM };
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const { t } = await getTranslations(request);
|
|
const ctx = await loadHrContext({ request, params });
|
|
const announcementId = params.id ?? "";
|
|
if (!ctx.hasModule) throw notFoundResponse();
|
|
try {
|
|
requireHrPermission(ctx, "hr.read_own");
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
|
|
const announcement = await getAnnouncementOrThrow(ctx.tenantId, announcementId);
|
|
await markRead({ tenantId: ctx.tenantId, announcementId, userId: ctx.userId });
|
|
const canManage = hrHasPermission(ctx, "hr.write");
|
|
const [reactions, comments, isAcknowledged, receiptsRaw] = await Promise.all([
|
|
getReactionGroups(ctx.tenantId, announcementId, ctx.userId),
|
|
listComments(ctx.tenantId, announcementId),
|
|
getUserAckState(ctx.tenantId, announcementId, ctx.userId),
|
|
canManage ? listReadReceipts(ctx.tenantId, announcementId) : Promise.resolve([]),
|
|
]);
|
|
|
|
const labelIds = [announcement.authorUserId, ...comments.map((c) => c.userId), ...receiptsRaw.map((r) => r.userId)];
|
|
const labels = await resolveUserLabels(labelIds);
|
|
|
|
const vm = buildAnnouncementDetailVM({
|
|
id: announcement.id,
|
|
title: announcement.title,
|
|
bodyMarkdown: announcement.body,
|
|
authorLabel: labels.get(announcement.authorUserId)?.label ?? announcement.authorUserId,
|
|
publishedIso: (announcement.publishAt ?? announcement.createdAt).toISOString(),
|
|
requiresAcknowledgement: announcement.requiresAcknowledgement,
|
|
isAcknowledged,
|
|
reactions,
|
|
comments: comments.map((c) => ({ id: c.id, authorLabel: labels.get(c.userId)?.label ?? c.userId, body: c.body, createdIso: c.createdAt.toISOString(), canArchive: canManage })),
|
|
canManage,
|
|
readReceipts: receiptsRaw.map((r) => ({ userLabel: labels.get(r.userId)?.label ?? r.userId, readIso: r.readAt.toISOString(), acknowledged: r.acknowledgedAt != null })),
|
|
});
|
|
return Response.json({ title: `${announcement.title} | ${process.env.APP_NAME}`, vm } satisfies LoaderData);
|
|
};
|
|
|
|
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
|
const ctx = await loadHrContext({ request, params, requireModule: true });
|
|
requireHrPermission(ctx, "hr.read_own");
|
|
const announcementId = params.id ?? "";
|
|
const form = await request.formData();
|
|
const intent = form.get("intent")?.toString();
|
|
try {
|
|
if (intent === "ack") {
|
|
await acknowledge({ tenantId: ctx.tenantId, announcementId, userId: ctx.userId });
|
|
return Response.json({ ok: true });
|
|
}
|
|
if (intent === "react") {
|
|
await toggleReaction({ tenantId: ctx.tenantId, announcementId, userId: ctx.userId, reaction: form.get("reaction")?.toString() ?? "👍" });
|
|
return Response.json({ ok: true });
|
|
}
|
|
if (intent === "comment") {
|
|
await addComment({ tenantId: ctx.tenantId, announcementId, userId: ctx.userId, body: form.get("body")?.toString() ?? "" });
|
|
return Response.json({ ok: true });
|
|
}
|
|
throw badRequestResponse();
|
|
} catch (error) {
|
|
if (error instanceof Response) throw error;
|
|
return hrActionErrorResponse(error);
|
|
}
|
|
};
|
|
|
|
export default function HrAnnouncementDetailRoute() {
|
|
const { vm } = useLoaderData<LoaderData>();
|
|
const fetcher = useFetcher();
|
|
const submit = (values: Record<string, string>) => fetcher.submit(values, { method: "post" });
|
|
return (
|
|
<HrAnnouncementDetailPage
|
|
vm={vm}
|
|
submitting={fetcher.state !== "idle"}
|
|
actions={{
|
|
onAck: () => submit({ intent: "ack" }),
|
|
onReact: (reaction) => submit({ intent: "react", reaction }),
|
|
onComment: (body) => submit({ intent: "comment", body }),
|
|
}}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|
|
|
|
export const handle = { moduleKey: "hr" };
|