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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | 1x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 6x 6x 1x 2x 24x | import React, { RefObject } from "react";
import { KeyboardStickyView } from "react-native-keyboard-controller";
import Animated, {
FadeInDown,
FadeOut,
SharedValue,
useAnimatedStyle,
} from "react-native-reanimated";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { Platform, ScrollView, Text, TextInput, TouchableOpacity, View } from "react-native";
import { ArrowUpIcon, CameraIcon, ImageIcon, StopIcon } 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 { BOTTOM_SUGGESTION_STAGGER_MS, IOS_COMPOSER_KEYBOARD_OFFSET } from "@/constants/chat";
import { SuggestionChip } from "@/types/chat";
import { ImagePreview } from "./ImagePreview";
interface ComposerProps {
inputRef: RefObject<TextInput | null>;
text: string;
onChangeText: (value: string) => void;
onSend: () => void;
onCancel: () => void;
isStreaming: boolean;
isInitializing: boolean;
isSendDisabled: boolean;
imageUri: string | null;
onRemoveImage: () => void;
onPickImage: () => void;
onLaunchCamera: () => void;
suggestions: (string | SuggestionChip)[];
onSelectSuggestion: (item: string | SuggestionChip) => void;
/** Drives the swap between the send arrow and the stop square. */
streamingProgress: SharedValue<number>;
sendArrowY: SharedValue<number>;
sendArrowOpacity: SharedValue<number>;
}
export const Composer = ({
inputRef,
text,
onChangeText,
onSend,
onCancel,
isStreaming,
isInitializing,
isSendDisabled,
imageUri,
onRemoveImage,
onPickImage,
onLaunchCamera,
suggestions,
onSelectSuggestion,
streamingProgress,
sendArrowY,
sendArrowOpacity,
}: ComposerProps) => {
const shared = useSharedStyles();
const styles = useStyles();
const { theme } = useTheme();
const insets = useSafeAreaInsets();
const animatedSendArrowStyle = useAnimatedStyle(() => ({
transform: [{ translateY: sendArrowY.value }],
opacity: sendArrowOpacity.value * (1 - streamingProgress.value),
}));
const animatedStopStyle = useAnimatedStyle(() => ({
opacity: streamingProgress.value,
transform: [{ scale: streamingProgress.value }],
}));
// iOS keeps its own keyboard inset, so the composer only needs a nudge to sit
// snug against the keyboard; Android gets its bottom inset applied statically.
// The nudge rides on the sticky view's own offset rather than a negative
// marginBottom on the input row: this container clips (see
// `interactiveSticky`), and a negative margin pulled the row's bottom edge
// outside the container, so the keyboard opening sliced the input pill and
// the send button in half.
const openedOffset = Platform.OS === "ios" ? IOS_COMPOSER_KEYBOARD_OFFSET : 0;
const inputBottomPadding = Platform.OS === "ios" ? 0 : insets.bottom + 8;
const isAttachmentDisabled = isStreaming || isInitializing;
return (
<KeyboardStickyView offset={{ opened: openedOffset }} style={styles.interactiveSticky}>
{suggestions.length > 0 && (
// The chips own `entering`; animating the strip too multiplied both
// opacities and stacked both 25px slides, so the strip travelled twice
// as far and read as a late pop. The exit stays here so the strip
// leaves as one piece, and fades in place rather than sliding: the
// composer is anchored to the bottom, so losing this strip drops the
// composer's top edge by its own height and the exiting copy is drawn
// against that new frame. Travelling down on top of that put the chips
// under the input row, on safe-area padding only the host can paint.
<Animated.View exiting={FadeOut.duration(200)} style={styles.bottomSuggestionsContainer}>
<ScrollView
horizontal
showsHorizontalScrollIndicator={false}
// Without this the strip inherits "never": with the keyboard up —
// which on the welcome screen is always, the input autoFocuses —
// the first tap is spent dismissing the keyboard and never reaches
// the chip, so every chip needs tapping twice. "handled" still lets
// a tap on the strip's own background dismiss it.
keyboardShouldPersistTaps="handled"
contentContainerStyle={styles.bottomSuggestionsScrollContent}
>
{suggestions.map((item, index) => {
const label = typeof item === "string" ? item : item.label;
return (
<Animated.View
// Keyed by label, not by index alone: `entering` only runs
// on mount, and a confirm turn swaps the whole set on the
// same bot message. Index-only keys reused the element, so
// the labels changed with no animation at all.
key={`${label}-${index}`}
entering={FadeInDown.delay(index * BOTTOM_SUGGESTION_STAGGER_MS).duration(200)}
>
<TouchableOpacity
style={styles.suggestionItem}
onPress={() => onSelectSuggestion(item)}
>
<Text style={styles.suggestionText}>{label}</Text>
</TouchableOpacity>
</Animated.View>
);
})}
</ScrollView>
</Animated.View>
)}
<ImagePreview uri={imageUri} onRemove={onRemoveImage} />
<View
testID="chat-input-row"
style={[styles.inputRow, { paddingBottom: inputBottomPadding }]}
>
<View style={styles.plusMenuContainer}>
<TouchableOpacity
testID="chat-pick-image-button"
onPress={onPickImage}
style={[isAttachmentDisabled && shared.disabledSendBtn]}
disabled={isAttachmentDisabled}
accessibilityRole="button"
accessibilityLabel="Pick image from gallery"
accessibilityState={{ disabled: isAttachmentDisabled }}
>
<ImageIcon
width={24}
height={24}
color={theme.isNewTheme ? theme.colors.icon.active : undefined}
/>
</TouchableOpacity>
<TouchableOpacity
testID="chat-launch-camera-button"
onPress={onLaunchCamera}
style={[isAttachmentDisabled && shared.disabledSendBtn]}
disabled={isAttachmentDisabled}
accessibilityRole="button"
accessibilityLabel="Take photo"
accessibilityState={{ disabled: isAttachmentDisabled }}
>
<CameraIcon
width={24}
height={24}
color={theme.isNewTheme ? theme.colors.icon.active : undefined}
/>
</TouchableOpacity>
</View>
<TextInput
ref={inputRef}
value={text}
onChangeText={onChangeText}
placeholder="Ask Flash Bot"
placeholderTextColor={theme.colors.gray80}
style={styles.input}
multiline
autoFocus
accessibilityLabel="Ask Flash Bot"
/>
<TouchableOpacity
testID="chat-send-button"
// Not `onSend` directly: TouchableOpacity would hand it the press
// event, which the composer would then try to send as the text.
onPress={isStreaming ? onCancel : () => onSend()}
disabled={isSendDisabled}
accessibilityRole="button"
accessibilityLabel={isStreaming ? "Stop" : "Send message"}
accessibilityState={{ disabled: isSendDisabled }}
>
<View style={[styles.sendBtnContainer, isSendDisabled && shared.disabledSendBtn]}>
<Animated.View
style={[
styles.stopIconWrapper,
animatedStopStyle,
isStreaming ? styles.zIndexTop : styles.zIndexBottom,
]}
pointerEvents={isStreaming ? "auto" : "none"}
>
<StopIcon
width={32}
height={32}
color={theme.isNewTheme ? theme.colors.icon.active : undefined}
/>
</Animated.View>
<Animated.View
style={[
styles.sendIconWrapper,
animatedSendArrowStyle,
isStreaming ? styles.zIndexBottom : styles.zIndexTop,
]}
pointerEvents={isStreaming ? "none" : "auto"}
>
<ArrowUpIcon
width={32}
height={32}
color={theme.colors.white}
detailColor={theme.isNewTheme ? theme.colors.icon.active : undefined}
/>
</Animated.View>
</View>
</TouchableOpacity>
</View>
</KeyboardStickyView>
);
};
const useStyles = makeStyles((theme) => ({
interactiveSticky: {
zIndex: 11,
// The strip's exit animation is drawn after the strip has left the layout,
// against a composer that has already lost the strip's height; without
// this it spills past the composer into the host's safe-area inset, which
// this remote cannot paint over.
overflow: "hidden",
},
inputRow: {
flexDirection: "row",
alignItems: "center",
paddingHorizontal: 10,
paddingTop: 10,
backgroundColor: theme.colors.white,
},
input: {
flex: 1,
backgroundColor: theme.isNewTheme
? theme.colors.background.surface
: theme.colors.background.pageMuted,
borderRadius: 20,
paddingHorizontal: 12,
marginHorizontal: 8,
maxHeight: 100,
paddingVertical: 8,
...textStyles.content.regular,
color: theme.colors.text.onBehalf,
},
plusMenuContainer: {
flexDirection: "row",
alignItems: "center",
gap: 12,
},
sendBtnContainer: {
width: 32,
height: 32,
justifyContent: "center",
alignItems: "center",
},
sendIconWrapper: {
position: "absolute" as const,
},
stopIconWrapper: {
position: "absolute" as const,
},
zIndexTop: {
zIndex: 2,
},
zIndexBottom: {
zIndex: 1,
},
bottomSuggestionsContainer: {
paddingVertical: 8,
backgroundColor: "transparent",
},
bottomSuggestionsScrollContent: {
paddingHorizontal: 12,
gap: 8,
flexDirection: "row",
alignItems: "center",
},
suggestionItem: {
backgroundColor: theme.colors.background.tertiary,
borderRadius: 12,
paddingHorizontal: 12,
paddingVertical: 6,
marginTop: 10,
},
suggestionText: {
...textStyles.content.semiBold,
color: theme.colors.text.primary,
fontSize: 12,
},
}));
|