Skip to content

Commit

Permalink
Merge pull request #3841 from TrainLCD/fix/deep-link
Browse files Browse the repository at this point in the history
ディープリンク処理タイミング修正
  • Loading branch information
TinyKitten authored Oct 21, 2024
2 parents 28df4be + b6ab37e commit 20a18b8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
20 changes: 14 additions & 6 deletions src/hooks/useDeepLink.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -36,7 +36,15 @@ export const useDeepLink = () => {
})
}
}
},
[openRoute]
)

useEffect(() => {
const listener = Linking.addEventListener('url', handleURL)

return () => {
listener.remove()
}
getUrlAsync()
}, [openRoute, url])
}, [handleURL])
}
21 changes: 3 additions & 18 deletions src/hooks/useOpenRouteFromLink.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)

Expand All @@ -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,
Expand All @@ -74,14 +66,7 @@ export const useOpenRouteFromLink = () => {
}))
navigation.navigate('Main')
},
[
navigation,
resetState,
selectedBound,
setLineState,
setNavigationState,
setStationState,
]
[navigation, setLineState, setNavigationState, setStationState]
)

const openLink = useCallback(
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useResetMainState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const useResetMainState = () => {
bottomState: 'LINE',
leftStations: [],
stationForHeader: null,
fromBuilder: false,
}))
setStationState((prev) => ({
...prev,
Expand Down

0 comments on commit 20a18b8

Please sign in to comment.