65 lines
2.9 KiB
TypeScript
65 lines
2.9 KiB
TypeScript
// P17 HR-Hub workflows (AK-P17b) — announcements feed. Pinned first, optional
|
|
// unread-only filter, audience-scoped to the viewer's team/location. Read needs
|
|
// hr.read_own; managing (editor) needs hr.write. Transitional UI.
|
|
|
|
import { type LoaderFunctionArgs, useLoaderData } from "react-router";
|
|
import { buildAnnouncementFeedVM, type HrAnnouncementFeedVM, type HrAnnouncementFeedRow } from "@hivegrid/view-models";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { hrHasPermission, loadHrContext, notFoundResponse, requireHrPermission } from "~/utils/integrations/hrAccess.server";
|
|
import { getProfileByUser } from "~/utils/integrations/hrService.server";
|
|
import { listFeed } from "~/utils/integrations/hrAnnouncementService.server";
|
|
import { hrAdminAnnouncementsPath, hrAnnouncementDetailPath } from "~/utils/integrations/hrRoutePaths";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
import { HrAnnouncementsFeedPage } from "~/components/screens/hr/HrAnnouncementsFeedPage";
|
|
|
|
type LoaderData = { title: string; vm: HrAnnouncementFeedVM };
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const { t } = await getTranslations(request);
|
|
const ctx = await loadHrContext({ request, params });
|
|
const tenantSlug = params.tenant ?? "";
|
|
const url = new URL(request.url);
|
|
const unreadOnly = url.searchParams.get("unread") === "1";
|
|
const title = `${t("hr.announcements.title")} | ${process.env.APP_NAME}`;
|
|
const manageHref = hrAdminAnnouncementsPath(tenantSlug);
|
|
|
|
if (!ctx.hasModule) {
|
|
return Response.json({ title, vm: buildAnnouncementFeedVM({ rows: [], unreadOnly, canManage: false, manageHref }) } satisfies LoaderData);
|
|
}
|
|
try {
|
|
requireHrPermission(ctx, "hr.read_own");
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
|
|
const profile = await getProfileByUser(ctx.tenantId, ctx.userId);
|
|
const entries = await listFeed({ tenantId: ctx.tenantId, userId: ctx.userId, viewerTeam: profile?.team ?? null, viewerLocationId: profile?.locationId ?? null });
|
|
const rows: HrAnnouncementFeedRow[] = entries.map((entry) => ({
|
|
id: entry.id,
|
|
title: entry.title,
|
|
snippet: entry.body.slice(0, 140),
|
|
pinned: entry.pinnedAt != null,
|
|
isRead: entry.isRead,
|
|
requiresAcknowledgement: entry.requiresAcknowledgement,
|
|
isAcknowledged: entry.isAcknowledged,
|
|
reactionCount: entry.reactionCount,
|
|
commentCount: entry.commentCount,
|
|
publishedIso: (entry.publishAt ?? entry.createdAt).toISOString(),
|
|
detailHref: hrAnnouncementDetailPath(tenantSlug, entry.id),
|
|
}));
|
|
|
|
const vm = buildAnnouncementFeedVM({ rows, unreadOnly, canManage: hrHasPermission(ctx, "hr.write"), manageHref });
|
|
return Response.json({ title, vm } satisfies LoaderData);
|
|
};
|
|
|
|
export default function HrAnnouncementsRoute() {
|
|
const { vm } = useLoaderData<LoaderData>();
|
|
return <HrAnnouncementsFeedPage vm={vm} />;
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|
|
|
|
export const handle = { moduleKey: "hr" };
|