import assert from 'assert';

import objectToGetParams from './utils/objectToGetParams';
import createShareButton from './hocs/createShareButton';

function twitterLink(
  url: string,
  { title, via, hashtags = [] }: { title?: string; via?: string; hashtags?: string[] },
) {
  assert(url, 'twitter.url');
  assert(Array.isArray(hashtags), 'twitter.hashtags is not an array');

  return (
    'https://twitter.com/share' +
    objectToGetParams({
      url,
      text: title,
      via,
      hashtags: hashtags.join(','),
    })
  );
}

const TwitterShareButton = createShareButton<{ title?: string; via?: string; hashtags?: string[] }>(
  'twitter',
  twitterLink,
  props => ({
    hashtags: props.hashtags,
    title: props.title,
    via: props.via,
  }),
  {
    windowWidth: 550,
    windowHeight: 400,
  },
);

export default TwitterShareButton;

import assert from 'assert';

import objectToGetParams from './utils/objectToGetParams';
import createShareButton from './hocs/createShareButton';

function twitterLink(
  url: string,
  { title, via, hashtags = [] }: { title?: string; via?: string; hashtags?: string[] },
) {
  assert(url, 'twitter.url');
  assert(Array.isArray(hashtags), 'twitter.hashtags is not an array');

  return (
    'https://twitter.com/share' +
    objectToGetParams({
      url,
      text: title,
      via,
      hashtags: hashtags.length > 0 ? hashtags.join(',') : undefined,
    })
  );
}

const TwitterShareButton = createShareButton<{ title?: string; via?: string; hashtags?: string[] }>(
  'twitter',
  twitterLink,
  props => ({
    hashtags: props.hashtags,
    title: props.title,
    via: props.via,
  }),
  {
    windowWidth: 550,
    windowHeight: 400,
  },
);

export default TwitterShareButton;

Handle no hashtag for Twitter
Handle no hashtag for Twitter

If you pass Twitter an empty string (which is what happens when you call Array.join on an empty array), Twitter adds an empty hashtag to the post when sharing with the native app on iOS. I haven't checked Android. Web seems fine.
