Skip to content

Commit b8efa6e

Browse files
committed
[Add] ページ遷移用のフック、useTransitを追加
1 parent a5ed466 commit b8efa6e

File tree

13 files changed

+82
-63
lines changed

13 files changed

+82
-63
lines changed

app/(main)/MainPage.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import SearchModal from "@/components/templates/MainPage/SearchModal"
1515
import SettingModal from "@/components/templates/MainPage/SettingModal"
1616
import TrackModal from "@/components/templates/MainPage/TrackModal"
1717
import { LOCAL_STORAGE_KEYS } from "@/constants/LocalStorageKeys"
18+
import { PAGE_PATH } from "@/constants/PagePath"
1819
import useBreakPoints from "@/hooks/useBreakPoints"
1920
import usePlayer from "@/hooks/usePlayer"
2021
import useTarckModal from "@/hooks/useTrackModal"
@@ -57,15 +58,15 @@ const MainPage = () => {
5758
)
5859

5960
if (!isDefined(selectedSpotifyPlaylists) && !isDefined(webDAVFolderPaths)) {
60-
router.push("/connect")
61+
router.push(PAGE_PATH.CONNECT_PAGE)
6162
return
6263
}
6364

6465
setIsLoading({
65-
stateChangedOn: "MainPage",
66+
stateChangedOn: PAGE_PATH.MAIN_PAGE,
6667
state: false
6768
})
68-
router.prefetch("/connect")
69+
router.prefetch(PAGE_PATH.CONNECT_PAGE)
6970
}, [setIsLoading, router])
7071

7172
const {

app/callback/spotify/SpotifyApiCallbackPage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { useSearchParams, useRouter } from "next/navigation"
44
import { memo, useEffect, useRef } from "react"
5+
import { PAGE_PATH } from "@/constants/PagePath"
56
import useErrorModal from "@/hooks/useErrorModal"
67
import useSpotifyToken from "@/hooks/useSpotifyToken"
78
import useStorage from "@/hooks/useStorage"
@@ -31,7 +32,7 @@ const SpotifyApiCallbackPage = () => {
3132
} catch (e) {
3233
showError(e)
3334
} finally {
34-
router.push("/connect?provider=spotify")
35+
router.push(`${PAGE_PATH.CONNECT_PAGE}?provider=spotify`)
3536
}
3637
})()
3738
}, [searchParams, router, getAccessToken, showError, userData])

app/connect/ConnectPage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import SpotifyConnector from "../../components/templates/ConnectPage/SpotifyConn
1515
import WebDAVConnector from "../../components/templates/ConnectPage/WebDAVConnector"
1616
import { loadingAtom } from "@/atoms/loadingAtom"
1717
import CardViewDefault from "@/components/parts/CardViewDefault"
18+
import { PAGE_PATH } from "@/constants/PagePath"
1819
import useBreakPoints from "@/hooks/useBreakPoints"
1920

