]> git.openstreetmap.org Git - rails.git/blob - app/helpers/social_share_button_helper.rb
Replace .ssb-icon with [data-share-type]
[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-buttons 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 => "rounded-circle focus-ring",
23           :title => I18n.t("application.share.#{site}.title"),
24           :target => "_blank",
25           :data => { :share_type => site == :email ? "email" : "site" }
26         }
27
28         link_to generate_share_url(site, title, url), link_options do
29           image_tag(icon, :alt => I18n.t("application.share.#{site}.alt"), :size => 28)
30         end
31       end, "\n")
32     end
33   end
34
35   private
36
37   def generate_share_url(site, title, url)
38     site = site.to_sym
39     title = URI.encode_uri_component(title)
40     url = URI.encode_uri_component(url)
41
42     case site
43     when :email
44       "mailto:?subject=#{title}&body=#{url}"
45     when :x
46       "https://x.com/intent/tweet?url=#{url}&text=#{title}"
47     when :linkedin
48       "https://www.linkedin.com/sharing/share-offsite/?url=#{url}"
49     when :facebook
50       "https://www.facebook.com/sharer/sharer.php?u=#{url}&t=#{title}"
51     when :mastodon
52       "https://mastodonshare.com/?text=#{title}&url=#{url}"
53     when :telegram
54       "https://t.me/share/url?url=#{url}&text=#{title}"
55     when :bluesky
56       "https://bsky.app/intent/compose?text=#{title}+#{url}"
57     else
58       raise ArgumentError, "Unsupported platform: #{platform}"
59     end
60   end
61 end