1
+ 'use client' ;
2
+
1
3
import type { NextPage } from 'next' ;
2
4
import { useApi , useToaster } from '@/hooks' ;
3
5
import { Container } from '@/components/Container' ;
4
6
import { useRouter } from 'next/router' ;
5
7
import type { SSRProps } from '@/utils/ssrUtils' ;
6
8
import Script from 'next/script' ;
9
+ import { useEffect , useState } from 'react' ;
10
+ import { AppleMusicIcon } from '@/components/Icons' ;
11
+ import { Button } from '@/components/Button' ;
7
12
8
13
const Login : NextPage < SSRProps > = ( ) => {
9
14
const router = useRouter ( ) ;
10
15
const toaster = useToaster ( ) ;
11
16
const api = useApi ( ) ;
12
17
// const [musicKitLoaded, setMusicKitLoaded] = useState(false);
13
- let inProgress = false ;
18
+ const [ inProgress , setInProgress ] = useState ( false ) ;
19
+ const [ showButton , setShowButton ] = useState ( false ) ;
14
20
15
21
const redirectToLogin = ( ) => {
16
22
toaster . error ( 'Sign in with Apple first' ) ;
@@ -30,15 +36,12 @@ const Login: NextPage<SSRProps> = () => {
30
36
}
31
37
32
38
if ( inProgress ) return ;
33
- inProgress = true ;
39
+ setInProgress ( true ) ;
34
40
35
41
try {
36
42
const music = MusicKit . getInstance ( ) ;
37
43
const MUT = await music . authorize ( ) ;
38
44
39
- // // eslint-disable-next-line no-promise-executor-return
40
- // await new Promise((resolve) => setTimeout(resolve, 500));
41
-
42
45
window . location . replace (
43
46
api . http . resolveUrl (
44
47
'/auth/APPLEMUSIC/callback/music' ,
@@ -51,7 +54,7 @@ const Login: NextPage<SSRProps> = () => {
51
54
console . error ( e ) ;
52
55
toaster . error ( 'Something went wrong trying to login. Please try again.' ) ;
53
56
}
54
- inProgress = false ;
57
+ setInProgress ( false ) ;
55
58
} ;
56
59
57
60
const initializeMusicKit = ( ) => {
@@ -71,9 +74,13 @@ const Login: NextPage<SSRProps> = () => {
71
74
} ) ( ) ;
72
75
} ;
73
76
74
- // useEffect(() => {
75
- // initializeMusicKit();
76
- // }, [router.isReady, musicKitLoaded, router.query]);
77
+ useEffect ( ( ) => {
78
+ // 3 second timeout to show the button
79
+ const timeout = setTimeout ( ( ) => {
80
+ setShowButton ( true ) ;
81
+ } , 3000 ) ;
82
+ return ( ) => clearTimeout ( timeout ) ;
83
+ } , [ ] ) ;
77
84
78
85
return (
79
86
< >
@@ -89,16 +96,18 @@ const Login: NextPage<SSRProps> = () => {
89
96
</ h1 >
90
97
< div className = "mt-8 flex flex-col gap-4" >
91
98
< p className = "text-center" >
92
- An Apple Music popup should appear soon...
99
+ An Apple Music popup should appear soon... If it doesn't,
100
+ please check if you have browser popups blocked.
93
101
</ p >
94
- { /* TODO: show the button below after 3 seconds or so in case the automatic popup didnt open @stijnvdkolk */ }
95
- { /* <Button
96
- onClick={appleMusicKitHandle}
97
- className="w-full bg-applemusic/80 text-white hover:bg-applemusic/60 active:bg-applemusic/50"
98
- >
99
- <AppleMusicIcon className="mr-2 !fill-white" hover={false} />
100
- Authorize
101
- </Button> */ }
102
+ { showButton && (
103
+ < Button
104
+ onClick = { appleMusicKitHandle }
105
+ className = "w-full bg-applemusic/80 text-white hover:bg-applemusic/60 active:bg-applemusic/50"
106
+ >
107
+ < AppleMusicIcon className = "mr-2 !fill-white" hover = { false } />
108
+ Authorize
109
+ </ Button >
110
+ ) } { ' ' }
102
111
</ div >
103
112
</ div >
104
113
</ Container >
0 commit comments