diff --git a/lib/api.js b/lib/api.js index e185dc0..a7a9068 100644 --- a/lib/api.js +++ b/lib/api.js @@ -1,5 +1,7 @@ import axios from 'axios' +const DOMAIN = process.browser ? document.location.origin : '' + const gistClient = axios.create({ baseURL: 'https://api.github.com', timeout: 5000, @@ -9,6 +11,16 @@ const gistClient = axios.create({ } }); +async function tweet (encodedImage) { + const processedData = encodedImage.split(',')[1] + + return axios.post(`${DOMAIN}/twitter`, { data: processedData }) + .then((res) => res.data.url) + .then((url) => encodeURIComponent(`Built with @carbon ${url}`)) + .then((uri) => `https://twitter.com/intent/tweet?text=${uri}`) + .then(openTwitterUrl) +} + async function uploadImage (encodedImage) { // upload image const url = 'https://api.imgur.com/3/image' @@ -40,5 +52,16 @@ const getGist = (id) => { export default { uploadImage, - getGist + getGist, + tweet +} + +// private +function openTwitterUrl (twitterUrl) { + const width = 575, height = 400 + const left = (window.outerWidth - width) / 2 + const right = (window.outerHeight - height) / 2 + const opts = `status=1,width=${width},height=${height},top=${top},left=${left}` + + window.open(twitterUrl, 'twitter', opts) }