|
1 | | -import * as React from 'react'; |
2 | | - |
3 | | -import { inject } from 'mobx-react'; |
4 | | - |
5 | | -import Navigation from 'app/pages/common/Navigation'; |
6 | | -import Title from 'app/components/Title'; |
7 | | -import SubTitle from 'app/components/SubTitle'; |
| 1 | +import { Button } from '@codesandbox/common/lib/components/Button'; |
8 | 2 | import MaxWidth from '@codesandbox/common/lib/components/flex/MaxWidth'; |
9 | 3 | import Margin from '@codesandbox/common/lib/components/spacing/Margin'; |
10 | | -import { Button } from '@codesandbox/common/lib/components/Button'; |
11 | 4 | import { |
12 | 5 | gitHubToSandboxUrl, |
13 | 6 | protocolAndHost, |
14 | 7 | gitHubRepoPattern, |
15 | 8 | } from '@codesandbox/common/lib/utils/url-generator'; |
| 9 | +import { inject } from 'mobx-react'; |
| 10 | +import React, { useCallback, useEffect, useState } from 'react'; |
| 11 | + |
| 12 | +import Navigation from 'app/pages/common/Navigation'; |
| 13 | +import Title from 'app/components/Title'; |
| 14 | +import SubTitle from 'app/components/SubTitle'; |
16 | 15 |
|
17 | 16 | import { |
18 | 17 | Container, |
19 | 18 | Content, |
20 | | - Label, |
21 | 19 | Description, |
22 | | - StyledInput, |
23 | 20 | ErrorMessage, |
| 21 | + Label, |
| 22 | + StyledInput, |
24 | 23 | } from './elements'; |
25 | 24 |
|
26 | 25 | const getFullGitHubUrl = url => |
27 | 26 | `${protocolAndHost()}${gitHubToSandboxUrl(url)}`; |
28 | 27 |
|
29 | | -class GitHub extends React.PureComponent { |
30 | | - state = { |
31 | | - url: '', |
32 | | - transformedUrl: '', |
33 | | - error: null, |
34 | | - }; |
35 | | - |
36 | | - componentDidMount() { |
37 | | - this.props.signals.githubPageMounted(); |
38 | | - } |
39 | | - |
40 | | - updateUrl = e => { |
41 | | - const url = e.target.value; |
42 | | - |
43 | | - if (!url) { |
44 | | - this.setState({ |
45 | | - url, |
46 | | - error: null, |
47 | | - transformedUrl: '', |
48 | | - }); |
49 | | - } else if (!gitHubRepoPattern.test(url)) { |
50 | | - this.setState({ |
51 | | - url, |
52 | | - error: 'The URL provided is not valid.', |
53 | | - transformedUrl: '', |
54 | | - }); |
| 28 | +const GitHub = ({ signals: { githubPageMounted } }) => { |
| 29 | + const [error, setError] = useState(null); |
| 30 | + const [transformedUrl, setTransformedUrl] = useState(''); |
| 31 | + const [url, setUrl] = useState(''); |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + githubPageMounted(); |
| 35 | + }, [githubPageMounted]); |
| 36 | + |
| 37 | + const updateUrl = useCallback(({ target: { value: newUrl } }) => { |
| 38 | + if (!newUrl) { |
| 39 | + setError(null); |
| 40 | + setTransformedUrl(''); |
| 41 | + setUrl(newUrl); |
| 42 | + } else if (!gitHubRepoPattern.test(newUrl)) { |
| 43 | + setError('The URL provided is not valid.'); |
| 44 | + setTransformedUrl(''); |
| 45 | + setUrl(newUrl); |
55 | 46 | } else { |
56 | | - this.setState({ |
57 | | - url: e.target.value, |
58 | | - transformedUrl: getFullGitHubUrl(url.trim()), |
59 | | - error: null, |
60 | | - }); |
| 47 | + setError(null); |
| 48 | + setTransformedUrl(getFullGitHubUrl(newUrl.trim())); |
| 49 | + setUrl(newUrl); |
61 | 50 | } |
62 | | - }; |
63 | | - |
64 | | - render() { |
65 | | - const { url, transformedUrl, error } = this.state; |
66 | | - return ( |
67 | | - <MaxWidth> |
68 | | - <Margin vertical={1.5} horizontal={1.5}> |
69 | | - <Container> |
70 | | - <Navigation title="GitHub Import" /> |
71 | | - |
72 | | - <Content vertical horizontal> |
73 | | - <Description> |
74 | | - <Title>Import from GitHub</Title> |
75 | | - <SubTitle> |
76 | | - Enter the URL to your GitHub repository to generate a URL to |
77 | | - your sandbox. The sandbox will stay in sync with your |
78 | | - repository. |
79 | | - <br /> |
80 | | - <a |
81 | | - href="/docs/importing#import-from-github" |
82 | | - rel="noopener norefereer" |
83 | | - target="_blank" |
84 | | - > |
85 | | - See the docs |
86 | | - </a> |
87 | | - </SubTitle> |
88 | | - </Description> |
89 | | - |
90 | | - <Label htmlFor="githuburl"> |
91 | | - URL to GitHub Repository (supports branches and paths too) |
92 | | - </Label> |
93 | | - <StyledInput |
94 | | - name="githuburl" |
95 | | - value={url} |
96 | | - onChange={this.updateUrl} |
97 | | - placeholder="Insert GitHub URL..." |
98 | | - /> |
99 | | - |
100 | | - {error !== null && <ErrorMessage>{error}</ErrorMessage>} |
101 | | - |
102 | | - <Label htmlFor="sandboxurl">Converted Sandbox URL</Label> |
103 | | - <StyledInput |
104 | | - name="sandboxurl" |
105 | | - value={transformedUrl} |
106 | | - placeholder="The Sandbox URL" |
107 | | - /> |
108 | | - |
109 | | - <Button disabled={!transformedUrl} to={gitHubToSandboxUrl(url)}> |
110 | | - Open Sandbox |
111 | | - </Button> |
112 | | - </Content> |
113 | | - </Container> |
114 | | - </Margin> |
115 | | - </MaxWidth> |
116 | | - ); |
117 | | - } |
118 | | -} |
| 51 | + }, []); |
| 52 | + |
| 53 | + return ( |
| 54 | + <MaxWidth> |
| 55 | + <Margin vertical={1.5} horizontal={1.5}> |
| 56 | + <Container> |
| 57 | + <Navigation title="GitHub Import" /> |
| 58 | + |
| 59 | + <Content vertical horizontal> |
| 60 | + <Description> |
| 61 | + <Title>Import from GitHub</Title> |
| 62 | + |
| 63 | + <SubTitle> |
| 64 | + Enter the URL to your GitHub repository to generate a URL to |
| 65 | + your sandbox. The sandbox will stay in sync with your |
| 66 | + repository. |
| 67 | + <br /> |
| 68 | + <a |
| 69 | + href="/docs/importing#import-from-github" |
| 70 | + rel="noopener norefereer" |
| 71 | + target="_blank" |
| 72 | + > |
| 73 | + See the docs |
| 74 | + </a> |
| 75 | + </SubTitle> |
| 76 | + </Description> |
| 77 | + |
| 78 | + <Label htmlFor="githuburl"> |
| 79 | + URL to GitHub Repository (supports branches and paths too) |
| 80 | + </Label> |
| 81 | + |
| 82 | + <StyledInput |
| 83 | + name="githuburl" |
| 84 | + onChange={updateUrl} |
| 85 | + placeholder="Insert GitHub URL..." |
| 86 | + value={url} |
| 87 | + /> |
| 88 | + |
| 89 | + {error !== null && <ErrorMessage>{error}</ErrorMessage>} |
| 90 | + |
| 91 | + <Label htmlFor="sandboxurl">Converted Sandbox URL</Label> |
| 92 | + |
| 93 | + <StyledInput |
| 94 | + name="sandboxurl" |
| 95 | + placeholder="The Sandbox URL" |
| 96 | + value={transformedUrl} |
| 97 | + /> |
| 98 | + |
| 99 | + <Button disabled={!transformedUrl} to={gitHubToSandboxUrl(url)}> |
| 100 | + Open Sandbox |
| 101 | + </Button> |
| 102 | + </Content> |
| 103 | + </Container> |
| 104 | + </Margin> |
| 105 | + </MaxWidth> |
| 106 | + ); |
| 107 | +}; |
119 | 108 |
|
120 | 109 | export default inject(['signals'])(GitHub); |
0 commit comments