From e9cf5b99dd47cd3eeff37febd5613125ba17de47 Mon Sep 17 00:00:00 2001 From: Cristian Cepeda <43882+pastuxso@users.noreply.github.com> Date: Tue, 19 Nov 2024 13:27:10 -0500 Subject: [PATCH] Add sign-out event handling and panel HTML refresh for CloudPanel (#1789) * Add sign-out event handling and panel HTML refresh for CloudPanel - Implemented emitPanelEvent in Kernel to emit specific events to panels. - Added signOut event handling in CloudPanel, which clears the app token and refreshes the panel HTML. - Updated extension.ts to emit signOut event when appropriate, aligning session management behavior. * Easier done with more reactiveness (#1803) --------- Co-authored-by: Sebastian (Tiedtke) Huckleberry --- src/extension/extension.ts | 4 ++++ src/extension/kernel.ts | 16 ++++++++++++++++ src/extension/panels/cloud.ts | 27 ++++++++++++++++++++++++--- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/extension/extension.ts b/src/extension/extension.ts index fd10c08ee..42889c33a 100644 --- a/src/extension/extension.ts +++ b/src/extension/extension.ts @@ -521,6 +521,10 @@ export class RunmeExtension { } else { const settingsDefault = getServerLifecycleIdentity() await commands.executeCommand('runme.lifecycleIdentitySelection', settingsDefault) + kernel.emitPanelEvent('runme.cloud', 'onCommand', { + name: 'signOut', + panelId: 'runme.cloud', + }) } kernel.updateFeatureContext('statefulAuth', !!session) }) diff --git a/src/extension/kernel.ts b/src/extension/kernel.ts index e163b1121..da1fafd08 100644 --- a/src/extension/kernel.ts +++ b/src/extension/kernel.ts @@ -39,6 +39,7 @@ import { type ExtensionName, type FeatureContext, FeatureName, + SyncSchema, } from '../types' import { ClientMessages, @@ -243,6 +244,21 @@ export class Kernel implements Disposable { return getEnvProps(ext) } + emitPanelEvent( + panelId: string, + eventName: K, + payload: SyncSchema[K], + ) { + const panel = this.panelManager.getPanel(panelId) + + if (!panel) { + log.error(`Panel ${panelId} not found`) + return + } + + panel.getBus()?.emit(eventName, payload) + } + useMonitor() { return this.monitor$.asObservable() } diff --git a/src/extension/panels/cloud.ts b/src/extension/panels/cloud.ts index 0c54b5ab8..46c009edc 100644 --- a/src/extension/panels/cloud.ts +++ b/src/extension/panels/cloud.ts @@ -1,4 +1,6 @@ import { ExtensionContext, WebviewView, window, ColorThemeKind, Uri, env } from 'vscode' +import { from } from 'rxjs' +import { take, withLatestFrom } from 'rxjs/operators' import { fetchStaticHtml, resolveAppToken } from '../utils' import { IAppToken } from '../services/runme' @@ -94,10 +96,11 @@ export default class CloudPanel extends TanglePanel { protected registerSubscribers(bus: SyncSchemaBus) { return [ bus.on('onCommand', (cmdEvent) => { - if (cmdEvent?.name !== 'signIn') { - return + if (cmdEvent?.name === 'signIn') { + this.onSignIn(bus) + } else if (cmdEvent?.name === 'signOut') { + this.onSignOut(bus) } - this.onSignIn(bus) }), bus.on('onArchiveCell', async (cmdEvent) => { const answer = await window.showInformationMessage( @@ -151,6 +154,24 @@ export default class CloudPanel extends TanglePanel { try { const appToken = await this.getAppToken(true) bus.emit('onAppToken', appToken!) + from(this.getHydratedHtml()) + .pipe(withLatestFrom(this.webview), take(1)) + .subscribe(([html, recentWebview]) => { + recentWebview.html = html + }) + } catch (err: any) { + log.error(err?.message || err) + } + } + + private async onSignOut(bus: SyncSchemaBus) { + try { + bus.emit('onAppToken', { token: 'EMPTY' }) + from(this.getHydratedHtml()) + .pipe(withLatestFrom(this.webview), take(1)) + .subscribe(([html, recentWebview]) => { + recentWebview.html = html + }) } catch (err: any) { log.error(err?.message || err) }