|
| 1 | +import React, { useCallback, useRef } from "react"; |
| 2 | +import { Dimensions, Pressable, View } from "react-native"; |
| 3 | +import { FlashList } from "@shopify/flash-list"; |
| 4 | +import { format } from "date-fns"; |
| 5 | + |
| 6 | +import { Text } from "~/components/ui/text"; |
| 7 | +import { cn } from "~/lib/utils"; |
| 8 | +import { useDateStore } from "~/stores/dateStore"; |
| 9 | + |
| 10 | +const mockScoreData = [ |
| 11 | + { date: new Date(2024, 6, 29), score: 75 }, |
| 12 | + { date: new Date(2024, 6, 30), score: 82 }, |
| 13 | + { date: new Date(2024, 6, 31), score: 58 }, |
| 14 | + { date: new Date(2024, 7, 1), score: 93 }, |
| 15 | + { date: new Date(2024, 7, 2), score: 69 }, |
| 16 | + { date: new Date(2024, 7, 3), score: 87 }, |
| 17 | + { date: new Date(2024, 7, 4), score: 45 }, |
| 18 | + { date: new Date(2024, 7, 5), score: 91 }, |
| 19 | + { date: new Date(2024, 7, 6), score: 63 }, |
| 20 | + { date: new Date(2024, 7, 7), score: 79 }, |
| 21 | +]; |
| 22 | + |
| 23 | +export type ScoreData = (typeof mockScoreData)[number]; |
| 24 | + |
| 25 | +const screenWidth = Dimensions.get("window").width; |
| 26 | +const itemWidth = 48; // 12 (width) + 4 (separator) * 3 |
| 27 | +const centerOffset = (screenWidth - itemWidth) / 2; |
| 28 | +const lastIndex = mockScoreData.length - 1; |
| 29 | + |
| 30 | +const DayItem = React.memo( |
| 31 | + ({ |
| 32 | + item, |
| 33 | + isSelected, |
| 34 | + onPress, |
| 35 | + }: { |
| 36 | + item: ScoreData; |
| 37 | + isSelected: boolean; |
| 38 | + onPress: () => void; |
| 39 | + }) => ( |
| 40 | + <Pressable onPress={onPress} className="h-20 w-16"> |
| 41 | + <View className="flex-col items-center gap-1"> |
| 42 | + <View |
| 43 | + className={cn( |
| 44 | + "h-6 w-6 items-center justify-center rounded-full", |
| 45 | + isSelected && "bg-primary", |
| 46 | + )} |
| 47 | + > |
| 48 | + <Text |
| 49 | + className={cn( |
| 50 | + "text-xs font-semibold text-gray-400", |
| 51 | + isSelected ? "text-black" : "text-gray-400", |
| 52 | + )} |
| 53 | + > |
| 54 | + {format(item.date, "EEEEE").toUpperCase()} |
| 55 | + </Text> |
| 56 | + </View> |
| 57 | + |
| 58 | + <View className="h-12 w-12 items-center justify-center rounded-full bg-green-900"> |
| 59 | + <Text className="text-xl font-semibold">{item.score}</Text> |
| 60 | + </View> |
| 61 | + </View> |
| 62 | + </Pressable> |
| 63 | + ), |
| 64 | +); |
| 65 | + |
| 66 | +export function DaySlider() { |
| 67 | + const { selectedDate, setSelectedDate } = useDateStore(); |
| 68 | + const listRef = useRef<FlashList<ScoreData> | null>(null); |
| 69 | + |
| 70 | + const scrollToIndex = (index: number) => { |
| 71 | + listRef.current?.scrollToIndex({ |
| 72 | + index, |
| 73 | + animated: true, |
| 74 | + viewPosition: 0.5, // This centers the item |
| 75 | + }); |
| 76 | + }; |
| 77 | + |
| 78 | + const renderItem = useCallback( |
| 79 | + ({ item, index }: { item: ScoreData; index: number }) => { |
| 80 | + const isSelected = |
| 81 | + item.date.toDateString() === selectedDate.toDateString(); |
| 82 | + return ( |
| 83 | + <DayItem |
| 84 | + item={item} |
| 85 | + isSelected={isSelected} |
| 86 | + onPress={() => { |
| 87 | + setSelectedDate(item.date); |
| 88 | + scrollToIndex(index); |
| 89 | + }} |
| 90 | + /> |
| 91 | + ); |
| 92 | + }, |
| 93 | + [selectedDate, setSelectedDate], |
| 94 | + ); |
| 95 | + |
| 96 | + const keyExtractor = useCallback( |
| 97 | + (item: ScoreData) => item.date.toISOString(), |
| 98 | + [], |
| 99 | + ); |
| 100 | + |
| 101 | + return ( |
| 102 | + <FlashList |
| 103 | + ref={listRef} |
| 104 | + data={mockScoreData} |
| 105 | + extraData={selectedDate} |
| 106 | + renderItem={renderItem} |
| 107 | + keyExtractor={keyExtractor} |
| 108 | + estimatedItemSize={48} |
| 109 | + horizontal |
| 110 | + showsHorizontalScrollIndicator={false} |
| 111 | + initialScrollIndex={lastIndex} |
| 112 | + // ItemSeparatorComponent={() => <View className="w-4" />} |
| 113 | + contentContainerStyle={{ |
| 114 | + paddingBottom: 4, |
| 115 | + paddingTop: 4, |
| 116 | + paddingLeft: 8, |
| 117 | + paddingRight: centerOffset, |
| 118 | + }} |
| 119 | + /> |
| 120 | + ); |
| 121 | +} |
0 commit comments