import { useHeaderHeight } from "@react-navigation/elements" import { useAtomValue } from "jotai" import { memo, useEffect, useRef, useState } from "react" import type { TouchableOpacityProps } from "react-native" import { ScrollView, StyleSheet, Text, TouchableOpacity, View } from "react-native" import type { WithSpringConfig } from "react-native-reanimated" import Animated, { useAnimatedStyle, useSharedValue, withSpring } from "react-native-reanimated" import { ThemedBlurView } from "@/src/components/common/ThemedBlurView" import { ContextMenu } from "@/src/components/ui/context-menu" import { bottomViewTabHeight } from "@/src/constants/ui" import type { ViewDefinition } from "@/src/constants/views" import { views } from "@/src/constants/views" import { useUnreadCountByView } from "@/src/store/unread/hooks" import { unreadSyncService } from "@/src/store/unread/store" import { offsetAtom, setCurrentView, viewAtom } from "./atoms" const springConfig: WithSpringConfig = { damping: 20, mass: 1, stiffness: 120, } export const ViewTab = () => { const headerHeight = useHeaderHeight() const offset = useAtomValue(offsetAtom) const currentView = useAtomValue(viewAtom) const tabRef = useRef(null) const indicatorPosition = useSharedValue(0) const [tabWidths, setTabWidths] = useState([]) const [tabPositions, setTabPositions] = useState([]) const scrollOffsetX = useRef(0) useEffect(() => { if (tabWidths.length > 0) { indicatorPosition.value = withSpring(tabPositions[currentView] || 0, springConfig) if (tabRef.current) { const x = currentView > 0 ? tabPositions[currentView - 1] + tabWidths[currentView - 1] : 0 const isCurrentTabVisible = scrollOffsetX.current < tabPositions[currentView] && scrollOffsetX.current + tabWidths[currentView] > tabPositions[currentView] if (!isCurrentTabVisible) { tabRef.current.scrollTo({ x, y: 0, animated: true }) } } } }, [currentView, indicatorPosition, tabPositions, tabWidths]) const indicatorStyle = useAnimatedStyle(() => { return { transform: [ { translateX: indicatorPosition.value + 10 + Math.abs(offset), }, ], backgroundColor: views[currentView].activeColor, width: (tabWidths[currentView] || 20) - 40 + Math.abs(offset), } }) return ( { scrollOffsetX.current = event.nativeEvent.contentOffset.x }} showsHorizontalScrollIndicator={false} className="border-tertiary-system-background" horizontal ref={tabRef} contentContainerStyle={styles.tabScroller} style={styles.root} > {views.map((view, index) => { const isSelected = currentView === view.view return ( { const { width, x } = event.nativeEvent.layout setTabWidths((prev) => { const newWidths = [...prev] newWidths[index] = width return newWidths }) setTabPositions((prev) => { const newPositions = [...prev] newPositions[index] = x return newPositions }) }} /> ) })} ) } const TabItem = memo( ({ isSelected, view, onLayout, }: { isSelected: boolean; view: ViewDefinition } & Pick) => { const unreadCount = useUnreadCountByView(view.view) return ( { switch (e.nativeEvent.index) { case 0: { unreadSyncService.markViewAsRead(view.view) break } } }} onLayout={onLayout} > setCurrentView(view.view)} className="relative mr-4 flex-row items-center justify-center rounded-full" > {view.name} {unreadCount > 0 && ( {unreadCount > 99 ? "99+" : unreadCount} )} ) }, ) const styles = StyleSheet.create({ indicator: { position: "absolute", bottom: 0, height: 2, borderRadius: 1, }, tabContainer: { backgroundColor: "transparent", bottom: 0, left: 0, position: "absolute", width: "100%", top: 0, }, tabScroller: { alignItems: "center", flexDirection: "row", paddingHorizontal: 4, }, root: { paddingHorizontal: 6 }, })