import React, { useCallback, useEffect, useRef, useState } from "react"; import { InteractionManager, Text, View } from "react-native"; import LottieView from "lottie-react-native"; import { useResponsive } from "@/lib/responsive"; import { useThemeColors } from "@/lib/theme"; import type { StationProps } from "@/lib/CommonTypes"; import { LED_vision } from "@/components/発車時刻表/LED_vidion"; import { LEDFrame } from "@/components/発車時刻表/LED_Vision_Component/LEDFrame"; type MenuLEDProps = { station: StationProps[]; }; type PreparedLED = { key: string; station: StationProps[]; }; const MemoizedLEDVision = React.memo(LED_vision); const getStationRenderKey = (station: StationProps[]) => station .map(({ StationNumber, Station_JP, lat, lng }) => [StationNumber ?? "", Station_JP, lat, lng].join(":"), ) .join("|"); const MenuLEDComponent = ({ station }: MenuLEDProps) => { const { colors, fixed } = useThemeColors(); const { fontScale, verticalScale } = useResponsive(); const stationKey = getStationRenderKey(station); const latestStationRef = useRef(station); latestStationRef.current = station; const [preparedLED, setPreparedLED] = useState(() => ({ key: stationKey, station, })); const [isLEDReady, setIsLEDReady] = useState(true); const activeStation = preparedLED.key === stationKey ? preparedLED.station : null; useEffect(() => { if (preparedLED.key === stationKey) return; let cancelled = false; setIsLEDReady(false); const task = InteractionManager.runAfterInteractions(() => { if (cancelled) return; setPreparedLED({ key: stationKey, station: latestStationRef.current, }); }); return () => { cancelled = true; task.cancel(); }; }, [preparedLED.key, stationKey]); const handleContentReady = useCallback(() => { setIsLEDReady(true); }, []); const showLoading = activeStation === null || !isLEDReady; const loadingMinHeight = verticalScale(180); return ( {activeStation && ( )} {showLoading && ( LEDを読み込み中… )} ); }; const areMenuLEDPropsEqual = ( previous: MenuLEDProps, next: MenuLEDProps, ) => getStationRenderKey(previous.station) === getStationRenderKey(next.station); export const MenuLED = React.memo(MenuLEDComponent, areMenuLEDPropsEqual); MenuLED.displayName = "MenuLED";