All files / src/features/Merchants/MerchantSidePanel/Modals/MatchReport/components MatchAITranslationPanel.tsx

100% Statements 8/8
100% Branches 6/6
100% Functions 5/5
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150                                                                        13x                                         3x                                 7x                                               3x                                                       10x 10x                         5x           5x      
import { Box, Stack } from "@mui/material";
import { CircleNotchIcon, WarningIcon } from "@phosphor-icons/react";
import { styled, useAppTheme } from "@theme/v2/Provider";
 
import GiveButton from "@shared/Button/GiveButton";
import GiveText from "@shared/Text/GiveText";
import TaskCard from "@features/Merchants/MerchantSidePanel/UnderwritingTasks/components/TaskCard";
 
import { MatchTranslationStatus } from "../hooks/useMatchTranslation";
import AISparkleIcon from "./AISparkleIcon";
import {
  AI_DISCLAIMER,
  AI_ERROR_SUBTITLE,
  AI_ERROR_TITLE,
  AI_LOADING_SUBTITLE,
  AI_LOADING_TITLE,
  AI_PANEL_TITLE,
  AI_RETRY_LABEL,
} from "../matchTranslation.constants";
 
interface Props {
  status: MatchTranslationStatus;
  summary?: string;
  onRetry: () => void;
  canRetry: boolean;
}
 
// The stacked "AI JSON Translation" panel from the mockup. Rendered below the
// raw-JSON "Response" card once the user triggers a translation (status leaves
// "idle"), or immediately in "success" when a persisted summary exists.
export default function MatchAITranslationPanel({
  status,
  summary,
  onRetry,
  canRetry,
}: Props) {
  return (
    <Box
      role="status"
      aria-live="polite"
      aria-busy={status === "loading"}
      data-testid="ai-translation-panel"
    >
      <TaskCard title={AI_PANEL_TITLE}>
        {status === "success" ? (
          <SuccessContent summary={summary} />
        ) : status === "error" ? (
          <ErrorContent onRetry={onRetry} canRetry={canRetry} />
        ) : (
          <LoadingContent />
        )}
      </TaskCard>
    </Box>
  );
}
 
function SuccessContent({ summary }: { summary?: string }) {
  return (
    <Stack gap="12px">
      <SummaryText
        variant="bodyS"
        color="secondary"
        data-testid="ai-translation-summary"
      >
        {summary}
      </SummaryText>
      <GiveText variant="bodyXS" color="secondary">
        {AI_DISCLAIMER}
      </GiveText>
    </Stack>
  );
}
 
function LoadingContent() {
  return (
    <StatusStack>
      <IconBadge>
        <CircleNotchIcon size="20px" />
      </IconBadge>
      <Stack gap="8px">
        <GiveText variant="bodyS" color="secondary" textAlign="center">
          {AI_LOADING_TITLE}
        </GiveText>
        <GiveText variant="bodyXS" color="secondary" textAlign="center">
          {AI_LOADING_SUBTITLE}
        </GiveText>
      </Stack>
    </StatusStack>
  );
}
 
function ErrorContent({
  onRetry,
  canRetry,
}: {
  onRetry: () => void;
  canRetry: boolean;
}) {
  return (
    <StatusStack>
      <IconBadge>
        <WarningIcon size="20px" />
      </IconBadge>
      <Stack gap="8px">
        <GiveText variant="bodyS" color="secondary" textAlign="center">
          {AI_ERROR_TITLE}
        </GiveText>
        <GiveText variant="bodyXS" color="secondary" textAlign="center">
          {AI_ERROR_SUBTITLE}
        </GiveText>
      </Stack>
      {canRetry && (
        <GiveButton
          variant="filled"
          size="small"
          label={AI_RETRY_LABEL}
          startIcon={<AISparkleIcon />}
          onClick={onRetry}
          data-testid="ai-translation-retry"
        />
      )}
    </StatusStack>
  );
}
 
function IconBadge({ children }: { children: React.ReactNode }) {
  const { palette } = useAppTheme();
  return (
    <Box
      padding="8px"
      bgcolor={palette.primitive?.transparent["darken-5"]}
      borderRadius="30px"
      width="36px"
      height="36px"
    >
      {children}
    </Box>
  );
}
 
const StatusStack = styled(Stack)({
  gap: "20px",
  alignItems: "center",
  justifyContent: "center",
});
 
const SummaryText = styled(GiveText)({
  whiteSpace: "pre-wrap",
});