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
150 lines (138 loc) · 3.53 KB
/
index.tsx
File metadata and controls
150 lines (138 loc) · 3.53 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
import React from 'react';
import ForkIcon from 'react-icons/lib/go/repo-forked';
import EyeIcon from 'react-icons/lib/go/eye';
import LikeIcon from 'react-icons/lib/go/heart';
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,
Image,
Overlay,
Avatar,
Stats,
SandboxStats,
SandboxImage,
SandboxInfo,
TemplateIcon,
} from './elements';
import Tags from '../Tags';
const getScreenshot = (id: string) =>
`https://codesandbox.io/api/v1/sandboxes/${id}/screenshot.png`;
/* eslint-disable camelcase */
export interface Sandbox {
title: string;
description: string;
tags: string[];
id: string;
screenshot_url: string;
template: TemplateType;
view_count: number;
fork_count: number;
like_count: number;
author: {
username: string;
avatar_url: string;
};
}
/* eslint-enable */
export interface Props {
sandbox: Sandbox;
small?: boolean;
noHeight?: boolean;
defaultHeight?: number;
noMargin?: boolean;
selectSandbox: (params: Sandbox) => void;
}
const kFormatter = (num: number): number | string => {
if (num > 999999) {
return (num / 1000000).toFixed(1) + 'M';
}
if (num > 999) {
return (num / 1000).toFixed(1) + 'K';
}
return num;
};
export default class SandboxCard extends React.PureComponent<Props> {
toggleOpen = () => {
this.props.selectSandbox({ ...this.props.sandbox });
};
handleKeyUp = e => {
if (e.keyCode === ENTER) {
this.toggleOpen();
}
};
render() {
const {
sandbox,
small,
noMargin,
noHeight,
defaultHeight = 152,
} = 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}
>
<Image>
<SandboxImage
alt={sandbox.title}
src={sandbox.screenshot_url || getScreenshot(sandbox.id)}
color={template.color()}
style={{ height: defaultHeight }}
/>
<Overlay>
<SandboxDescription>{sandbox.description}</SandboxDescription>
{sandbox.tags && <Tags tags={sandbox.tags} />}
</Overlay>
</Image>
<SandboxInfo noHeight={noHeight}>
<SandboxTitle color={template.color()}>{sandbox.title}</SandboxTitle>
<TemplateIcon>
<Icon width={16} height={16} />
</TemplateIcon>
</SandboxInfo>
<SandboxStats>
<Stats>
<li>
<EyeIcon />
{kFormatter(sandbox.view_count)}
</li>
<li>
<ForkIcon />
{kFormatter(sandbox.fork_count)}
</li>
<li>
<LikeIcon />
{kFormatter(sandbox.like_count)}
</li>
</Stats>
{sandbox.author && (
<a href={profileUrl(sandbox.author.username)}>
<Avatar src={sandbox.author.avatar_url} />
</a>
)}
</SandboxStats>
</Container>
);
}
}