1 # frozen_string_literal: true
 
   3 module ShareButtonsHelper
 
   4   SHARE_BUTTONS_CONFIG = {
 
   5     :email => "share_button_icons/email.svg",
 
   6     :bluesky => "share_button_icons/bluesky.svg",
 
   7     :facebook => "share_button_icons/facebook.svg",
 
   8     :linkedin => "share_button_icons/linkedin.svg",
 
   9     :mastodon => "share_button_icons/mastodon.svg",
 
  10     :telegram => "share_button_icons/telegram.svg",
 
  11     :x => "share_button_icons/x.svg"
 
  14   # Generates a set of share buttons based on the specified options.
 
  15   def share_buttons(title:, url:)
 
  17       :class => "d-flex gap-1 align-items-end flex-wrap mb-3"
 
  20         tag.button(:type => "button",
 
  21                    :class => "btn btn-secondary p-1 border-1 rounded-circle",
 
  22                    :title => I18n.t("application.share.share.title"),
 
  24                    :data => { :share_type => "native",
 
  26                               :share_url => url }) do
 
  27           image_tag("share_button_icons/share.svg", :alt => I18n.t("application.share.share.alt"), :size => 18, :class => "d-block")
 
  31       buttons << SHARE_BUTTONS_CONFIG.map do |site, icon|
 
  34           :class => "rounded-circle focus-ring",
 
  35           :title => I18n.t("application.share.#{site}.title"),
 
  37           :data => { :share_type => site == :email ? "email" : "site" }
 
  40         link_to generate_share_url(site, title, url), link_options do
 
  41           image_tag(icon, :alt => I18n.t("application.share.#{site}.alt"), :size => 28)
 
  45       safe_join(buttons, "\n")
 
  51   def generate_share_url(site, title, url)
 
  53     title = URI.encode_uri_component(title)
 
  54     url = URI.encode_uri_component(url)
 
  58       "mailto:?subject=#{title}&body=#{url}"
 
  60       "https://x.com/intent/tweet?url=#{url}&text=#{title}"
 
  62       "https://www.linkedin.com/sharing/share-offsite/?url=#{url}"
 
  64       "https://www.facebook.com/sharer/sharer.php?u=#{url}&t=#{title}"
 
  66       "https://mastodonshare.com/?text=#{title}&url=#{url}"
 
  68       "https://t.me/share/url?url=#{url}&text=#{title}"
 
  70       "https://bsky.app/intent/compose?text=#{title}+#{url}"
 
  72       raise ArgumentError, "Unsupported platform: #{platform}"