@@ -16,8 +16,8 @@ configure app to AMAZONQ if for Amazon Q login
1616</template >
1717<script lang="ts">
1818import { PropType , defineComponent } from ' vue'
19- import Login from ' ./login.vue'
20- import Reauthenticate from ' ./reauthenticate.vue'
19+ import Login , { getReadyElementId as getLoginReadyElementId } from ' ./login.vue'
20+ import Reauthenticate , { getReadyElementId as getReauthReadyElementId } from ' ./reauthenticate.vue'
2121import { AuthFlowState , FeatureId } from ' ./types'
2222import { WebviewClientFactory } from ' ../../../webviews/client'
2323import { CommonAuthWebview } from ' ./backend'
@@ -51,15 +51,84 @@ export default defineComponent({
5151 })
5252
5353 await this .refreshAuthState ()
54+
55+ // We were recieving the 'load' event before refreshAuthState() resolved (I'm assuming behavior w/ Vue + browser loading not blocking),
56+ // so post refreshAuhState() if we detect we already loaded, then execute immediately since the event already happened.
57+ if (didLoad ) {
58+ handleLoaded ()
59+ } else {
60+ window .addEventListener (' load' , () => {
61+ handleLoaded ()
62+ })
63+ }
5464 },
5565 methods: {
5666 async refreshAuthState() {
5767 await client .refreshAuthState ()
5868 this .authFlowState = await client .getAuthState ()
69+
70+ // Used for telemetry purposes
71+ if (this .authFlowState === ' LOGIN' ) {
72+ ;(window as any ).uiState = ' login'
73+ ;(window as any ).uiReadyElementId = getLoginReadyElementId ()
74+ } else if (this .authFlowState && this .authFlowState !== undefined ) {
75+ ;(window as any ).uiState = ' reauth'
76+ ;(window as any ).uiReadyElementId = getReauthReadyElementId ()
77+ }
78+
5979 this .refreshKey += 1
6080 },
6181 },
6282})
83+
84+ // ---- START ---- The following handles the process of indicating the UI has loaded successfully.
85+ // TODO: Move this in to a reusable class for other webviews, it feels a bit messy here
86+ let didSetReady = false
87+
88+ // Setup error handlers to report. This may not actually be able to catch certain errors that we'd expect,
89+ // so this may have to be revisited.
90+ window .onerror = function (message ) {
91+ if (didSetReady ) {
92+ return
93+ }
94+
95+ setUiReady ((window as any ).uiState , message .toString ())
96+ }
97+ document .addEventListener (
98+ ' error' ,
99+ (e ) => {
100+ if (didSetReady ) {
101+ return
102+ }
103+
104+ setUiReady ((window as any ).uiState , e .message )
105+ },
106+ true
107+ )
108+
109+ let didLoad = false
110+ window .addEventListener (' load' , () => {
111+ didLoad = true
112+ })
113+ const handleLoaded = () => {
114+ // in case some unexpected behavior triggers this flow again, skip since we already emitted for this instance
115+ if (didSetReady ) {
116+ return
117+ }
118+
119+ const foundElement = !! document .getElementById ((window as any ).uiReadyElementId )
120+ if (! foundElement ) {
121+ setUiReady ((window as any ).uiState , ` Could not find element: ${(window as any ).uiReadyElementId } ` )
122+ } else {
123+ // Successful load!
124+ setUiReady ((window as any ).uiState )
125+ }
126+ }
127+ const setUiReady = (state : ' login' | ' reauth' , errorMessage ? : string ) => {
128+ client .setUiReady (state , errorMessage )
129+ didSetReady = true
130+ }
131+ // ---- END ----
63132 </script >
64133<style >
65134body {
0 commit comments