-
Notifications
You must be signed in to change notification settings - Fork 61
/
ShareController.js
109 lines (94 loc) · 3.47 KB
/
ShareController.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
import React, { Component } from 'react'
import { assoc, compose, concat, last, join, map, split } from 'ramda'
import { Future as Task } from 'ramda-fantasy'
import { List } from 'immutable-ext'
import { compress } from 'lzutf8'
import { createPreset } from 'utils/presets'
import { logError } from 'utils/tools'
const domain = `${window.location.protocol}//${window.location.host}`
const shortURLCache = {}
// getGoogleShortURL :: url -> Task Error url
const getGoogleShortURL = url =>
Task((rej, res) => {
if (shortURLCache[url]) return res(shortURLCache[url])
const onError = () => rej(Error(`Problem getting short URL: ${url}`))
window.gapi.client.urlshortener.url
.insert({ longUrl: url })
.then(({ result }) => {
const shortURL = result.id
shortURLCache[url] = shortURL
res(shortURL)
}, onError)
})
const getIDFromGoogleURL = compose(last, split('/'))
// combineShortURLs :: [url] -> url
const combineShortURLs = compose(
concat(`${domain}/share/`),
join('-'),
map(getIDFromGoogleURL),
)
// compressShortMultiURL :: url -> Task Error url
const compressShortMultiURL = (url) => {
if (!url.includes('-')) return Task.of(url)
return getGoogleShortURL(url)
.map(shortURL => combineShortURLs([shortURL]))
}
class ShareController extends Component {
state = {
isEnabled: false,
isLoading: false,
shortURL: ''
}
// getShortURL :: preset -> Task Error url
getShortURL = preset => compose(
getGoogleShortURL,
this.getShareableURL,
createPreset,
assoc('usePredefinedSettings', true),
)(preset)
onClick = () => {
this.setState({ isLoading: true })
List(this.props.audioPlaylist)
.traverse(Task.of, this.getShortURL)
.map(combineShortURLs)
.chain(compressShortMultiURL)
.fork(logError, (url) => {
this.launchModal(url)
this.setState({ isLoading: false })
})
}
getShareableURL = (preset) => {
const compressedPreset = compress(JSON.stringify(preset), { outputEncoding: 'Base64' })
const shareableURL = `djen.co/#share=${compressedPreset}`
if (shareableURL.length > 2048) logError('URL exceeds 2048 chars. May not succeed')
return shareableURL
}
launchModal = (url) => {
const content = (<ShareBox url={url} />)
this.props.actions.enableModal({ content, title: 'Share URL', isCloseable: true, className: 'modal--auto-width' })
}
render = () => {
const isDisabled = !this.props.googleAPIHasLoaded
return (
<div className="">
<button className={`button-primary button-primary--alpha-dark button-primary--tiny u-flex-row ${this.state.isLoading ? '' : 'icon-is-hidden'}`} onClick={this.onClick} disabled={isDisabled}>
<span className="button-primary__inner">Share</span>
<span className="button-primary__icon">
<span className="spinner" />
</span>
</button>
</div>
)
}
}
const ShareBox = props => (
<div>
<input
className="input-base input-base--bare input-base--large input-base--long"
type="text"
value={props.url}
onClick={e => e.target.select()}
/>
</div>
)
export default ShareController