-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgif.coffee
44 lines (35 loc) · 1.12 KB
/
gif.coffee
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
import React from 'react'
import fixWeirdGusses from '/fix-weird-guesses'
url = 'https://api.giphy.com/v1/gifs/search'
api_key = '02c86584244447a3884c4a867d36932b'
limit = 20
sample = (arr=[])->
arr[Math.floor Math.random() * arr.length]
export default \
class Gif extends React.Component
constructor: ->
super arguments...
@state = {}
componentDidMount: =>
# giphy setup
correctedName = fixWeirdGusses[@props.name] or @props.name
q = (correctedName or 'work sad').replace(/\s+/g, '+')
# get gifs
res = await fetch "#{url}?#{new URLSearchParams {api_key, limit, q}}"
{ data: gifs } = await res.json()
# fetch a random result
gif = sample gifs
{
original: { mp4, webp }
original_still: { url: poster }
} = gif.images
@setState { mp4, webp, poster }
render: ->
{ mp4, webp, poster } = @state
return null unless poster
<div className="Gif">
<video loop={true} muted={true} playsInline={true} autoPlay={true} poster={poster}>
<source src={mp4} type="video/mp4"/>
<source src={webp} type="video/webp"/>
</video>
</div>