mirror of https://github.com/sgoudham/carbon.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.9 KiB
JavaScript
76 lines
1.9 KiB
JavaScript
import axios from 'axios'
|
|
import debounce from 'lodash.debounce'
|
|
import ms from 'ms'
|
|
|
|
import getConfig from 'next/config'
|
|
const { publicRuntimeConfig } = getConfig()
|
|
|
|
const client = axios.create({ baseURL: publicRuntimeConfig.API_URL })
|
|
|
|
const RATE_LIMIT_CODE = 420
|
|
|
|
const gistClient = axios.create({
|
|
baseURL: 'https://api.github.com',
|
|
timeout: 5000,
|
|
headers: {
|
|
Accept: 'application/vnd.github.v3+json',
|
|
'Content-Type': 'application/json'
|
|
}
|
|
})
|
|
|
|
function tweet(content, encodedImage) {
|
|
const processedData = encodedImage.split(',')[1]
|
|
|
|
return client
|
|
.post('/twitter', {
|
|
imageData: processedData,
|
|
altText: content
|
|
})
|
|
.then(res => res.data.url)
|
|
.then(url => encodeURIComponent(`Built with #Carbon, by @dawn_labs ${url}`))
|
|
.then(uri => `https://twitter.com/intent/tweet?text=${uri}`)
|
|
.then(openTwitterUrl)
|
|
.catch(checkIfRateLimited)
|
|
}
|
|
|
|
function image(state) {
|
|
return client.post('/image', { state }).then(res => res.data)
|
|
}
|
|
|
|
function getGist(uid) {
|
|
return gistClient
|
|
.get(`/gists/${uid}`)
|
|
.then(res => res.data)
|
|
.then(gist => gist.files)
|
|
.then(files => files[Object.keys(files)[0]])
|
|
}
|
|
|
|
// private
|
|
function openTwitterUrl(twitterUrl) {
|
|
const width = 575
|
|
const height = 400
|
|
const left = (window.outerWidth - width) / 2
|
|
const top = (window.outerHeight - height) / 2
|
|
const opts = `status=1,width=${width},height=${height},top=${top},left=${left}`
|
|
|
|
window.open(twitterUrl, 'twitter', opts)
|
|
}
|
|
|
|
function checkIfRateLimited(err) {
|
|
if (err.response.status === RATE_LIMIT_CODE) {
|
|
alert(
|
|
"Oh no! Looks like to many people are trying to tweet right now and we've been rate limited. Try again soon or save and upload manually!"
|
|
)
|
|
return
|
|
}
|
|
|
|
throw err
|
|
}
|
|
|
|
export default {
|
|
client,
|
|
getGist,
|
|
tweet: debounce(tweet, ms('5s'), { leading: true, trailing: false }),
|
|
image: debounce(image, ms('5s'), { leading: true, trailing: false })
|
|
}
|