All files / apps/chatbot/src/components/MessageList/blocks ConfirmationBlock.tsx

88.46% Statements 23/26
80% Branches 20/25
85.71% Functions 6/7
95.45% Lines 21/22

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 151 152 153 154                                          1x             12x 12x 12x 12x   12x 6x     12x   12x       12x 20x   12x 1x 1x 1x     12x   12x 2x                 10x                                             1x                           12x                                                                                                        
import React, { useEffect, useState } from "react";
import { ActivityIndicator, Pressable, Text, View } from "react-native";
 
import { useTheme } from "@repo/ui/themes";
import { makeStyles } from "@repo/ui/themes/makeStyles";
import { textStyles } from "@repo/ui/themes/typography";
 
import { useStyles as useSharedStyles } from "@/components/styles";
 
import { usePendingActionCountdown } from "@/hooks/usePendingActionCountdown";
 
import { Block } from "@/types/chat";
 
interface ConfirmationBlockProps {
  block: Block;
  msgId: string;
  pendingAction: any;
  isActionProcessing?: boolean;
  handleAction: (msgId: string, actionId: string, type: "confirm" | "cancel") => void;
}
 
export const ConfirmationBlock = ({
  block,
  msgId,
  pendingAction,
  isActionProcessing,
  handleAction,
}: ConfirmationBlockProps) => {
  const shared = useSharedStyles();
  const styles = useStyles();
  const theme = useTheme();
  const [submittingType, setSubmittingType] = useState<"confirm" | "cancel" | null>(null);
 
  useEffect(() => {
    Eif (!isActionProcessing) setSubmittingType(null);
  }, [isActionProcessing]);
 
  const { isExpired, countdownLabel } = usePendingActionCountdown(pendingAction);
 
  const isLoading = isActionProcessing || submittingType !== null;
 
  // The pressed button shows its own spinner; when the action is already in
  // flight from elsewhere both buttons spin, since neither one owns it.
  const isSpinning = (type: "confirm" | "cancel") =>
    submittingType === type || (isLoading && submittingType === null);
 
  const handleActionWithInstantFeedback = (type: "confirm" | "cancel") => {
    Iif (isLoading) return;
    setSubmittingType(type);
    handleAction(msgId, pendingAction.id, type);
  };
 
  Iif (!pendingAction) return null;
 
  if (isExpired) {
    return (
      <View style={styles.blockConfirmation} testID="chat-expired-confirmation">
        <Text style={shared.blockText}>
          This request has expired. Please send it again if you still want to proceed.
        </Text>
      </View>
    );
  }
 
  return (
    <View style={styles.blockConfirmation}>
      <Text style={styles.confirmationTitle}>{block.title || "Please confirm"}</Text>
      <Text style={styles.confirmationDesc}>{block.description}</Text>
      {countdownLabel !== null && (
        <Text style={styles.confirmationCountdown} testID="chat-confirmation-countdown">
          {countdownLabel}
        </Text>
      )}
      <View style={styles.actionRow}>
        <Pressable
          style={[styles.cancelBtn, isLoading && shared.disabledSendBtn]}
          onPress={() => handleActionWithInstantFeedback("cancel")}
          disabled={isLoading}
        >
          {isSpinning("cancel") ? (
            <ActivityIndicator color={theme.theme.colors.gray90} />
          ) : (
            <Text style={styles.cancelText}>Cancel</Text>
          )}
        </Pressable>
        <Pressable
          style={[styles.confirmBtn, isLoading && shared.disabledSendBtn]}
          onPress={() => handleActionWithInstantFeedback("confirm")}
          disabled={isLoading}
        >
          {isSpinning("confirm") ? (
            <ActivityIndicator color={theme.theme.colors.white} />
          ) : (
            <Text style={styles.confirmText}>Confirm</Text>
          )}
        </Pressable>
      </View>
    </View>
  );
};
 
const useStyles = makeStyles((theme) => ({
  blockConfirmation: {
    backgroundColor: theme.colors.white,
  },
  confirmationTitle: {
    ...textStyles.content.semiBold,
    fontSize: 14,
    marginBottom: 4,
  },
  confirmationDesc: {
    ...textStyles.content.regular,
    fontSize: 13,
    color: theme.colors.text.muted,
    marginBottom: 12,
  },
  confirmationCountdown: {
    ...textStyles.content.regular,
    fontSize: 12,
    color: theme.colors.text.muted,
    marginBottom: 8,
  },
  actionRow: {
    flexDirection: "row",
    width: "100%",
    justifyContent: "space-between",
    gap: 8,
  },
  confirmBtn: {
    justifyContent: "center",
    alignItems: "center",
    borderRadius: 8,
    width: "48%",
    height: 40,
    backgroundColor: theme.colors.badge.actionDone,
  },
  confirmText: {
    ...textStyles.content.regular,
    color: theme.colors.white,
  },
  cancelBtn: {
    justifyContent: "center",
    alignItems: "center",
    borderRadius: 8,
    width: "48%",
    height: 40,
    backgroundColor: theme.colors.badge.actionCancel,
  },
  cancelText: {
    ...textStyles.content.regular,
    color: theme.colors.gray90,
  },
}));