@@ -436,14 +436,14 @@ export class ServerManagerView {
436436 initServerActions ( ) : void {
437437 const $serverImgs : NodeListOf < HTMLImageElement > =
438438 document . querySelectorAll ( ".server-icons" ) ;
439- for ( const [ index , $serverImg ] of $serverImgs . entries ( ) ) {
440- this . addContextMenu ( $serverImg , index ) ;
439+ for ( const $serverImg of $serverImgs ) {
440+ this . addContextMenu ( $serverImg ) ;
441441 if ( $serverImg . src === defaultIcon ) {
442- this . displayInitialCharLogo ( $serverImg , index ) ;
442+ this . displayInitialCharLogo ( $serverImg ) ;
443443 }
444444
445445 $serverImg . addEventListener ( "error" , ( ) => {
446- this . displayInitialCharLogo ( $serverImg , index ) ;
446+ this . displayInitialCharLogo ( $serverImg ) ;
447447 } ) ;
448448 }
449449 }
@@ -495,11 +495,7 @@ export class ServerManagerView {
495495 return tab instanceof ServerTab ? ( await tab . webview ) . properties . url : "" ;
496496 }
497497
498- displayInitialCharLogo ( $img : HTMLImageElement , index : number ) : void {
499- // The index parameter is needed because webview[data-tab-id] can
500- // increment beyond the size of the sidebar org array and throw an
501- // error
502-
498+ displayInitialCharLogo ( $img : HTMLImageElement ) : void {
503499 const $altIcon = document . createElement ( "div" ) ;
504500 const $parent = $img . parentElement ! ;
505501 const $container = $parent . parentElement ! ;
@@ -520,7 +516,7 @@ export class ServerManagerView {
520516 $img . remove ( ) ;
521517 $parent . append ( $altIcon ) ;
522518
523- this . addContextMenu ( $altIcon , index ) ;
519+ this . addContextMenu ( $altIcon ) ;
524520 }
525521
526522 sidebarHoverEvent (
@@ -824,8 +820,8 @@ export class ServerManagerView {
824820 }
825821 }
826822
827- async isLoggedIn ( index : number ) : Promise < boolean > {
828- const tab = this . tabs [ index ] ;
823+ async isLoggedIn ( tabId : string ) : Promise < boolean > {
824+ const tab = this . getTabById ( tabId ) ;
829825 if ( ! ( tab instanceof ServerTab ) ) return false ;
830826 const webview = await tab . webview ;
831827 const url = webview . getWebContents ( ) . getURL ( ) ;
@@ -836,9 +832,16 @@ export class ServerManagerView {
836832 return this . tabs . find ( ( tab ) => tab . properties . tabId === tabId ) ;
837833 }
838834
839- addContextMenu ( $serverImg : HTMLElement , index : number ) : void {
835+ addContextMenu ( $serverImg : HTMLElement ) : void {
840836 $serverImg . addEventListener ( "contextmenu" , async ( event ) => {
841837 event . preventDefault ( ) ;
838+ const tabElement = $serverImg . closest ( ".tab" ) ;
839+ const tabId =
840+ tabElement instanceof HTMLElement
841+ ? tabElement . dataset . tabId
842+ : undefined ;
843+ if ( tabId === undefined ) return ;
844+
842845 const template = [
843846 {
844847 label : t . __ ( "Disconnect organization" ) ,
@@ -852,11 +855,11 @@ export class ServerManagerView {
852855 ) ,
853856 } ) ;
854857 if ( response === 0 ) {
855- if ( DomainUtil . removeDomain ( index ) ) {
858+ if ( DomainUtil . removeDomainById ( tabId ) ) {
856859 ipcRenderer . send ( "reload-full-app" ) ;
857860 } else {
858861 const { title, content} = Messages . orgRemovalError (
859- DomainUtil . getDomain ( index ) . url ,
862+ DomainUtil . getDomainById ( tabId ) ! . url ,
860863 ) ;
861864 dialog . showErrorBox ( title , content ) ;
862865 }
@@ -865,19 +868,19 @@ export class ServerManagerView {
865868 } ,
866869 {
867870 label : t . __ ( "Notification settings" ) ,
868- enabled : await this . isLoggedIn ( index ) ,
871+ enabled : await this . isLoggedIn ( tabId ) ,
869872 click : async ( ) => {
870873 // Switch to tab whose icon was right-clicked
871- const tab = this . tabs [ index ] ;
872- await this . activateTab ( tab . properties . tabId ) ;
874+ const tab = this . getTabById ( tabId ) ;
875+ await this . activateTab ( tabId ) ;
873876 if ( tab instanceof ServerTab )
874877 ( await tab . webview ) . showNotificationSettings ( ) ;
875878 } ,
876879 } ,
877880 {
878881 label : t . __ ( "Copy Zulip URL" ) ,
879882 click ( ) {
880- clipboard . writeText ( DomainUtil . getDomain ( index ) . url ) ;
883+ clipboard . writeText ( DomainUtil . getDomainById ( tabId ) ! . url ) ;
881884 } ,
882885 } ,
883886 ] ;
0 commit comments