import MaskedView from '@react-native-masked-view/masked-view'
import { X, RefreshCw, SendHorizontal } from '@tamagui/lucide-icons'
import { useRef, useState, useEffect, useCallback, memo } from 'react'
import { NativeSyntheticEvent, NativeScrollEvent } from 'react-native'
import { XStack, YStack, ZStack, Text, Button, ScrollView, Input } from 'tamagui'
import { LinearGradient } from 'tamagui/linear-gradient'
const buttonProps = {
w: '$4',
h: '$4',
br: '$10',
chromeless: true,
backgroundColor: 'rgba(0, 0, 0, 0.2)',
pressStyle: {
backgroundColor: 'rgba(0, 0, 0, 0.3)',
borderColor: 'transparent',
},
} as const
export type Comment = {
id?: string
commenter: string
text: string
time: string
}
export type CameraViewProps = {
children?: React.ReactNode
onCloseCamera: () => void
onFlipCamera: () => void
commentsQueue: Comment[]
setCommentsQueue: (comments: Comment[]) => void
}
const TopActionBar = memo(
({ onCloseCamera, onFlipCamera }: { onCloseCamera: () => void; onFlipCamera: () => void }) => (
)
)
const CommentItem = memo(({ comment }: { comment: Comment }) => (
{comment.commenter}
{new Date(comment.time).toLocaleTimeString()}
{comment.text}
))
const CommentsViewport = memo(
({
commentsQueue,
setCommentsQueue,
}: {
commentsQueue: Comment[]
setCommentsQueue: (comments: Comment[]) => void
}) => {
const scrollViewRef = useRef(null)
const [autoScroll, setAutoScroll] = useState(true)
const [displayedComments, setDisplayedComments] = useState([])
useEffect(() => {
if (commentsQueue.length === 0) return
const displayNewComments = async () => {
if (commentsQueue.length === 0) return
const processQueue = async () => {
for (const comment of commentsQueue) {
const delay = comment.text.length * 50
await new Promise((resolve) => setTimeout(resolve, delay))
setDisplayedComments((prev) => [...prev, comment])
}
setCommentsQueue([])
}
processQueue()
}
displayNewComments()
}, [commentsQueue, setCommentsQueue])
const handleScroll = useCallback((event: NativeSyntheticEvent) => {
const { contentOffset, contentSize, layoutMeasurement } = event.nativeEvent
if (!contentOffset) return
const distanceFromBottom =
(contentSize?.height || 0) - (contentOffset.y + layoutMeasurement?.height || 0)
const isNearBottom = distanceFromBottom < 10
setAutoScroll(isNearBottom)
}, [])
return (
}
>
}
>
{
if (autoScroll) {
scrollViewRef.current?.scrollToEnd({ animated: false })
}
}}
bounces={false}
showsVerticalScrollIndicator={false}
scrollEnabled
nestedScrollEnabled
contentContainerStyle={{
flexGrow: 1,
justifyContent: 'flex-start',
paddingHorizontal: 16,
paddingBottom: 16,
}}
automaticallyAdjustKeyboardInsets
>
{displayedComments.map((comment) => (
))}
)
}
)
export const CameraView = memo(({ children, ...props }: CameraViewProps) => (
{children}
))