"use client"; import { useSafeIntl } from "@kopexa/i18n"; import { Tooltip } from "@kopexa/sight"; import { riskRating } from "@kopexa/theme"; import { messages } from "./messages"; import { getRiskLevelFromRating, isRatingUnrated, type RiskLevel, type RiskRating, } from "./types"; export interface RiskRatingDisplayProps { /** The risk rating to display */ rating: RiskRating | null | undefined; /** Show the rating value badge */ showBadge?: boolean; /** Show the level label text */ showLabel?: boolean; /** Size variant */ size?: "sm" | "md"; } /** * Display component for risk ratings. * Shows a colored badge with the rating value and level label. * Handles "not rated" state gracefully. */ export function RiskRatingDisplay({ rating, showBadge = true, showLabel = true, size = "md", }: RiskRatingDisplayProps) { const intl = useSafeIntl(); // Handle unrated state if (isRatingUnrated(rating)) { const styles = riskRating({ size, level: "unrated" }); const levelLabel = intl.formatMessage(messages.level_unrated); return (
{showBadge &&
} {showLabel && {levelLabel}}
); } // At this point, rating is guaranteed to be non-null with valid values const ratedValue = rating as RiskRating; const level = getRiskLevelFromRating(ratedValue.rating); const styles = riskRating({ size, level: level as Exclude, }); const levelLabelKey = `level_${level}` as keyof typeof messages; const levelLabel = intl.formatMessage(messages[levelLabelKey]); return (
{intl.formatMessage(messages.likelihood)}: {ratedValue.likelihood}/5
{intl.formatMessage(messages.consequence)}: {ratedValue.consequence} /5
{ratedValue.comment && (
{ratedValue.comment}
)} } >
{showBadge &&
{ratedValue.rating}
} {showLabel && {levelLabel}}
); }