diff --git a/apps/mobile/src/modules/ai/summary.tsx b/apps/mobile/src/modules/ai/summary.tsx new file mode 100644 index 000000000..0be8c119b --- /dev/null +++ b/apps/mobile/src/modules/ai/summary.tsx @@ -0,0 +1,275 @@ +import { cn } from "@follow/utils" +import MaskedView from "@react-native-masked-view/masked-view" +import * as Haptics from "expo-haptics" +import { LinearGradient } from "expo-linear-gradient" +import type { FC, ReactNode } from "react" +import * as React from "react" +import type { LayoutChangeEvent } from "react-native" +import { + Clipboard, + Pressable, + ScrollView, + StyleSheet, + TextInput, + TouchableOpacity, + View, +} from "react-native" +import Animated, { + useAnimatedStyle, + useSharedValue, + withRepeat, + withSequence, + withSpring, + withTiming, +} from "react-native-reanimated" +import { useSafeAreaInsets } from "react-native-safe-area-context" +import { useColor } from "react-native-uikit-colors" + +import { BottomModal } from "@/src/components/ui/modal/BottomModal" +import { Text } from "@/src/components/ui/typography/Text" +import { AiCuteReIcon } from "@/src/icons/ai_cute_re" +import { CloseCuteReIcon } from "@/src/icons/close_cute_re" +import { CopyCuteReIcon } from "@/src/icons/copy_cute_re" +import { isAndroid, isIOS } from "@/src/lib/platform" +import { toast } from "@/src/lib/toast" + +export const AISummary: FC<{ + className?: string + summary?: string | ReactNode + pending?: boolean + rawSummaryForCopy?: string + error?: string + onRetry?: () => void +}> = ({ className, summary, pending = false, rawSummaryForCopy, error, onRetry }) => { + const opacity = useSharedValue(0.3) + const height = useSharedValue(0) + const [isSheetOpen, setSheetOpen] = React.useState(false) + React.useEffect(() => { + if (pending) { + opacity.value = withRepeat( + withSequence( + withTiming(1, { + duration: 800, + }), + withTiming(0.3, { + duration: 800, + }), + ), + -1, + ) + } + }, [opacity, pending]) + const animatedContentStyle = useAnimatedStyle(() => ({ + height: height.value, + opacity: + height.value === 0 + ? 0 + : withTiming(1, { + duration: 200, + }), + overflow: "hidden", + })) + const [contentHeight, setContentHeight] = React.useState(0) + const measureContent = (event: LayoutChangeEvent) => { + setContentHeight(event.nativeEvent.layout.height + 10) + height.value = withSpring(event.nativeEvent.layout.height + 10, { + dampingRatio: 2, + stiffness: 90, + overshootClamping: true, + restDisplacementThreshold: 0.01, + restSpeedThreshold: 0.01, + duration: 200, + }) + } + const purpleColor = useColor("purple") + + // Check if summary is a React element or string + const isReactElement = React.isValidElement(summary) + const summaryText = typeof summary === "string" ? summary : "" + const summaryTextForSheet = rawSummaryForCopy || summaryText + if (pending || (!summary && !error)) return null + const mainContent = ( + + + + + + AI Summary + + } + > + + AI Summary + + + + {summaryTextForSheet && ( + { + Clipboard.setString(summaryTextForSheet) + Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success) + toast.success("Copied to clipboard") + }} + onLongPress={() => setSheetOpen(true)} + className="bg-quaternary-system-fill rounded-full p-1.5 active:opacity-70" + hitSlop={{ + top: 8, + bottom: 8, + left: 8, + right: 8, + }} + > + + + )} + + + + {error ? ( + + + {error} + + {onRetry && ( + + Retry + + )} + + ) : isReactElement ? ( + {summary} + ) : ( + + )} + + + + + + {error ? ( + + + {error} + + {onRetry && ( + + Retry + + )} + + ) : isReactElement ? ( + {summary} + ) : ( + + {summaryText?.trim()} + + )} + + + + ) + return ( + <> + setSheetOpen(true) : undefined}> + {mainContent} + + setSheetOpen(false)} + text={summaryTextForSheet} + /> + + ) +} +const SelectableTextSheet: FC<{ + visible: boolean + onClose: () => void + text: string +}> = ({ visible, onClose, text }) => { + const insets = useSafeAreaInsets() + const textColor = useColor("label") + const handleCopyAll = () => { + Clipboard.setString(text) + Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success) + onClose() + } + return ( + + + + + + + AI Summary + + + + + + {text} + + + + ) +} + +/** + * A component that allows text selection on both iOS and Android. + * + * https://stackoverflow.com/a/78267868 + */ +function SelectableText({ className, children }: { className?: string; children: ReactNode }) { + if (isIOS) { + return ( + + {children} + + ) + } else { + return ( + + {children} + + ) + } +} +const styles = StyleSheet.create({ + card: { + borderWidth: 0.5, + elevation: 2, + }, +})