]> git.openstreetmap.org Git - rails.git/blob - app/helpers/social_share_button_helper.rb
Drop unused ability to filter social sharing sites
[rails.git] / app / helpers / social_share_button_helper.rb
1 module SocialShareButtonHelper
2   require "uri"
3
4   SOCIAL_SHARE_CONFIG = {
5     :email => "social_icons/email.svg",
6     :bluesky => "social_icons/bluesky.svg",
7     :facebook => "social_icons/facebook.svg",
8     :linkedin => "social_icons/linkedin.svg",
9     :mastodon => "social_icons/mastodon.svg",
10     :telegram => "social_icons/telegram.svg",
11     :x => "social_icons/x.svg"
12   }.freeze
13
14   # Generates a set of social share buttons based on the specified options.
15   def social_share_buttons(opts = {})
16     tag.div(
17       :class => "social-share-button d-flex gap-1 align-items-end flex-wrap mb-3"
18     ) do
19       SOCIAL_SHARE_CONFIG.map do |site, icon|
20         link_options = {
21           :rel => ["nofollow", opts[:rel]].compact,
22           :class => "ssb-icon rounded-circle",
23           :title => I18n.t("application.share.#{site}.title"),
24           :target => "_blank"
25         }
26
27         link_to generate_share_url(site, opts), link_options do
28           image_tag(icon, :alt => I18n.t("application.share.#{site}.alt"), :size => 28)
29         end
30       end.join.html_safe
31     end
32   end
33
34   private
35
36   def generate_share_url(site, params)
37     site = site.to_sym
38     case site
39     when :email
40       to = params[:to] || ""
41       subject = CGI.escape(params[:title])
42       body = CGI.escape(params[:url])
43       "mailto:#{to}?subject=#{subject}&body=#{body}"
44     when :x
45       via_str = params[:via] ? "&via=#{URI.encode_www_form_component(params[:via])}" : ""
46       hashtags_str = params[:hashtags] ? "&hashtags=#{URI.encode_www_form_component(params[:hashtags].join(','))}" : ""
47       "https://x.com/intent/tweet?url=#{URI.encode_www_form_component(params[:url])}&text=#{URI.encode_www_form_component(params[:title])}#{hashtags_str}#{via_str}"
48     when :linkedin
49       "https://www.linkedin.com/sharing/share-offsite/?url=#{URI.encode_www_form_component(params[:url])}"
50     when :facebook
51       "https://www.facebook.com/sharer/sharer.php?u=#{URI.encode_www_form_component(params[:url])}&t=#{URI.encode_www_form_component(params[:title])}"
52     when :mastodon
53       "https://mastodonshare.com/?text=#{URI.encode_www_form_component(params[:title])}&url=#{URI.encode_www_form_component(params[:url])}"
54     when :telegram
55       "https://t.me/share/url?url=#{URI.encode_www_form_component(params[:url])}&text=#{URI.encode_www_form_component(params[:title])}"
56     when :bluesky
57       "https://bsky.app/intent/compose?text=#{URI.encode_www_form_component(params[:title])}+#{URI.encode_www_form_component(params[:url])}"
58     else
59       raise ArgumentError, "Unsupported platform: #{platform}"
60     end
61   end
62 end