]> git.openstreetmap.org Git - rails.git/blob - app/helpers/social_share_button_helper.rb
Merge remote-tracking branch 'upstream/pull/5420'
[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(title:, url:)
16     tag.div(
17       :class => "social-share-button d-flex gap-1 align-items-end flex-wrap mb-3"
18     ) do
19       safe_join(SOCIAL_SHARE_CONFIG.map do |site, icon|
20         link_options = {
21           :rel => "nofollow",
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, title, url), link_options do
28           image_tag(icon, :alt => I18n.t("application.share.#{site}.alt"), :size => 28)
29         end
30       end, "\n")
31     end
32   end
33
34   private
35
36   def generate_share_url(site, title, url)
37     site = site.to_sym
38     title = URI.encode_www_form_component(title)
39     url = URI.encode_www_form_component(url)
40
41     case site
42     when :email
43       "mailto:?subject=#{title}&body=#{url}"
44     when :x
45       "https://x.com/intent/tweet?url=#{url}&text=#{title}"
46     when :linkedin
47       "https://www.linkedin.com/sharing/share-offsite/?url=#{url}"
48     when :facebook
49       "https://www.facebook.com/sharer/sharer.php?u=#{url}&t=#{title}"
50     when :mastodon
51       "https://mastodonshare.com/?text=#{title}&url=#{url}"
52     when :telegram
53       "https://t.me/share/url?url=#{url}&text=#{title}"
54     when :bluesky
55       "https://bsky.app/intent/compose?text=#{title}+#{url}"
56     else
57       raise ArgumentError, "Unsupported platform: #{platform}"
58     end
59   end
60 end