From b6ab37ee95ad622eff55a6c3f353b5d1176d2fe1 Mon Sep 17 00:00:00 2001 From: Tsubasa SEKIGUCHI Date: Mon, 21 Oct 2024 11:53:27 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=87=E3=82=A3=E3=83=BC=E3=83=97=E3=83=AA?= =?UTF-8?q?=E3=83=B3=E3=82=AF=E5=87=A6=E7=90=86=E3=82=BF=E3=82=A4=E3=83=9F?= =?UTF-8?q?=E3=83=B3=E3=82=B0=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useDeepLink.ts | 20 ++++++++++++++------ src/hooks/useOpenRouteFromLink.ts | 21 +++------------------ src/hooks/useResetMainState.ts | 1 + 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/hooks/useDeepLink.ts b/src/hooks/useDeepLink.ts index 2b2e1dc3e..ce0e70394 100644 --- a/src/hooks/useDeepLink.ts +++ b/src/hooks/useDeepLink.ts @@ -1,13 +1,13 @@ import * as Linking from 'expo-linking' -import { useEffect } from 'react' +import { useCallback, useEffect } from 'react' import { useOpenRouteFromLink } from './useOpenRouteFromLink' export const useDeepLink = () => { - const url = Linking.useURL() const { openLink: openRoute } = useOpenRouteFromLink() - useEffect(() => { - const getUrlAsync = async () => { + const handleURL = useCallback( + async (event: Linking.EventType) => { + const url = event.url if (url && (await Linking.canOpenURL(url))) { const parsedUrl = Linking.parse(url) if (parsedUrl.queryParams) { @@ -36,7 +36,15 @@ export const useDeepLink = () => { }) } } + }, + [openRoute] + ) + + useEffect(() => { + const listener = Linking.addEventListener('url', handleURL) + + return () => { + listener.remove() } - getUrlAsync() - }, [openRoute, url]) + }, [handleURL]) } diff --git a/src/hooks/useOpenRouteFromLink.ts b/src/hooks/useOpenRouteFromLink.ts index e914a57c1..76c6732c7 100644 --- a/src/hooks/useOpenRouteFromLink.ts +++ b/src/hooks/useOpenRouteFromLink.ts @@ -1,7 +1,7 @@ import { useMutation } from '@connectrpc/connect-query' import { useNavigation } from '@react-navigation/native' import { useCallback } from 'react' -import { useRecoilState, useSetRecoilState } from 'recoil' +import { useSetRecoilState } from 'recoil' import { getStationsByLineGroupId, getStationsByLineId, @@ -15,13 +15,11 @@ import { LineDirection } from '../models/Bound' import lineState from '../store/atoms/line' import navigationState from '../store/atoms/navigation' import stationState from '../store/atoms/station' -import { useResetMainState } from './useResetMainState' export const useOpenRouteFromLink = () => { const navigation = useNavigation() - const resetState = useResetMainState() - const [{ selectedBound }, setStationState] = useRecoilState(stationState) + const setStationState = useSetRecoilState(stationState) const setNavigationState = useSetRecoilState(navigationState) const setLineState = useSetRecoilState(lineState) @@ -42,17 +40,11 @@ export const useOpenRouteFromLink = () => { stations: Station[], direction: LineDirection | null ) => { - if (selectedBound) { - return - } - const line = station?.line if (!line) { return } - resetState() - setStationState((prev) => ({ ...prev, stations, @@ -74,14 +66,7 @@ export const useOpenRouteFromLink = () => { })) navigation.navigate('Main') }, - [ - navigation, - resetState, - selectedBound, - setLineState, - setNavigationState, - setStationState, - ] + [navigation, setLineState, setNavigationState, setStationState] ) const openLink = useCallback( diff --git a/src/hooks/useResetMainState.ts b/src/hooks/useResetMainState.ts index 402d96685..182f2e853 100644 --- a/src/hooks/useResetMainState.ts +++ b/src/hooks/useResetMainState.ts @@ -15,6 +15,7 @@ export const useResetMainState = () => { bottomState: 'LINE', leftStations: [], stationForHeader: null, + fromBuilder: false, })) setStationState((prev) => ({ ...prev,