Skip to content

Commit

Permalink
Show toasts for notifications and notification banner on critical not…
Browse files Browse the repository at this point in the history
…ifications(#5097)

* Display toast notifications with actions

* Condition matcher for displaying notifications

* Show notification banner

* feedback 1

* Modified deserialization cases and added tests

* not required file change

* feedback 1

* feedback 1

* modified the base class

* merge conflicts resolved

* rearranged call site

* show notifications when panel is opened

* fixed tests

* detekt

* feedback

* convert panels into wrappers

* fixed test
  • Loading branch information
manodnyab authored Nov 22, 2024
1 parent 87bbbce commit dfe18e9
Show file tree
Hide file tree
Showing 17 changed files with 449 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowFactory
import com.intellij.openapi.wm.ex.ToolWindowEx
import com.intellij.ui.content.Content
import com.intellij.ui.content.ContentManager
import com.intellij.ui.components.panels.Wrapper
import com.intellij.util.ui.components.BorderLayoutPanel
import software.aws.toolkits.core.utils.debug
import software.aws.toolkits.core.utils.getLogger
import software.aws.toolkits.jetbrains.core.credentials.AwsBearerTokenConnection
Expand All @@ -22,6 +22,8 @@ import software.aws.toolkits.jetbrains.core.credentials.pinning.QConnection
import software.aws.toolkits.jetbrains.core.credentials.sono.Q_SCOPES
import software.aws.toolkits.jetbrains.core.credentials.sso.bearer.BearerTokenAuthState
import software.aws.toolkits.jetbrains.core.credentials.sso.bearer.BearerTokenProviderListener
import software.aws.toolkits.jetbrains.core.notifications.NotificationPanel
import software.aws.toolkits.jetbrains.core.notifications.ProcessNotificationsBase
import software.aws.toolkits.jetbrains.core.webview.BrowserState
import software.aws.toolkits.jetbrains.services.amazonq.QWebviewPanel
import software.aws.toolkits.jetbrains.services.amazonq.RefreshQChatPanelButtonPressedListener
Expand All @@ -35,7 +37,21 @@ import java.awt.event.ComponentAdapter
import java.awt.event.ComponentEvent

class AmazonQToolWindowFactory : ToolWindowFactory, DumbAware {

Check warning on line 39 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/toolwindow/AmazonQToolWindowFactory.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Extension class should be final and non-public

Extension class should not be public

override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
val mainPanel = BorderLayoutPanel()
val qPanel = Wrapper()
val notificationPanel = NotificationPanel()

mainPanel.addToTop(notificationPanel)
mainPanel.add(qPanel)
val notifListener = ProcessNotificationsBase.getInstance(project)
notifListener.addListenerForNotification { bannerContent ->
runInEdt {
notificationPanel.updateNotificationPanel(bannerContent)
}
}

if (toolWindow is ToolWindowEx) {
val actionManager = ActionManager.getInstance()
toolWindow.setTitleActions(listOf(actionManager.getAction("aws.q.toolwindow.titleBar")))
Expand All @@ -46,7 +62,7 @@ class AmazonQToolWindowFactory : ToolWindowFactory, DumbAware {
ToolkitConnectionManagerListener.TOPIC,
object : ToolkitConnectionManagerListener {
override fun activeConnectionChanged(newConnection: ToolkitConnection?) {
onConnectionChanged(project, newConnection, toolWindow)
onConnectionChanged(project, newConnection, qPanel)
}
}
)
Expand All @@ -56,8 +72,7 @@ class AmazonQToolWindowFactory : ToolWindowFactory, DumbAware {
object : RefreshQChatPanelButtonPressedListener {
override fun onRefresh() {
runInEdt {
contentManager.removeAllContents(true)
prepareChatContent(project, contentManager)
prepareChatContent(project, qPanel)
}
}
}
Expand All @@ -68,43 +83,37 @@ class AmazonQToolWindowFactory : ToolWindowFactory, DumbAware {
object : BearerTokenProviderListener {
override fun onChange(providerId: String, newScopes: List<String>?) {
if (ToolkitConnectionManager.getInstance(project).connectionStateForFeature(QConnection.getInstance()) == BearerTokenAuthState.AUTHORIZED) {
val content = contentManager.factory.createContent(AmazonQToolWindow.getInstance(project).component, null, false).also {
it.isCloseable = true
it.isPinnable = true
}
val qComponent = AmazonQToolWindow.getInstance(project).component

runInEdt {
contentManager.removeAllContents(true)
contentManager.addContent(content)
qPanel.setContent(qComponent)
}
}
}
}
)

val content = prepareChatContent(project, contentManager)
prepareChatContent(project, qPanel)

val content = contentManager.factory.createContent(mainPanel, null, false).also {
it.isCloseable = true
it.isPinnable = true
}
toolWindow.activate(null)
contentManager.setSelectedContent(content)
contentManager.addContent(content)
}

private fun prepareChatContent(
project: Project,
contentManager: ContentManager,
): Content {
qPanel: Wrapper,
) {
val component = if (isQConnected(project) && !isQExpired(project)) {
AmazonQToolWindow.getInstance(project).component
} else {
QWebviewPanel.getInstance(project).browser?.prepareBrowser(BrowserState(FeatureId.AmazonQ))
QWebviewPanel.getInstance(project).component
}

val content = contentManager.factory.createContent(component, null, false).also {
it.isCloseable = true
it.isPinnable = true
}
contentManager.addContent(content)
return content
qPanel.setContent(component)
}

override fun init(toolWindow: ToolWindow) {
Expand All @@ -125,8 +134,7 @@ class AmazonQToolWindowFactory : ToolWindowFactory, DumbAware {

override fun shouldBeAvailable(project: Project): Boolean = isQWebviewsAvailable()

private fun onConnectionChanged(project: Project, newConnection: ToolkitConnection?, toolWindow: ToolWindow) {
val contentManager = toolWindow.contentManager
private fun onConnectionChanged(project: Project, newConnection: ToolkitConnection?, qPanel: Wrapper) {
val isNewConnectionForQ = newConnection?.let {
(it as? AwsBearerTokenConnection)?.let { conn ->
val scopeShouldHave = Q_SCOPES
Expand All @@ -151,15 +159,8 @@ class AmazonQToolWindowFactory : ToolWindowFactory, DumbAware {
LOG.debug { "returning login window; no Q connection found" }
QWebviewPanel.getInstance(project).component
}

val content = contentManager.factory.createContent(component, null, false).also {
it.isCloseable = true
it.isPinnable = true
}

runInEdt {
contentManager.removeAllContents(true)
contentManager.addContent(content)
qPanel.setContent(component)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package software.aws.toolkits.jetbrains.services.amazonq.toolwindow

import com.intellij.openapi.project.Project
import com.intellij.ui.components.panels.Wrapper
import com.intellij.util.ui.components.BorderLayoutPanel
import software.aws.toolkits.core.utils.error
import software.aws.toolkits.core.utils.getLogger
import software.aws.toolkits.jetbrains.core.webview.BrowserState
import software.aws.toolkits.jetbrains.services.amazonq.QWebviewPanel
import software.aws.toolkits.jetbrains.utils.isQConnected
import software.aws.toolkits.jetbrains.utils.isQExpired
import software.aws.toolkits.telemetry.FeatureId
import javax.swing.JComponent

class OuterAmazonQPanel(val project: Project) : BorderLayoutPanel() {
private val wrapper = Wrapper()
init {
isOpaque = false
addToCenter(wrapper)
val component = if (isQConnected(project) && !isQExpired(project)) {
AmazonQToolWindow.getInstance(project).component
} else {
QWebviewPanel.getInstance(project).browser?.prepareBrowser(BrowserState(FeatureId.AmazonQ))
QWebviewPanel.getInstance(project).component
}
updateQPanel(component)
}

fun updateQPanel(content: JComponent) {
try {
wrapper.setContent(content)
} catch (e: Exception) {
getLogger<OuterAmazonQPanel>().error { "Error while creating window" }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ object AwsToolkit {

const val GITHUB_URL = "https://github.com/aws/aws-toolkit-jetbrains"
const val AWS_DOCS_URL = "https://docs.aws.amazon.com/console/toolkit-for-jetbrains"
const val GITHUB_CHANGELOG = "https://github.com/aws/aws-toolkit-jetbrains/blob/main/CHANGELOG.md"
}

data class PluginInfo(val id: String, val name: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,109 @@

package software.aws.toolkits.jetbrains.core.notifications

import com.intellij.icons.AllIcons
import com.intellij.ide.BrowserUtil
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages
import com.intellij.ui.EditorNotificationPanel
import software.aws.toolkits.jetbrains.AwsToolkit
import software.aws.toolkits.resources.AwsCoreBundle

fun checkSeverity(notificationSeverity: String): NotificationSeverity = when (notificationSeverity) {
"Critical" -> NotificationSeverity.CRITICAL
"Warning" -> NotificationSeverity.WARNING
"Info" -> NotificationSeverity.INFO
else -> NotificationSeverity.INFO
}

// TODO: Add actions that can be performed from the notifications here
object NotificationManager {
fun createActions(
project: Project,
followupActions: List<NotificationFollowupActions>?,
message: String,
title: String,

): List<NotificationActionList> = buildList {
var url: String? = null
followupActions?.forEach { action ->
if (action.type == "ShowUrl") {
url = action.content.locale.url
}

if (action.type == "UpdateExtension") {
add(
NotificationActionList(AwsCoreBundle.message("notification.update")) {
// TODO: Add update logic
}
)
}

if (action.type == "OpenChangelog") {
add(
NotificationActionList(AwsCoreBundle.message("notification.changelog")) {
BrowserUtil.browse(AwsToolkit.GITHUB_CHANGELOG)
}
)
}
}
add(
NotificationActionList(AwsCoreBundle.message("general.more_dialog")) {
if (url == null) {
Messages.showYesNoDialog(
project,
message,
title,
AwsCoreBundle.message("general.acknowledge"),
AwsCoreBundle.message("general.cancel"),
AllIcons.General.Error
)
} else {
val link = url ?: AwsToolkit.GITHUB_URL
val openLink = Messages.showYesNoDialog(
project,
message,
title,
AwsCoreBundle.message(AwsCoreBundle.message("notification.learn_more")),
AwsCoreBundle.message("general.cancel"),
AllIcons.General.Error
)
if (openLink == 0) {
BrowserUtil.browse(link)
}
}
}
)
}

fun buildNotificationActions(actions: List<NotificationActionList>): List<AnAction> = actions.map { (title, block) ->
object : AnAction(title) {
override fun actionPerformed(e: AnActionEvent) {
block()
}
}
}

fun buildBannerPanel(panel: EditorNotificationPanel, actions: List<NotificationActionList>): EditorNotificationPanel {
actions.forEach { (actionTitle, block) ->
panel.createActionLabel(actionTitle) {
block()
}
}

return panel
}
}

data class NotificationActionList(
val title: String,
val blockToExecute: () -> Unit,
)

data class BannerContent(
val title: String,
val message: String,
val actions: List<NotificationActionList>,
val id: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package software.aws.toolkits.jetbrains.core.notifications

import com.intellij.icons.AllIcons
import com.intellij.openapi.application.runInEdt
import com.intellij.ui.EditorNotificationPanel
import com.intellij.ui.components.panels.Wrapper
import com.intellij.util.ui.components.BorderLayoutPanel
import software.aws.toolkits.resources.AwsCoreBundle

class NotificationPanel : BorderLayoutPanel() {
private val wrapper = Wrapper()
init {
isOpaque = false
addToCenter(wrapper)
ProcessNotificationsBase.showBannerNotification.forEach {
updateNotificationPanel(it.value)
}
}

private fun removeNotificationPanel(notificationId: String) = runInEdt {
ProcessNotificationsBase.showBannerNotification.remove(notificationId) // TODO: add id to dismissed notification list
wrapper.removeAll()
}

fun updateNotificationPanel(bannerContent: BannerContent) {
val panel = EditorNotificationPanel()
panel.text = bannerContent.title
panel.icon(AllIcons.General.Error)
val panelWithActions = NotificationManager.buildBannerPanel(panel, bannerContent.actions)
panelWithActions.createActionLabel(AwsCoreBundle.message("general.dismiss")) {
removeNotificationPanel(bannerContent.id)
}

wrapper.setContent(panelWithActions)
}
}
Loading

0 comments on commit dfe18e9

Please sign in to comment.