- Login: company-code hint after an invalid-credentials failure with an empty company. - Password forms show the active rules (label-first, count-safe) and tie hint and refusal to the input. - Interests and roles: soft delete with confirm dialogs; deactivate now confirms too. - Dashboard figure explanations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
53 lines
2.0 KiB
TypeScript
53 lines
2.0 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { ApiError, apiGet } from "@/app/lib/clientApi";
|
|
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
|
|
|
type Rule = { param: string; value: number | null };
|
|
type Translate = ReturnType<typeof useT>;
|
|
|
|
const sentence = (t: Translate, param: string, value: unknown) => t(`PwdHint_${param}`, { n: value == null ? "" : String(value) });
|
|
|
|
/**
|
|
* The refusal text when the API answered 400 PASSWORD_RULE, else null.
|
|
* ponytail: relies on the BFF forwarding `param`/`value` in details (mobileApi.extractDetails).
|
|
*/
|
|
export function passwordRuleError(err: unknown, t: Translate): string | null {
|
|
const data = err instanceof ApiError ? (err.data as { code?: string; details?: { param?: string; value?: unknown } } | undefined) : undefined;
|
|
if (data?.code !== "PASSWORD_RULE" || !data.details?.param) return null;
|
|
return t("PwdRuleRefused", { rule: sentence(t, data.details.param, data.details.value) });
|
|
}
|
|
|
|
/** The switched-on password rules as a short list; shows nothing while loading or if they cannot be loaded. */
|
|
export function PasswordRulesHint({ source, id, onShown }: { source: "admin" | "public"; id?: string; onShown?: (shown: boolean) => void }) {
|
|
const t = useT();
|
|
const [rules, setRules] = useState<Rule[]>([]);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
(async () => {
|
|
try {
|
|
const loaded = await apiGet<Rule[]>(source === "admin" ? "/api/password-rules" : "/api/public/password-rules");
|
|
if (!cancelled && Array.isArray(loaded)) setRules(loaded);
|
|
} catch {
|
|
// no banner: the hint is optional, a refusal still names the rule
|
|
}
|
|
})();
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [source]);
|
|
|
|
useEffect(() => onShown?.(rules.length > 0), [rules.length, onShown]);
|
|
|
|
if (!rules.length) return null;
|
|
return (
|
|
<ul id={id} className="list-disc pl-5 text-sm text-muted-foreground">
|
|
{rules.map((r) => (
|
|
<li key={r.param}>{sentence(t, r.param, r.value)}</li>
|
|
))}
|
|
</ul>
|
|
);
|
|
}
|