-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
169 lines (161 loc) · 5.46 KB
/
index.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import React, { Component } from 'react'
import { Icons, Icon, Styles, Spinner, ShareLink } from 'thicket-elements'
import { Trigger } from 'thicket-elements/lib/ShareLink'
import './Gif.css'
import Editable from '../Editable'
import streamSaver from 'streamsaver'
import ImageDataConverter from '../../utils/imageDataConverter'
import { getGIFLink } from '../../utils/links'
import { isMobile } from '../../utils/constants'
import TimedGif from '../TimedGif'
const { linearGradient, glow } = Styles
const { downloadSvg, shareSvg, facebookSvg, twitterSvg } = Icons
const isSafari = navigator.userAgent.toLowerCase().indexOf('safari/') > -1
const getFilename = ({ nickname, caption }) => `thicket${nickname ? `-${nickname}` : ''}${caption ? `-${caption}` : ''}.gif`
const downloadFile = gif => {
const filename = getFilename(gif)
const fileStream = streamSaver.createWriteStream(filename, gif.src.length)
const writer = fileStream.getWriter()
const data = new ImageDataConverter(gif.src).convertToTypedArray()
writer.write(data)
writer.close()
}
const Download = ({ gif, onShareHook }) => {
if (document.documentElement.clientWidth < 600) {
return null
}
// chrome
if (/Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor)) {
return <button
className="gif__button"
onClick={() => onShareHook(() => downloadFile(gif))}
>
<Icon src={downloadSvg} />
</button>
}
// safari & firefox
return <a
className="gif__button"
download={getFilename(gif)}
href={gif.src}
>
<Icon src={downloadSvg} />
</a>
}
export default class Gif extends Component {
state = { sharing: false }
render() {
const {
onShareHook=cb=>cb(),
gif,
header,
editable,
onChange,
communityId,
children,
className,
} = this.props
const { sharing } = this.state
if (!gif) {
return <div className="gif__loading"><Spinner /></div>
}
const { src, nickname, caption, id } = gif
const publicURL = getGIFLink(communityId, id)
return <div className={`gif__wrap${className ? ` ${className}` : ''}`} style={{ background: linearGradient, boxShadow: glow }}>
<div className="gif__img-wrap">
<TimedGif
className={`gif__img${isSafari ? ' gif__img--safari' : ''}`}
src={src}
alt={caption}
/>
</div>
<div className="gif__inner">
{header}
<div key="body" className="gif__body">
{(nickname || editable) &&
<div
key="nickname"
data-test="gif-created-by"
>
<h4 className="gif__subtitle">GIF created by:</h4>
{editable
? <Editable
value={nickname}
onChange={e => onChange({ ...gif, nickname: e.currentTarget.value })}
/>
: <div>{nickname}</div>}
</div>
}
{(caption || editable) &&
<div
key="caption"
data-test="gif-caption"
>
<h4 className="gif__subtitle">GIF caption:</h4>
{editable
? <Editable
value={caption}
onChange={e => onChange({ ...gif, caption: e.currentTarget.value })}
/>
: <div>{caption}</div>}
</div>
}
{src && <div>
<h4>Save & Share GIF:</h4>
{isMobile
? <div
className="gif__invite-wrap"
style={{ background: linearGradient }}
>
<input
className="gif__invite"
ref={i => this.input = i}
type="text"
readOnly
value={publicURL}
onClick={e => onShareHook(() => this.input.setSelectionRange(0, 9999))}
/>
<img
src={shareSvg}
alt="Share this GIF"
className="gif__invite-img"
/>
</div>
: <div className="gif__buttons">
<Download gif={gif} onShareHook={onShareHook} />
<Trigger active={sharing}>
<button
className="gif__button"
onClick={() => onShareHook(() => this.setState({ sharing: true }))}
>
<Icon src={shareSvg} />
</button>
</Trigger>
{sharing && <ShareLink
className="gif__share"
title={"GIF Share Link"}
body={"Share the link with friends so they view this GIF."}
toCopy={publicURL}
onBlur={() => this.setState({ sharing: false })}
/>}
<button
className="gif__button"
onClick={() => onShareHook(() => window.open(`https://www.facebook.com/sharer.php?u=${publicURL}`, '_blank'))}
>
<Icon src={facebookSvg} />
</button>
<button
className="gif__button"
onClick={() => onShareHook(() => window.open(`https://www.twitter.com/intent/tweet?url=${publicURL}`, '_blank'))}
>
<Icon src={twitterSvg} />
</button>
</div>
}
</div>}
{children}
</div>
</div>
</div>
}
}