forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact-hooks.js
More file actions
146 lines (130 loc) · 4.02 KB
/
react-hooks.js
File metadata and controls
146 lines (130 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import React from 'react';
import { graphql } from 'gatsby';
import WideSandbox from '@codesandbox/common/lib/components/WideSandbox';
import Margin from '@codesandbox/common/lib/components/spacing/Margin';
import TitleAndMetaTags from '../components/TitleAndMetaTags';
import PageContainer from '../components/PageContainer';
import { Heading2 } from '../components/headings';
import Layout from '../components/layout';
import SandboxModal from '../screens/explore/SandboxModal';
import ShuffleWords from './_shuffleWords';
import { Container, Sandboxes } from './explore/_elements';
export default class extends React.PureComponent {
state = {
sandboxes: this.props.data.allAirtable.edges.map(s => ({
...s.node.data,
title: s.node.data.title,
})),
renderModal: false,
};
componentDidMount() {
// We need to do this for SSR, the modal can't be rendered when using SSR,
// we cannot just put a check in `render` because that would mean that client
// render and server render are not the same. So we force a rerender.
// eslint-disable-next-line
this.setState({ renderModal: true });
}
openSandbox = index => {
this.setState(state => {
const sandbox = state.sandboxes[index];
const { id, title, description } = sandbox;
return {
selectedSandbox: {
id,
title,
description,
screenshotUrl: sandbox.screenshot_url,
},
};
});
};
openPreviousSandbox = currentIndex => () => {
this.openSandbox(currentIndex - 1);
};
openNextSandbox = currentIndex => () => {
this.openSandbox(currentIndex + 1);
};
selectSandbox = ({ id, title, description, screenshotUrl }) => {
this.setState({
selectedSandbox: { id, title, description, screenshotUrl },
});
};
getCurrentIndex = () =>
this.state.selectedSandbox
? this.state.sandboxes.findIndex(
s => this.state.selectedSandbox.id === s.id
)
: -1;
closeModal = () => {
this.setState({ selectedSandbox: undefined });
};
render() {
const { sandboxes, selectedSandbox } = this.state;
const currentIndex = this.getCurrentIndex();
return (
<Layout>
<Container>
<TitleAndMetaTags
description="A showcase of the amazing uses for React Hooks!"
title="React Hooks Community Examples - CodeSandbox"
/>
{this.state.renderModal && (
<SandboxModal
onClose={this.closeModal}
sandboxId={selectedSandbox && selectedSandbox.id}
screenshotUrl={selectedSandbox && selectedSandbox.screenshotUrl}
title={selectedSandbox && selectedSandbox.title}
description={selectedSandbox && selectedSandbox.description}
openPreviousSandbox={
currentIndex > 0 &&
currentIndex !== -1 &&
this.openPreviousSandbox(currentIndex)
}
openNextSandbox={
currentIndex < this.state.sandboxes.length - 1 &&
currentIndex !== -1 &&
this.openNextSandbox(currentIndex)
}
/>
)}
<PageContainer as="main" width={1440}>
<Heading2
css={`
text-align: center;
`}
>
React Hooks Community Examples
</Heading2>
<ShuffleWords />
<Sandboxes>
{sandboxes.map(sandbox => (
<Margin key={sandbox.id} bottom={2}>
<WideSandbox
selectSandbox={this.selectSandbox}
sandbox={sandbox}
/>
</Margin>
))}
</Sandboxes>
</PageContainer>
</Container>
</Layout>
);
}
}
export const query = graphql`
{
allAirtable {
edges {
node {
data {
id
title
description
template
}
}
}
}
}
`;