|
| 1 | +import { useId, useState, type ReactNode } from "react"; |
| 2 | +import { ScrollView, View } from "react-native"; |
| 3 | +import Svg, { Defs, LinearGradient, Rect, Stop } from "react-native-svg"; |
| 4 | + |
| 5 | +import { WorkLogBlock } from "./work-log-layout"; |
| 6 | + |
| 7 | +const FADE_HEIGHT = 24; |
| 8 | + |
| 9 | +/** A capped thought box; a clipped thought fades out so the next reply does not read as part of it. */ |
| 10 | +export function ReasoningGroupScroll(props: { |
| 11 | + readonly continues: boolean | undefined; |
| 12 | + readonly fadeColor: string; |
| 13 | + readonly children: ReactNode; |
| 14 | +}) { |
| 15 | + const [viewportHeight, setViewportHeight] = useState(0); |
| 16 | + const [contentHeight, setContentHeight] = useState(0); |
| 17 | + const [atEnd, setAtEnd] = useState(false); |
| 18 | + const clipped = contentHeight > viewportHeight + 1; |
| 19 | + return ( |
| 20 | + <WorkLogBlock continues={props.continues}> |
| 21 | + <View className="ml-7"> |
| 22 | + <ScrollView |
| 23 | + nestedScrollEnabled |
| 24 | + className="max-h-96 py-1" |
| 25 | + scrollEventThrottle={64} |
| 26 | + onLayout={(event) => setViewportHeight(event.nativeEvent.layout.height)} |
| 27 | + onContentSizeChange={(_, height) => setContentHeight(height)} |
| 28 | + onScroll={({ nativeEvent }) => |
| 29 | + setAtEnd( |
| 30 | + nativeEvent.contentOffset.y + nativeEvent.layoutMeasurement.height >= |
| 31 | + nativeEvent.contentSize.height - 1, |
| 32 | + ) |
| 33 | + } |
| 34 | + > |
| 35 | + {props.children} |
| 36 | + </ScrollView> |
| 37 | + {clipped && !atEnd ? <BottomFade color={props.fadeColor} /> : null} |
| 38 | + </View> |
| 39 | + </WorkLogBlock> |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +function BottomFade(props: { readonly color: string }) { |
| 44 | + const gradientId = `reasoning-fade-${useId().replaceAll(":", "")}`; |
| 45 | + return ( |
| 46 | + <View |
| 47 | + pointerEvents="none" |
| 48 | + className="absolute inset-x-0 bottom-0" |
| 49 | + style={{ height: FADE_HEIGHT }} |
| 50 | + > |
| 51 | + <Svg width="100%" height="100%"> |
| 52 | + <Defs> |
| 53 | + <LinearGradient id={gradientId} x1="0%" x2="0%" y1="0%" y2="100%"> |
| 54 | + <Stop offset={0} stopColor={props.color} stopOpacity={0} /> |
| 55 | + <Stop offset={1} stopColor={props.color} stopOpacity={1} /> |
| 56 | + </LinearGradient> |
| 57 | + </Defs> |
| 58 | + <Rect width="100%" height="100%" fill={`url(#${gradientId})`} /> |
| 59 | + </Svg> |
| 60 | + </View> |
| 61 | + ); |
| 62 | +} |
0 commit comments