All files / apps/chatbot/src/components/Welcome index.tsx

100% Statements 12/12
89.47% Branches 17/19
100% Functions 4/4
100% Lines 10/10

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   1x 11x                               1x               11x 11x 11x 11x   11x                                                                                                   11x                                                                                                            
import React from "react";
import Animated, { Easing, FadeInDown, FadeOutUp } from "react-native-reanimated";
import { ActivityIndicator, Text, TouchableOpacity, View } from "react-native";
 
import { RotateCcwIcon } from "@repo/ui/icons";
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 { getGreeting } from "@/utils/greeting";
 
import { useWelcomeTextStyles } from "./styles";
import { WelcomeTextSkeleton } from "./WelcomeTextSkeleton";
 
/** A dropped connection is worth rewording; anything else is shown verbatim. */
const CONNECTION_FAILURE_HINTS = ["Network request failed", "Failed to create thread"];
 
const toDisplayError = (initError: string) =>
  CONNECTION_FAILURE_HINTS.some((hint) => initError.includes(hint))
    ? "Could not connect to server. Please check your connection and try again."
    : initError;
 
interface WelcomeProps {
  userName?: string;
  /** The agent's own sentence about what it can do; empty until the thread exists. */
  description: string;
  initError: string | null;
  /** The first thread creation is still in flight — nothing to show yet. */
  isInitialising: boolean;
  /** Any thread creation is in flight, including a retry from the error state. */
  isInitializing: boolean;
  onReset: () => void;
}
 
export const Welcome = ({
  userName,
  description,
  initError,
  isInitialising,
  isInitializing,
  onReset,
}: WelcomeProps) => {
  const shared = useSharedStyles();
  const styles = useStyles();
  const { theme } = useTheme();
  const textStyle = useWelcomeTextStyles();
 
  return (
    <View style={styles.centeredMessageContainer}>
      <Animated.View
        entering={FadeInDown.duration(220)}
        exiting={FadeOutUp.duration(260).easing(Easing.out(Easing.ease))}
      >
        <View style={styles.welcomeBlocksContainer}>
          <Text style={styles.welcomeGreetingText}>
            {userName ? `${getGreeting()}, ${userName}!` : `${getGreeting()}!`}
          </Text>
          {description ? (
            <Text style={textStyle.welcomeDescriptionText}>{description}</Text>
          ) : (
            // Only while the thread is actually being created. A finished init
            // that produced no text block leaves the sentence out entirely —
            // shimmering on for a sentence that is never coming reads as a
            // hung screen, and the client still must not invent one.
            isInitialising && !initError && <WelcomeTextSkeleton />
          )}
        </View>
 
        {initError && (
          <Animated.View entering={FadeInDown.duration(220)} style={styles.welcomeErrorContainer}>
            <Text style={styles.welcomeErrorText}>{toDisplayError(initError)}</Text>
            <TouchableOpacity
              testID="chat-welcome-retry"
              accessibilityRole="button"
              accessibilityLabel="Try again"
              accessibilityState={{ disabled: isInitializing }}
              style={[styles.tryAgainBtn, isInitializing && shared.disabledSendBtn]}
              onPress={onReset}
              disabled={isInitializing}
              activeOpacity={0.8}
            >
              {isInitializing ? (
                <ActivityIndicator color={theme.colors.white} size="small" />
              ) : (
                <>
                  <RotateCcwIcon width={16} height={16} color={theme.colors.white} />
                  <Text style={styles.tryAgainText}>Try Again</Text>
                </>
              )}
            </TouchableOpacity>
          </Animated.View>
        )}
      </Animated.View>
    </View>
  );
};
 
const useStyles = makeStyles((theme) => ({
  centeredMessageContainer: {
    flex: 1,
    justifyContent: "center",
  },
  welcomeBlocksContainer: {
    gap: 8,
    width: "100%",
    paddingHorizontal: 12,
    paddingVertical: 4,
    alignItems: "center",
  },
  welcomeGreetingText: {
    ...textStyles.content.semiBold,
    fontSize: 24,
    color: theme.colors.text.primary,
    lineHeight: 32,
    textAlign: "center",
    marginBottom: 4,
  },
  welcomeErrorContainer: {
    alignItems: "center",
    justifyContent: "center",
    paddingHorizontal: 24,
    paddingVertical: 16,
    marginTop: 12,
  },
  welcomeErrorText: {
    ...textStyles.content.regular,
    fontSize: 14,
    color: theme.colors.text.error,
    textAlign: "center",
    lineHeight: 20,
    marginBottom: 16,
    maxWidth: 280,
  },
  tryAgainBtn: {
    flexDirection: "row",
    alignItems: "center",
    justifyContent: "center",
    gap: 8,
    backgroundColor: theme.isNewTheme
      ? theme.colors.background.selectedStrong
      : theme.colors.text.onBehalf || "#007AFF",
    paddingVertical: 10,
    paddingHorizontal: 20,
    borderRadius: 20,
  },
  tryAgainText: {
    ...textStyles.content.semiBold,
    fontSize: 14,
    color: theme.colors.white,
  },
}));