|
1 | | -import React, { FunctionComponent, useState, useEffect } from 'react'; |
| 1 | +import React, { FunctionComponent, useEffect, useState } from 'react'; |
| 2 | +import { Button } from '@codesandbox/common/lib/components/Button'; |
| 3 | + |
2 | 4 | import { useOvermind } from 'app/overmind'; |
3 | 5 |
|
4 | | -import { Button } from '@codesandbox/common/lib/components/Button'; |
| 6 | +import { Explanation, Heading } from '../elements'; |
5 | 7 | import { Container } from '../LiveSessionEnded/elements'; |
6 | | -import { Heading, Explanation } from '../elements'; |
7 | 8 |
|
8 | | -import { List, Item } from './elements'; |
| 9 | +import { Item, List } from './elements'; |
9 | 10 |
|
10 | | -const NetlifyLogs: FunctionComponent = () => { |
11 | | - const [logs, setLogs] = useState(['Contacting Netlify']); |
| 11 | +export const NetlifyLogs: FunctionComponent = () => { |
12 | 12 | const { |
| 13 | + actions: { modalClosed }, |
13 | 14 | state: { |
14 | | - deployment: { netlifyLogs: url }, |
| 15 | + deployment: { netlifyLogs: netlifyLogsUrl }, |
15 | 16 | }, |
16 | | - actions: { modalClosed }, |
17 | 17 | } = useOvermind(); |
| 18 | + const [logs, setLogs] = useState(['Waiting for build to start...']); |
18 | 19 |
|
19 | 20 | useEffect(() => { |
20 | | - const getLogs = async apiUrl => { |
21 | | - const data = await fetch(apiUrl); |
22 | | - const { logs: newLogs } = await data.json(); |
23 | | - setLogs(newLogs); |
24 | | - }; |
25 | | - const interval = setInterval(() => getLogs(url), 2000); |
26 | | - return () => { |
27 | | - clearInterval(interval); |
28 | | - }; |
29 | | - }, [url]); |
| 21 | + const interval = setInterval(async () => { |
| 22 | + const { logs: fetchedLogs } = await fetch(netlifyLogsUrl).then(data => |
| 23 | + data.json() |
| 24 | + ); |
| 25 | + |
| 26 | + if (fetchedLogs.length > 0) { |
| 27 | + setLogs(fetchedLogs); |
| 28 | + } |
| 29 | + }, 2000); |
| 30 | + |
| 31 | + return () => clearInterval(interval); |
| 32 | + }, [netlifyLogsUrl]); |
30 | 33 |
|
31 | 34 | return ( |
32 | 35 | <Container> |
33 | 36 | <Heading>Sandbox Site Logs</Heading> |
| 37 | + |
34 | 38 | <Explanation> |
35 | 39 | Builds typically take a minute or two to complete |
36 | 40 | </Explanation> |
| 41 | + |
37 | 42 | <List> |
38 | | - {logs.map((log, i) => ( |
39 | | - // eslint-disable-next-line react/no-array-index-key |
40 | | - <Item key={i}>{log}</Item> |
| 43 | + {logs.map(log => ( |
| 44 | + <Item key={log}>{log}</Item> |
41 | 45 | ))} |
42 | 46 | </List> |
43 | | - <Button small onClick={() => modalClosed()}> |
| 47 | + |
| 48 | + <Button onClick={() => modalClosed()} small> |
44 | 49 | Close |
45 | 50 | </Button> |
46 | 51 | </Container> |
47 | 52 | ); |
48 | 53 | }; |
49 | | - |
50 | | -export default NetlifyLogs; |
|
0 commit comments