1
+ import { useEffect , useState } from 'react' ;
1
2
import { withTheme } from 'styled-components' ;
2
3
import { ArrowRight } from 'react-iconly' ;
3
4
import PropTypes from 'prop-types' ;
4
- import { Corner } from '@components' ;
5
+ import { Tooltip , Corner } from '@components' ;
5
6
import { useForm } from 'react-hook-form' ;
6
7
import rules from '@common/rules' ;
7
8
import { isEmpty } from 'lodash' ;
8
9
import { toLowerCase } from '@utils' ;
10
+ import { useToasts } from '@contexts/toasts' ;
11
+ import { getIsGithubRateLimited } from '@lib/user-builder' ;
9
12
import {
10
13
StyledContainer ,
11
14
StyledForm ,
@@ -15,17 +18,33 @@ import {
15
18
StyledButton ,
16
19
StyledErrorMessage ,
17
20
ProductHuntContainer ,
21
+ RemainingPortfolios ,
18
22
} from './styles' ;
19
23
20
24
function HomeForm ( { theme } ) {
25
+ const [ remainingPortfolios , setRemainingPortfolios ] = useState ( 0 ) ;
21
26
const { register, handleSubmit, formState, errors } = useForm ( {
22
27
mode : 'onChange' ,
23
28
} ) ;
29
+ const { ToastsType, addToastWithTimeout } = useToasts ( ) ;
30
+
31
+ useEffect ( async ( ) => {
32
+ try {
33
+ const { resources } = await getIsGithubRateLimited ( true ) ;
34
+ const { remaining } = resources . core ;
35
+ setRemainingPortfolios ( remaining ) ;
36
+ if ( remaining === 0 ) {
37
+ addToastWithTimeout ( ToastsType . ERROR , 'Github API rate limit exceeded try again in 1 hour' ) ;
38
+ }
39
+ } catch ( error ) {
40
+ console . log ( error ) ;
41
+ }
42
+ } , [ ] ) ;
24
43
25
44
const { isValid } = formState ;
26
45
27
46
const onSubmit = ( { username } ) => {
28
- if ( ! username ) return ;
47
+ if ( ! username || remainingPortfolios === 0 ) return ;
29
48
const formattedUsername = toLowerCase ( username ) ;
30
49
if ( window !== undefined ) window . location = `/portfolio/${ formattedUsername } ` ;
31
50
} ;
@@ -37,19 +56,47 @@ function HomeForm({ theme }) {
37
56
< HeroTitle > Just type your username and watch the magic</ HeroTitle >
38
57
< StyledForm onSubmit = { handleSubmit ( onSubmit ) } >
39
58
< StyledInput
59
+ disabled = { remainingPortfolios === 0 }
40
60
placeholder = "Github username"
41
61
name = "username"
42
62
type = "text"
43
63
ref = { register ( rules . username ) }
44
64
error = { ! isEmpty ( errors . username ) }
45
65
/>
46
- < StyledButton type = "submit" disabled = { ! isValid } >
66
+ < StyledButton type = "submit" disabled = { ! isValid || remainingPortfolios === 0 } >
47
67
< ArrowRight set = "light" primaryColor = { theme . bg . default } />
48
68
</ StyledButton >
49
69
</ StyledForm >
50
70
< StyledErrorMessage >
51
71
{ ! isEmpty ( errors . username ) && errors . username . message }
52
72
</ StyledErrorMessage >
73
+ < RemainingPortfolios >
74
+ < Tooltip
75
+ size = "big"
76
+ position = "bottom"
77
+ content = "GitHub limits the number of requests per hour to 60 for unauthenticated users"
78
+ >
79
+ < RemainingPortfolios >
80
+ < p > Available requests</ p >
81
+ < p >
82
+ { remainingPortfolios }
83
+ /60
84
+ </ p >
85
+ </ RemainingPortfolios >
86
+ </ Tooltip >
87
+ { remainingPortfolios === 0 && (
88
+ < p className = "github-rate" >
89
+ Github API rate limit exceeded try again in 1 hour
90
+ < a
91
+ target = "_blank"
92
+ rel = "noreferrer"
93
+ href = "https://docs.github.com/es/github-ae@latest/rest/reference/rate-limit"
94
+ >
95
+ Docs
96
+ </ a >
97
+ </ p >
98
+ ) }
99
+ </ RemainingPortfolios >
53
100
< ProductHuntContainer >
54
101
< a
55
102
href = "https://www.producthunt.com/posts/devcover?utm_source=badge-review& utm_medium = badge & utm_souce = badge - devcover #discussion- body "
0 commit comments