Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Allow defining unique flashes to be removed when duplicate

Oliver Güntherf34074af24b1
diff --git a/app/components/op_primer/flash_component.erb b/app/components/op_primer/flash_component.erb
index 63c0bcfec36..4682dfebf99 100644
--- a/app/components/op_primer/flash_component.erb
+++ b/app/components/op_primer/flash_component.erb
@@ -27,6 +27,12 @@ See COPYRIGHT and LICENSE files for more details.
++#%>
-<div class="op-primer-flash--item">
- <%= render_parent %>
-</div>
+<%=
+ render(Primer::BaseComponent.new(
+ tag: :div,
+ classes: "op-primer-flash--item",
+ data: { unique_key: @unique_key }.compact
+ )) do
+ render_parent
+ end
+%>
diff --git a/app/components/op_primer/flash_component.rb b/app/components/op_primer/flash_component.rb
index 24fa22fcbb2..2c50edc45ac 100644
--- a/app/components/op_primer/flash_component.rb
+++ b/app/components/op_primer/flash_component.rb
@@ -33,6 +33,8 @@ module OpPrimer
include OpPrimer::ComponentHelpers
def initialize(**system_arguments)
+ @unique_key = system_arguments.delete(:unique_key)
+
system_arguments[:test_selector] ||= "primer-banner-message-component"
system_arguments[:dismiss_scheme] ||= :remove
system_arguments[:dismiss_label] ||= I18n.t(:button_close)
diff --git a/frontend/src/turbo/flash-stream-action.ts b/frontend/src/turbo/flash-stream-action.ts
index 4a0ebe430b3..cd2a2dcbda7 100644
--- a/frontend/src/turbo/flash-stream-action.ts
+++ b/frontend/src/turbo/flash-stream-action.ts
@@ -3,8 +3,17 @@ import { StreamActions, StreamElement } from '@hotwired/turbo';
export function registerFlashStreamAction() {
StreamActions.flash = function dialogStreamAction(this:StreamElement) {
const content = this.templateElement.content;
+ const flash = content.firstElementChild as HTMLElement;
const target = document.getElementById('primerized-flash-messages') as HTMLElement;
- target.innerHTML = '';
- target.append(content);
+
+ if (flash.dataset.uniqueKey) {
+ const existingFlash = target.querySelector(`[data-unique-key="${flash.dataset.uniqueKey}"]`);
+ if (existingFlash) {
+ existingFlash.replaceWith(flash);
+ return;
+ }
+ }
+
+ target.append(flash);
};
}
diff --git a/lookbook/docs/patterns/08-flash-banner.md.erb b/lookbook/docs/patterns/08-flash-banner.md.erb
index a79cdd58fcf..29cd2c23249 100644
--- a/lookbook/docs/patterns/08-flash-banner.md.erb
+++ b/lookbook/docs/patterns/08-flash-banner.md.erb
@@ -67,8 +67,12 @@ As this is a common use case, the method `render_error_flash_message_via_turbo_s
<%= embed OpPrimer::FlashComponentPreview, :danger, panels: %i[source] %>
-<% if false %>
### With optional button:
<%= embed OpPrimer::FlashComponentPreview, :button, panels: %i[source] %>
-<% end %>
+
+### Replacing existing flashes
+
+By passing `data-unique-key`, you can delete previous flashes using the same key
+
+<%= embed OpPrimer::FlashComponentPreview, :unique_key, panels: %i[source] %>
diff --git a/lookbook/previews/op_primer/flash_component_preview.rb b/lookbook/previews/op_primer/flash_component_preview.rb
index 33be6e1143e..9456c8435a1 100644
--- a/lookbook/previews/op_primer/flash_component_preview.rb
+++ b/lookbook/previews/op_primer/flash_component_preview.rb
@@ -13,6 +13,12 @@ module OpPrimer
end
end
+ def unique_key
+ render(OpPrimer::FlashComponent.new(unique_key: "bla", scheme: :danger, icon: :stop)) do |_component|
+ "Stop right there."
+ end
+ end
+
def button
render(OpPrimer::FlashComponent.new) do |component|
component.with_action_button(
diff --git a/modules/meeting/app/components/meetings/update_flash_component.rb b/modules/meeting/app/components/meetings/update_flash_component.rb
index 3d5391352d4..de6cfc685d9 100644
--- a/modules/meeting/app/components/meetings/update_flash_component.rb
+++ b/modules/meeting/app/components/meetings/update_flash_component.rb
@@ -33,7 +33,12 @@ module Meetings
alias_method :meeting, :model
def call
- render(::OpPrimer::FlashComponent.new(icon: :info)) do |banner|
+ render(
+ ::OpPrimer::FlashComponent.new(
+ icon: :info,
+ unique_key: "meetings-update-flash"
+ )
+ ) do |banner|
banner.with_action_button(
tag: :a,
href: helpers.meeting_path(meeting),