forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
151 lines (136 loc) · 3.6 KB
/
index.tsx
File metadata and controls
151 lines (136 loc) · 3.6 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
147
148
149
150
151
import React from 'react';
import getIcon from '../../templates/icons';
import getTemplate, { TemplateType } from '../../templates';
import { profileUrl } from '../../utils/url-generator';
import { ENTER } from '../../utils/keycodes';
import {
Container,
SandboxTitle,
SandboxDescription,
SandboxImage,
SandboxInfo,
TemplateIcon,
Author,
} from './elements';
const getScreenshot = id =>
`https://codesandbox.io/api/v1/sandboxes/${id}/screenshot.png`;
/* eslint-disable camelcase */
export type Props = {
sandbox: {
picks?: Array<{ title: string; description: string }>;
title: string;
description: string;
id: string;
screenshot_url: string;
template: TemplateType;
author: {
username: string;
avatar_url: string;
};
};
small?: boolean;
noHeight?: boolean;
defaultHeight?: number;
noMargin?: boolean;
selectSandbox: (params: {
id: string;
title: string;
description: string;
screenshotUrl: string;
}) => void;
};
/* eslint-enable camelcase */
export default class WideSandbox extends React.PureComponent<Props> {
state = {
imageLoaded: false,
};
getTitle = () => {
if ((this.props.sandbox.picks || []).length !== 0) {
return this.props.sandbox.picks[0].title;
}
return this.props.sandbox.title || this.props.sandbox.id;
};
getDescription = () => {
if ((this.props.sandbox.picks || []).length !== 0) {
return this.props.sandbox.picks[0].description;
}
return this.props.sandbox.description;
};
toggleOpen = () => {
this.props.selectSandbox({
id: this.props.sandbox.id,
title: this.getTitle(),
description: this.getDescription(),
screenshotUrl: this.props.sandbox.screenshot_url,
});
};
handleKeyUp = e => {
if (e.keyCode === ENTER) {
this.toggleOpen();
}
};
render() {
const {
sandbox,
small,
noMargin,
noHeight,
defaultHeight = 245,
} = this.props;
if (!sandbox) {
return (
<Container style={{}}>
<SandboxImage as="div" style={{ border: 0, height: 150 }} />
<SandboxInfo />
</Container>
);
}
const template = getTemplate(sandbox.template);
const Icon = getIcon(sandbox.template);
return (
<Container
noMargin={noMargin}
small={small}
style={{}}
onClick={this.toggleOpen}
role="button"
tabIndex={0}
onKeyUp={this.handleKeyUp}
>
<SandboxImage
alt={this.getTitle()}
src={sandbox.screenshot_url || getScreenshot(sandbox.id)}
color={template.color()}
style={{ height: this.state.imageLoaded ? 'auto' : defaultHeight }}
ref={img => {
if (img && img.complete) {
this.setState({ imageLoaded: true });
}
}}
onLoad={() => {
this.setState({ imageLoaded: true });
}}
/>
<SandboxInfo noHeight={noHeight}>
<SandboxTitle color={template.color()}>
{this.getTitle()}
</SandboxTitle>
{this.getDescription() ? (
<SandboxDescription>{this.getDescription()}</SandboxDescription>
) : null}
{sandbox.author && (
<a href={profileUrl(sandbox.author.username)}>
<Author
username={sandbox.author.username}
avatarUrl={sandbox.author.avatar_url}
/>
</a>
)}
<TemplateIcon>
<Icon width={24} height={24} />
</TemplateIcon>
</SandboxInfo>
</Container>
);
}
}