Skip to content

Commit

Permalink
非効率なコードの修正
Browse files Browse the repository at this point in the history
  • Loading branch information
TinyKitten committed Jul 28, 2024
1 parent 0e09049 commit 2ed7a52
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 28 deletions.
12 changes: 6 additions & 6 deletions src/hooks/useAnonymousUser.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import type { FirebaseAuthTypes } from '@react-native-firebase/auth'
import auth from '@react-native-firebase/auth'
import { useEffect, useState } from 'react'
import { useEffect, useRef } from 'react'

/**
* Recoilが使えない環境の時にもユーザーを持ちたい場合に使います。基本的に `useCachedAnonymousUser` を使ってください。
*/
const useAnonymousUser = (): FirebaseAuthTypes.User | undefined => {
const [user, setUser] = useState<FirebaseAuthTypes.User>()
const userRef = useRef<FirebaseAuthTypes.User>()
useEffect(() => {
const unsubscribe = auth().onAuthStateChanged((authUser) => {
if (authUser) {
setUser(authUser)
userRef.current = authUser
} else {
auth()
.signInAnonymously()
.then((credential) => setUser(credential.user))
.then((credential) => (userRef.current = credential.user))
}
})
return unsubscribe
}, [setUser, user])
}, [])

return user
return userRef.current
}

export default useAnonymousUser
26 changes: 4 additions & 22 deletions src/hooks/useTTS.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { Audio, InterruptionModeAndroid, InterruptionModeIOS } from 'expo-av'
import * as FileSystem from 'expo-file-system'
import { useCallback, useEffect, useMemo, useRef } from 'react'
import DeviceInfo from 'react-native-device-info'
import {
DEV_TTS_API_URL,
LOCAL_TTS_API_URL,
PRODUCTION_TTS_API_URL,
} from 'react-native-dotenv'
import { DEV_TTS_API_URL, PRODUCTION_TTS_API_URL } from 'react-native-dotenv'
import { useRecoilValue } from 'recoil'
import speechState from '../store/atoms/speech'
import { isDevApp } from '../utils/isDevApp'
import useAnonymousUser from './useAnonymousUser'
import useConnectivity from './useConnectivity'
import { usePrevious } from './usePrevious'
import { useTTSCache } from './useTTSCache'
import useTTSText from './useTTSText'
Expand All @@ -26,7 +20,7 @@ export const useTTS = (): void => {
const ttsText = useTTSText(firstSpeechRef.current)
const [prevTextJa, prevTextEn] = usePrevious(ttsText)
const [textJa, textEn] = ttsText
const isInternetAvailable = useConnectivity()

const user = useAnonymousUser()

const soundJaRef = useRef<Audio.Sound | null>(null)
Expand Down Expand Up @@ -80,9 +74,6 @@ export const useTTS = (): void => {
}, [])

const ttsApiUrl = useMemo(() => {
if (__DEV__ && DeviceInfo.isEmulatorSync()) {
return LOCAL_TTS_API_URL
}
return isDevApp ? DEV_TTS_API_URL : PRODUCTION_TTS_API_URL
}, [])

Expand Down Expand Up @@ -173,9 +164,8 @@ export const useTTS = (): void => {

useEffect(() => {
if (
playingRef.current ||
!enabled ||
!isInternetAvailable ||
playingRef.current ||
prevTextJa === textJa ||
prevTextEn === textEn
) {
Expand All @@ -186,15 +176,7 @@ export const useTTS = (): void => {
textJa,
textEn,
})
}, [
enabled,
isInternetAvailable,
prevTextEn,
prevTextJa,
speech,
textEn,
textJa,
])
}, [enabled, prevTextEn, prevTextJa, speech, textEn, textJa])

useEffect(() => {
return () => {
Expand Down

0 comments on commit 2ed7a52

Please sign in to comment.