Files
HiveGrid-Wireframes/hivegrid-cloud/apps/web/app/components/impersonation-banner.tsx
T

50 lines
2.2 KiB
TypeScript

import { Form } from "react-router";
import { useTranslation } from "react-i18next";
import { formatIcu } from "@toolctrl/i18n";
type ImpersonationBannerProps = {
fromEmail: string;
toEmail: string;
csrf?: string;
};
// Migrated component (#72 i18n-foundation, example #1).
// Hardcoded "Acting as X from Y" and "Exit impersonation" strings replaced
// with `common.impersonation.*` keys. The "Acting as …" message uses
// ICU-MessageFormat placeholders so future locale variants can reorder the
// {to}/{from} tokens — Arabic for instance reads right-to-left and may want
// the order flipped.
export default function ImpersonationBanner({ fromEmail, toEmail, csrf }: ImpersonationBannerProps) {
const { t, i18n } = useTranslation("common");
const actingAsPattern = t("impersonation.actingAs");
const actingAs = formatIcu(actingAsPattern === "impersonation.actingAs" ? "Acting as {to} from {from}" : actingAsPattern, { to: toEmail, from: fromEmail }, { locale: i18n.language });
const exitLabel = t("impersonation.exit");
return (
<section
role="status"
aria-label={t("impersonation.ariaLabel")}
data-testid="impersonation-banner"
className="border-b-2 border-red-950 bg-red-700 text-white shadow-sm"
>
<div className="mx-auto flex max-w-7xl flex-col gap-3 px-3 py-3 text-sm font-semibold sm:flex-row sm:items-center sm:justify-between sm:px-6 lg:px-8">
<div className="min-w-0">
<span className="uppercase tracking-wide text-red-100">{t("impersonation.active")}</span>
<span className="mx-2 text-red-200" aria-hidden="true">
•
</span>
<span>{actingAs}</span>
</div>
<Form method="post" action="/logout">
{csrf && <input type="hidden" name="csrf" value={csrf} />}
<button
type="submit"
className="inline-flex shrink-0 items-center justify-center rounded-md bg-white px-3 py-2 text-xs font-bold text-red-700 shadow-sm hover:bg-red-50 focus:outline-none focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-red-700 sm:text-sm"
>
{exitLabel === "impersonation.exit" ? "Exit impersonation" : exitLabel}
</button>
</Form>
</div>
</section>
);
}