import React, {memo, useState} from "react"; import {Button, Picker, PickerModes, SegmentedControl, Text, View} from "react-native-ui-lib"; import {MaterialIcons} from "@expo/vector-icons"; import {months} from "./constants"; import {StyleSheet} from "react-native"; import {useAtom} from "jotai"; import {modeAtom, selectedDateAtom} from "@/components/pages/calendar/atoms"; import {format, isSameDay} from "date-fns"; import * as Device from "expo-device"; import {Mode} from "react-native-big-calendar"; export const CalendarHeader = memo(() => { const [selectedDate, setSelectedDate] = useAtom(selectedDateAtom); const [mode, setMode] = useAtom(modeAtom); const [tempIndex, setTempIndex] = useState(null); const isTablet = Device.deviceType === Device.DeviceType.TABLET; const segments = isTablet ? [{label: "D"}, {label: "W"}, {label: "M"}] : [{label: "D"}, {label: "3D"}, {label: "M"}]; const handleSegmentChange = (index: number) => { let selectedMode: Mode; if (isTablet) { selectedMode = ["day", "week", "month"][index] as Mode; } else { selectedMode = ["day", "3days", "month"][index] as Mode; } setTempIndex(index); if (selectedMode) { setTimeout(() => { setMode(selectedMode as "day" | "week" | "month" | "3days"); setTempIndex(null); }, 150); } }; const handleMonthChange = (month: string) => { const currentDay = selectedDate.getDate(); const currentYear = selectedDate.getFullYear(); const newMonthIndex = months.indexOf(month); const updatedDate = new Date(currentYear, newMonthIndex, currentDay); setSelectedDate(updatedDate); }; const isSelectedDateToday = isSameDay(selectedDate, new Date()); const getInitialIndex = () => { if (isTablet) { switch (mode) { case "day": return 0; case "week": return 1; case "month": return 2; default: return 1; } } else { switch (mode) { case "day": return 0; case "3days": return 1; case "month": return 2; default: return 1; } } }; return ( {isTablet && ( {selectedDate.getFullYear()} )} handleMonthChange(itemValue as string)} trailingAccessory={} topBarProps={{ title: selectedDate.getFullYear().toString(), titleStyle: {fontFamily: "Manrope_500Medium", fontSize: 17}, }} > {months.map((month) => ( ))} ); }); const styles = StyleSheet.create({ segmentslblStyle: { fontSize: 12, fontFamily: "Manrope_600SemiBold", }, todayButton: { backgroundColor: "transparent", borderWidth: 0, height: 30, width: 30, alignItems: 'center', justifyContent: 'center', }, todayDate: { position: 'absolute', fontSize: 12, fontFamily: "Manrope_600SemiBold", color: "#5f6368", top: '30%', }, });