2021
const ConnectPage = () => {
@@ -75,7 +76,7 @@ const ConnectPage = () => {
7576
useEffect(() => {
7677
;(async () => {
7778
setIsLoading({
78-
stateChangedOn: "ConnectPage",
79+
stateChangedOn: PAGE_PATH.CONNECT_PAGE,
7980
state: false
8081
})
8182

atoms/loadingAtom.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import { atom } from "recoil"
2+
import { PagePath } from "@/types/PagePath"
23

34
export const loadingAtom = atom<{
4-
stateChangedOn:
5-
| "initial"
6-
| "MainPage"
7-
| "ConnectPage"
8-
| "SpotifyCallbackPage"
9-
| "SigninPage"
5+
stateChangedOn: PagePath | undefined
106
state: boolean
117
}>({
128
key: "loadingAtom",
139
default: {
14-
stateChangedOn: "initial",
10+
stateChangedOn: undefined,
1511
state: false
1612
}
1713
})

components/layout/Curtain.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import LayoutHeader from "./LayoutHeader"
1111
import NowLoading from "./NowLoading"
1212
import { faviconIndexAtom } from "@/atoms/faviconIndexAtom"
1313
import { loadingAtom } from "@/atoms/loadingAtom"
14+
import { PAGE_PATH } from "@/constants/PagePath"
1415
import { STYLING_VALUES } from "@/constants/StylingValues"
1516
import { ZINDEX_NUMBERS } from "@/constants/ZIndexNumbers"
1617
import useBreakPoints from "@/hooks/useBreakPoints"
@@ -32,8 +33,14 @@ const Curtain = ({ children }: Children) => {
3233
() => `calc(100vh - ${STYLING_VALUES.HEADER_HEIGHT}px)`,
3334
[]
3435
)
35-
const isConnectPage = useMemo(() => pathname === "/connect", [pathname])
36-
const isSigninPage = useMemo(() => pathname === "/signin", [pathname])
36+
const isConnectPage = useMemo(
37+
() => pathname === PAGE_PATH.CONNECT_PAGE,
38+
[pathname]
39+
)
40+
const isSigninPage = useMemo(
41+
() => pathname === PAGE_PATH.SIGNIN_PAGE,
42+
[pathname]
43+
)
3744

3845
const isLoading = useRecoilValue(loadingAtom)
3946
const [className, setClassName] = useState("")
@@ -65,7 +72,7 @@ const Curtain = ({ children }: Children) => {
6572
return
6673
}
6774

68-
if (isLoading.state && isLoading.stateChangedOn !== "initial") {
75+
if (isLoading.state && isDefined(isLoading.stateChangedOn)) {
6976
setIsDisplayLoadingScreen(true)
7077
setClassName("animate__animated animate__fadeIn")
7178
}

components/layout/ErrorModal.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { IoWarningOutline } from "react-icons/io5"
66
import { useRecoilState } from "recoil"
77
import { errorModalConfigAtom } from "@/atoms/errorModalConfigAtom"
88
import { SpotifyAuthError } from "@/classes/SpotifyAuthError"
9+
import { PAGE_PATH } from "@/constants/PagePath"
910
import { ErrorModalConfig } from "@/types/ErrorModalConfig"
1011

1112
const ErrorModal = () => {
@@ -36,7 +37,7 @@ const ErrorModal = () => {
3637
const handleModalClose = useCallback(() => {
3738
if (lastConfig.instance instanceof SpotifyAuthError) {
3839
/** ここに処理が来る段階で既にSpotifyの認証情報は削除済み */
39-
router.replace("/connect?provider=spotify")
40+
router.replace(`${PAGE_PATH.CONNECT_PAGE}?provider=spotify`)
4041
setErrorModalConfigs([])
4142
return
4243
}

components/layout/LayoutNavbar.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
ScrollArea
1010
} from "@mantine/core"
1111
import { useHotkeys } from "@mantine/hooks"
12-
import { useRouter } from "next/navigation"
1312
import {
1413
Dispatch,
1514
SetStateAction,
@@ -28,13 +27,14 @@ import GradientButton from "../parts/GradientButton"
2827
import ProviderHeading from "../parts/ProviderHeading"
2928
import NavbarCheckbox from "../parts/navbar/NavbarCheckbox"
3029
import NavbarItemButton from "../parts/navbar/NavbarItemButton"
31-
import { loadingAtom } from "@/atoms/loadingAtom"
3230
import { navbarAtom, navbarClassNameAtom } from "@/atoms/navbarAtom"
3331
import { searchModalAtom } from "@/atoms/searchModalAtom"
3432
import { LOCAL_STORAGE_KEYS } from "@/constants/LocalStorageKeys"
33+
import { PAGE_PATH } from "@/constants/PagePath"
3534
import { STYLING_VALUES } from "@/constants/StylingValues"
3635
import useErrorModal from "@/hooks/useErrorModal"
3736
import useMIX from "@/hooks/useMIX"
37+
import useTransit from "@/hooks/useTransit"
3838
import { NavbarItem } from "@/types/NavbarItem"
3939
import { Provider } from "@/types/Provider"
4040
import { Queue } from "@/types/Queue"
@@ -68,14 +68,13 @@ const LayoutNavbar = ({
6868
onSettingModalOpen,
6969
setIsPreparingPlayback
7070
}: Props) => {
71-
const router = useRouter()
72-
const setIsLoading = useSetRecoilState(loadingAtom)
7371
const [isMixing, setIsMixing] = useState(false)
7472
const [navbarClassName, setNavbarClassName] =
7573
useRecoilState(navbarClassNameAtom)
7674
const [isOpened, setIsOpened] = useRecoilState(navbarAtom)
7775
const { showError } = useErrorModal()
7876
const { mixAllTracks } = useMIX()
77+
const { onTransit } = useTransit()
7978
const setIsSearchModalOpen = useSetRecoilState(searchModalAtom)
8079
useHotkeys([
8180
["Slash", () => setIsSearchModalOpen(true)],
@@ -361,14 +360,9 @@ const LayoutNavbar = ({
361360

362361
<NavbarItemButton
363362
icon={<GrConnect />}
364-
onClick={async () => {
365-
setIsLoading({
366-
stateChangedOn: "MainPage",
367-
state: true
368-
})
369-
await new Promise(resolve => setTimeout(resolve, 300))
370-
router.push("/connect")
371-
}}
363+
onClick={() =>
364+
onTransit(PAGE_PATH.MAIN_PAGE, PAGE_PATH.CONNECT_PAGE)
365+
}
372366
>
373367
サービス接続設定
374368
</NavbarItemButton>

components/templates/ConnectPage/ProviderSelector.tsx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { Flex, Box, Title, Text, Center } from "@mantine/core"
2-
import { useRouter } from "next/navigation"
32
import { memo } from "react"
4-
import { useRecoilValue, useSetRecoilState } from "recoil"
5-
import { loadingAtom } from "@/atoms/loadingAtom"
3+
import { useRecoilValue } from "recoil"
64
import { spotifySettingStateAtom } from "@/atoms/spotifySettingStateAtom"
75
import { webDAVSettingStateAtom } from "@/atoms/webDAVSettingStateAtom"
86
import ArrowTextButton from "@/components/parts/ArrowTextButton"
97
import ProviderSelectorItem from "@/components/parts/ProviderSelectorItem"
8+
import { PAGE_PATH } from "@/constants/PagePath"
109
import useBreakPoints from "@/hooks/useBreakPoints"
10+
import useTransit from "@/hooks/useTransit"
1111

1212
type Props = {
1313
className?: string
@@ -22,8 +22,7 @@ const ProviderSelector = ({
2222
onShowSpotifyConnector,
2323
onShowWebDAVConnector
2424
}: Props) => {
25-
const router = useRouter()
26-
const setIsLoading = useSetRecoilState(loadingAtom)
25+
const { onTransit } = useTransit()
2726
const { setRespVal } = useBreakPoints()
2827
const spotifySettingState = useRecoilValue(spotifySettingStateAtom)
2928
const webDAVSettingState = useRecoilValue(webDAVSettingStateAtom)
@@ -62,14 +61,9 @@ const ProviderSelector = ({
6261
<Center mt="3rem">
6362
<ArrowTextButton
6463
direction="right"
65-
onClick={async () => {
66-
setIsLoading({
67-
stateChangedOn: "ConnectPage",
68-
state: true
69-
})
70-
await new Promise(resolve => setTimeout(resolve, 300))
71-
router.push("/")
72-
}}
64+
onClick={() =>
65+
onTransit(PAGE_PATH.CONNECT_PAGE, PAGE_PATH.MAIN_PAGE)
66+
}
7367
>
7468
メインページに移動する
7569
</ArrowTextButton>

components/templates/SigninPage/Signin.tsx

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { Stack, Group, Button } from "@mantine/core"
2-
import { useRouter } from "next/navigation"
32
import { memo, useCallback, useMemo, useState } from "react"
43
import { HiOutlineMail } from "react-icons/hi"
54
import { PiPasswordBold } from "react-icons/pi"
6-
import { useRecoilValue, useSetRecoilState } from "recoil"
7-
import { loadingAtom } from "@/atoms/loadingAtom"
5+
import { useRecoilValue } from "recoil"
86
import { spotifySettingStateAtom } from "@/atoms/spotifySettingStateAtom"
97
import { webDAVSettingStateAtom } from "@/atoms/webDAVSettingStateAtom"
108
import GradientButton from "@/components/parts/GradientButton"
119
import LabeledInput from "@/components/parts/LabeledInput"
10+
import { PAGE_PATH } from "@/constants/PagePath"
1211
import useAuth from "@/hooks/useAuth"
1312
import useBreakPoints from "@/hooks/useBreakPoints"
1413
import useErrorModal from "@/hooks/useErrorModal"
14+
import useTransit from "@/hooks/useTransit"
1515
import { SigninPageType } from "@/types/SigninPageType"
1616
import { isDefined } from "@/utils/isDefined"
1717

@@ -22,14 +22,13 @@ type Props = {
2222
}
2323

2424
const Signin = ({ className, isDisplay, slideTo }: Props) => {
25+
const { onTransit } = useTransit()
2526
const { setRespVal } = useBreakPoints()
2627
const { showError } = useErrorModal()
27-
const router = useRouter()
2828

2929
const { checkUserExists, signIn } = useAuth()
3030
const spotifySettingState = useRecoilValue(spotifySettingStateAtom)
3131
const webDAVSettingState = useRecoilValue(webDAVSettingStateAtom)
32-
const setIsLoading = useSetRecoilState(loadingAtom)
3332

3433
const [emailInput, setEmailInput] = useState("")
3534
const [passwordInput, setPasswordInput] = useState("")
@@ -51,21 +50,11 @@ const Signin = ({ className, isDisplay, slideTo }: Props) => {
5150
}
5251

5352
if (spotifySettingState !== "done" || webDAVSettingState !== "done") {
54-
setIsLoading({
55-
stateChangedOn: "SigninPage",
56-
state: true
57-
})
58-
await new Promise(resolve => setTimeout(resolve, 300))
59-
router.push("/connect")
53+
await onTransit(PAGE_PATH.SIGNIN_PAGE, PAGE_PATH.CONNECT_PAGE)
6054
return
6155
}
6256

63-
setIsLoading({
64-
stateChangedOn: "SigninPage",
65-
state: true
66-
})
67-
await new Promise(resolve => setTimeout(resolve, 300))
68-
router.push("/")
57+
await onTransit(PAGE_PATH.SIGNIN_PAGE, PAGE_PATH.MAIN_PAGE)
6958
} catch (e) {
7059
showError(e)
7160
} finally {
@@ -77,11 +66,10 @@ const Signin = ({ className, isDisplay, slideTo }: Props) => {
7766
passwordInput,
7867
signIn,
7968
showError,
80-
router,
81-
setIsLoading,
8269
spotifySettingState,
8370
webDAVSettingState,
84-
slideTo
71+
slideTo,
72+
onTransit
8573
])
8674

8775
const isValidEmail = useMemo(

constants/PagePath.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const PAGE_PATH = {
2+
MAIN_PAGE: "/",
3+
CONNECT_PAGE: "/connect",
4+
SIGNIN_PAGE: "/signin",
5+
SPOTIFY_CALLBACK_PAGE: "/callback/spotify"
6+
} as const

hooks/useInitializer.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import useSetWebDAVSettingState from "./useWebDAVSettingState"
1010
import { faviconIndexAtom } from "@/atoms/faviconIndexAtom"
1111
import { loadingAtom } from "@/atoms/loadingAtom"
1212
import { LOCAL_STORAGE_KEYS } from "@/constants/LocalStorageKeys"
13+
import { PAGE_PATH } from "@/constants/PagePath"
1314
import { auth } from "@/utils/firebase"
1415
import { isDefined } from "@/utils/isDefined"
1516
import { generateRandomNumber } from "@/utils/randomNumberGenerator"
@@ -51,12 +52,12 @@ const useInitializer = () => {
5152
!userInfo.emailVerified ||
5253
!isDefined(decryptionKey))
5354
) {
54-
router.push("/signin")
55+
router.push(PAGE_PATH.SIGNIN_PAGE)
5556
return
5657
}
5758

5859
setIsLoading({
59-
stateChangedOn: "SigninPage",
60+
stateChangedOn: PAGE_PATH.MAIN_PAGE,
6061
state: false
6162
})
6263
}, [setIsLoading, isLoadingUserInfo, userInfo, router])

hooks/useTransit.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { useRouter } from "next/navigation"
2+
import { useCallback } from "react"
3+
import { useSetRecoilState } from "recoil"
4+
import { loadingAtom } from "@/atoms/loadingAtom"
5+
import { PagePath } from "@/types/PagePath"
6+
7+
const useTransit = () => {
8+
const router = useRouter()
9+
const setIsLoading = useSetRecoilState(loadingAtom)
10+
11+
const onTransit = useCallback(
12+
async (from: PagePath, to: PagePath) => {
13+
setIsLoading({
14+
stateChangedOn: from,
15+
state: true
16+
})
17+
await new Promise(resolve => setTimeout(resolve, 1000))
18+
router.push(to)
19+
},
20+
[router, setIsLoading]
21+
)
22+
23+
return { onTransit } as const
24+
}
25+
26+
export default useTransit

types/PagePath.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { PAGE_PATH } from "../constants/PagePath"
2+
3+
export type PagePath = (typeof PAGE_PATH)[keyof typeof PAGE_PATH]

0 commit comments

Comments
 (0)