Skip to content

Commit 9d2c032

Browse files
committed
v2
1 parent 9652b25 commit 9d2c032

File tree

4 files changed

+69
-55
lines changed

4 files changed

+69
-55
lines changed

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,16 @@ class QWebviewBrowser(val project: Project, private val parentDisposable: Dispos
183183
ToolkitConnectionManager.getInstance(project)
184184
.activeConnectionForFeature(QConnection.getInstance()) as? AwsBearerTokenConnection
185185
)?.let { connection ->
186-
runInEdt {
187-
SsoLogoutAction(connection).actionPerformed(
188-
AnActionEvent.createFromDataContext(
189-
"qBrowser",
190-
null,
191-
DataContext.EMPTY_CONTEXT
192-
)
186+
runInEdt {
187+
SsoLogoutAction(connection).actionPerformed(
188+
AnActionEvent.createFromDataContext(
189+
"qBrowser",
190+
null,
191+
DataContext.EMPTY_CONTEXT
193192
)
194-
}
193+
)
195194
}
195+
}
196196
}
197197

198198
is BrowserMessage.Reauth -> {
@@ -220,6 +220,10 @@ class QWebviewBrowser(val project: Project, private val parentDisposable: Dispos
220220
)
221221
}
222222

223+
is BrowserMessage.ListProfiles -> {
224+
handleListProfilesMessage()
225+
}
226+
223227
is BrowserMessage.PublishWebviewTelemetry -> {
224228
publishTelemetry(message)
225229
}
@@ -272,48 +276,6 @@ class QWebviewBrowser(val project: Project, private val parentDisposable: Dispos
272276

273277
when (stage) {
274278
"PROFILE_SELECT" -> {
275-
ApplicationManager.getApplication().executeOnPooledThread {
276-
var errorMessage: String = ""
277-
val profiles = try {
278-
QRegionProfileManager.getInstance().listRegionProfiles(project)
279-
} catch (e: Exception) {
280-
e.message?.let {
281-
errorMessage = it
282-
}
283-
LOG.warn { "Failed to call listRegionProfiles API: $errorMessage" }
284-
val qConn = ToolkitConnectionManager.getInstance(project).activeConnectionForFeature(QConnection.getInstance())
285-
Telemetry.amazonq.didSelectProfile.use { span ->
286-
span.source(QProfileSwitchIntent.Auth.value)
287-
.amazonQProfileRegion(QRegionProfileManager.getInstance().activeProfile(project)?.region ?: "not-set")
288-
.ssoRegion((qConn as? AwsBearerTokenConnection)?.region)
289-
.credentialStartUrl((qConn as? AwsBearerTokenConnection)?.startUrl)
290-
.result(MetricResult.Failed)
291-
.reason(e.message)
292-
}
293-
294-
null
295-
}
296-
297-
if (profiles?.size == 1) {
298-
LOG.debug { "User only have access to 1 Q profile, auto-selecting profile ${profiles.first().profileName} for ${project.name}" }
299-
QRegionProfileManager.getInstance().switchProfile(project, profiles.first(), QProfileSwitchIntent.Update)
300-
}
301-
302-
// required EDT as this entire block is executed on thread pool
303-
runInEdt {
304-
val jsonData = """
305-
{
306-
stage: '$stage',
307-
status: '${if (profiles != null) "succeeded" else "failed"}',
308-
profiles: ${writeValueAsString(profiles ?: "")},
309-
errorMessage: '$errorMessage'
310-
}
311-
""".trimIndent()
312-
313-
executeJS("window.ideClient.prepareUi($jsonData)")
314-
}
315-
}
316-
317279
val jsonData = """
318280
{
319281
stage: '$stage',
@@ -353,6 +315,51 @@ class QWebviewBrowser(val project: Project, private val parentDisposable: Dispos
353315
jcefBrowser.loadHTML(getWebviewHTML(webScriptUri, query))
354316
}
355317

318+
private fun handleListProfilesMessage() {
319+
ApplicationManager.getApplication().executeOnPooledThread {
320+
var errorMessage: String = ""
321+
val profiles = try {
322+
QRegionProfileManager.getInstance().listRegionProfiles(project)
323+
} catch (e: Exception) {
324+
e.message?.let {
325+
errorMessage = it
326+
}
327+
LOG.warn { "Failed to call listRegionProfiles API: $errorMessage" }
328+
val qConn = ToolkitConnectionManager.getInstance(project).activeConnectionForFeature(QConnection.getInstance())
329+
Telemetry.amazonq.didSelectProfile.use { span ->
330+
span.source(QProfileSwitchIntent.Auth.value)
331+
.amazonQProfileRegion(QRegionProfileManager.getInstance().activeProfile(project)?.region ?: "not-set")
332+
.ssoRegion((qConn as? AwsBearerTokenConnection)?.region)
333+
.credentialStartUrl((qConn as? AwsBearerTokenConnection)?.startUrl)
334+
.result(MetricResult.Failed)
335+
.reason(e.message)
336+
}
337+
338+
null
339+
}
340+
341+
if (profiles?.size == 1) {
342+
LOG.debug { "User only have access to 1 Q profile, auto-selecting profile ${profiles.first().profileName} for ${project.name}" }
343+
QRegionProfileManager.getInstance().switchProfile(project, profiles.first(), QProfileSwitchIntent.Update)
344+
return@executeOnPooledThread
345+
}
346+
347+
// required EDT as this entire block is executed on thread pool
348+
runInEdt {
349+
val jsonData = """
350+
{
351+
stage: 'PROFILE_SELECT',
352+
status: '${if (profiles != null) "succeeded" else "failed"}',
353+
profiles: ${writeValueAsString(profiles ?: "")},
354+
errorMessage: '$errorMessage'
355+
}
356+
""".trimIndent()
357+
358+
executeJS("window.ideClient.prepareUi($jsonData)")
359+
}
360+
}
361+
}
362+
356363
companion object {
357364
private val LOG = getLogger<QWebviewBrowser>()
358365
private const val WEB_SCRIPT_URI = "http://webview/js/getStart.js"

plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/webview/BrowserMessage.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo
2727
JsonSubTypes.Type(value = BrowserMessage.Reauth::class, name = "reauth"),
2828
JsonSubTypes.Type(value = BrowserMessage.SendUiClickTelemetry::class, name = "sendUiClickTelemetry"),
2929
JsonSubTypes.Type(value = BrowserMessage.SwitchProfile::class, name = "switchProfile"),
30-
JsonSubTypes.Type(value = BrowserMessage.PublishWebviewTelemetry::class, name = "webviewTelemetry")
30+
JsonSubTypes.Type(value = BrowserMessage.PublishWebviewTelemetry::class, name = "webviewTelemetry"),
31+
JsonSubTypes.Type(value = BrowserMessage.ListProfiles::class, name = "listProfiles")
3132
)
3233
sealed interface BrowserMessage {
3334

@@ -66,6 +67,8 @@ sealed interface BrowserMessage {
6667
val arn: String,
6768
) : BrowserMessage
6869

70+
object ListProfiles : BrowserMessage
71+
6972
data class SendUiClickTelemetry(val signInOptionClicked: String?) : BrowserMessage
7073

7174
data class PublishWebviewTelemetry(val event: String) : BrowserMessage

plugins/core/webview/src/q-ui/components/profileSelection.vue

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export default defineComponent({
8585
}
8686
},
8787
computed: {
88-
isWaitingResponse(): boolean {
88+
isWaitingResponse() {
8989
const profileResult = this.$store.state.listProfilesResult
9090
if (profileResult instanceof ListProfilePendingResult) {
9191
return true
@@ -97,12 +97,14 @@ export default defineComponent({
9797
this.errorMessage = GENERIC_PROFILE_LOAD_ERROR
9898
} else {
9999
// should not be this path
100-
this.errorMessage = 'Unexpected error happened while loading Q webview page'
100+
this.errorMessage = "Unexpected error happenede while loading Q Webview page"
101101
}
102-
103-
return false
104102
}
105103
},
104+
mounted() {
105+
window.ideApi.postMessage({command: 'listProfiles'})
106+
},
107+
106108
methods: {
107109
toggleItemSelection(profile: Profile) {
108110
this.selectedProfile = profile;

plugins/toolkit/jetbrains-core/src/software/aws/toolkits/jetbrains/core/explorer/webview/ToolkitLoginWebview.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ class ToolkitWebviewBrowser(val project: Project, private val parentDisposable:
246246

247247
is BrowserMessage.SwitchProfile -> {}
248248

249+
is BrowserMessage.ListProfiles -> {}
250+
249251
is BrowserMessage.PublishWebviewTelemetry -> {
250252
publishTelemetry(message)
251253
}

0 commit comments

Comments
 (0)