diff --git a/app/lib/sharing.js b/app/lib/sharing.js new file mode 100644 index 0000000..6b67359 --- /dev/null +++ b/app/lib/sharing.js @@ -0,0 +1,58 @@ +import Settings from 'electron-settings' +import { shell, remote } from 'electron' + +/** + * Open hashes in a browser + * @param {string[]} hashes + */ +export function openInBrowser (hashes) { + hashes.forEach(hash => { + shell.openExternal(getURLFromHash(hash)) + }) + return Promise.resolve(hashes) +} + +/** + * Constructs a shareable url with the gateway from settings or the default one + * + * @param {string} hash + * @returns {string} + */ +export function getURLFromHash (hash) { + const gatewayURL = Settings.get('gatewayURL') || 'https://siderus.io' + return `${gatewayURL}/ipfs/${hash}` +} + +export function shareViaFacebook (hash) { + return shell.openExternal(`https://www.facebook.com/sharer.php?u=${getURLFromHash(hash)}`) +} + +export function shareViaTwitter (hash) { + return shell.openExternal(`https://twitter.com/intent/tweet?text=${getURLFromHash(hash)}`) +} + +export function shareViaEmail (hash) { + return shell.openExternal(`mailto:?body=${getURLFromHash(hash)}`) +} + +export const shareMenuTemplate = (hash) => ([{ + label: 'Copy URL', + click: () => { + remote.clipboard.writeText(getURLFromHash(hash)) + } +}, { + label: 'via Email', + click: () => { + shareViaEmail(hash) + } +}, { + label: 'via Facebook', + click: () => { + shareViaFacebook(hash) + } +}, { + label: 'via Twitter', + click: () => { + shareViaTwitter(hash) + } +}]) diff --git a/app/windows/Details/renderer.jsx b/app/windows/Details/renderer.jsx index e80ef02..72402b4 100644 --- a/app/windows/Details/renderer.jsx +++ b/app/windows/Details/renderer.jsx @@ -11,13 +11,13 @@ import { promiseIPFSReady, unpinObject } from '../../api' -import { openInBrowser } from '../Storage/fileIntegration' import { remote } from 'electron' import queryString from 'query-string' import cx from 'classnames' import { Window, Toolbar, Actionbar, ButtonGroup } from 'react-photonkit' import Button from '../../components/Button' import { trackEvent } from '../../stats' +import { shareMenuTemplate, openInBrowser } from '../../lib/sharing' // Load Components import InformationTab from './Components/InformationTab' @@ -37,6 +37,8 @@ class DetailsWindow extends React.Component { componentDidMount () { trackEvent('DetailsWindowOpen', {}) promiseIPFSReady().then(this.fetchData) + + this.shareMenu = remote.Menu.buildFromTemplate(shareMenuTemplate(hash)) } fetchData = () => { @@ -78,6 +80,10 @@ class DetailsWindow extends React.Component { openInBrowser([hash]) } + handleShare = () => { + this.shareMenu.popup({}) + } + handlePin = () => { this.setState({ isUpdatingPin: true }) pinObject(hash) @@ -137,6 +143,12 @@ class DetailsWindow extends React.Component { onClick={this.handleOpenInBrowser} pullRight /> +