Skip to content

Commit

Permalink
feat[Op#50985] Add Primer Flash
Browse files Browse the repository at this point in the history
  • Loading branch information
akabiru committed Nov 22, 2023
1 parent dee79b6 commit b60360a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
22 changes: 19 additions & 3 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,23 @@ def due_date_distance_in_words(date)
end
end

def render_primer_flash_message?
flash[:primer_flash].present?
end

def render_primer_flash_message
return unless render_primer_flash_message?

render(FlashMessageComponent.new(**flash[:primer_flash].to_hash))
end

# Renders flash messages
def render_flash_messages
def render_legacy_flash_messages
return if render_primer_flash_message?

messages = flash
.reject { |k, _| k.start_with? '_' }
.map { |k, v| render_flash_message(k, v) }
.map { |k, v| render_legacy_flash_message(k, v) }

safe_join messages, "\n"
end
Expand All @@ -132,20 +144,24 @@ def join_flash_messages(messages)
end
end

def render_flash_message(type, message, html_options = {})
def render_legacy_flash_message(type, message, html_options = {}) # rubocop:disable Metrics/AbcSize
if type.to_s == 'notice'
type = 'success'
end

toast_css_classes = ["op-toast -#{type}", html_options.delete(:class)]

# Add autohide class to notice flashes if configured
if type.to_s == 'success' && User.current.pref.auto_hide_popups?
toast_css_classes << 'autohide-toaster'
end

html_options = { class: toast_css_classes.join(' '), role: 'alert' }.merge(html_options)
close_button = content_tag :a, '', class: 'op-toast--close icon-context icon-close',
title: I18n.t('js.close_popup_title'),
tabindex: '0'
toast = content_tag(:div, join_flash_messages(message), class: 'op-toast--content')

content_tag :div, '', class: 'op-toast--wrapper' do
content_tag :div, '', class: 'op-toast--casing' do
content_tag :div, html_options do
Expand Down
3 changes: 2 additions & 1 deletion app/views/layouts/base.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@ See COPYRIGHT and LICENSE files for more details.
<% end %>
<div class="content-overlay"></div>
<main id="content-wrapper" class="<%= initial_classes %>">
<%= render_primer_flash_message %>
<% if show_decoration %>
<div id="breadcrumb" class="<%= initial_classes %><%= show_breadcrumb ? ' -show' : '' %>">
<%= you_are_here_info %>
<%= full_breadcrumb %>
<%= call_hook :view_layouts_base_breadcrumb %>
</div>
<% end %>
<%= render_flash_messages %>
<%= render_legacy_flash_messages %>
<% if show_onboarding_modal? %>
<section data-augmented-model-wrapper
data-modal-initialize-now="true"
Expand Down

0 comments on commit b60360a

Please sign in to comment.