@@ -441,14 +441,14 @@ export class ServerManagerView {
441441 initServerActions ( ) : void {
442442 const $serverImgs : NodeListOf < HTMLImageElement > =
443443 document . querySelectorAll ( ".server-icons" ) ;
444- for ( const [ index , $serverImg ] of $serverImgs . entries ( ) ) {
445- this . addContextMenu ( $serverImg , index ) ;
444+ for ( const $serverImg of $serverImgs ) {
445+ this . addContextMenu ( $serverImg ) ;
446446 if ( $serverImg . src === defaultIcon ) {
447- this . displayInitialCharLogo ( $serverImg , index ) ;
447+ this . displayInitialCharLogo ( $serverImg ) ;
448448 }
449449
450450 $serverImg . addEventListener ( "error" , ( ) => {
451- this . displayInitialCharLogo ( $serverImg , index ) ;
451+ this . displayInitialCharLogo ( $serverImg ) ;
452452 } ) ;
453453 }
454454 }
@@ -500,11 +500,7 @@ export class ServerManagerView {
500500 return tab instanceof ServerTab ? ( await tab . webview ) . properties . url : "" ;
501501 }
502502
503- displayInitialCharLogo ( $img : HTMLImageElement , index : number ) : void {
504- // The index parameter is needed because webview[data-tab-id] can
505- // increment beyond the size of the sidebar org array and throw an
506- // error
507-
503+ displayInitialCharLogo ( $img : HTMLImageElement ) : void {
508504 const $altIcon = document . createElement ( "div" ) ;
509505 const $parent = $img . parentElement ! ;
510506 const $container = $parent . parentElement ! ;
@@ -525,7 +521,7 @@ export class ServerManagerView {
525521 $img . remove ( ) ;
526522 $parent . append ( $altIcon ) ;
527523
528- this . addContextMenu ( $altIcon , index ) ;
524+ this . addContextMenu ( $altIcon ) ;
529525 }
530526
531527 sidebarHoverEvent (
@@ -818,8 +814,8 @@ export class ServerManagerView {
818814 }
819815 }
820816
821- async isLoggedIn ( index : number ) : Promise < boolean > {
822- const tab = this . tabs [ index ] ;
817+ async isLoggedIn ( tabId : string ) : Promise < boolean > {
818+ const tab = this . getTabById ( tabId ) ;
823819 if ( ! ( tab instanceof ServerTab ) ) return false ;
824820 const webview = await tab . webview ;
825821 const url = webview . getWebContents ( ) . getURL ( ) ;
@@ -830,9 +826,16 @@ export class ServerManagerView {
830826 return this . tabs . find ( ( tab ) => tab . properties . tabId === tabId ) ;
831827 }
832828
833- addContextMenu ( $serverImg : HTMLElement , index : number ) : void {
829+ addContextMenu ( $serverImg : HTMLElement ) : void {
834830 $serverImg . addEventListener ( "contextmenu" , async ( event ) => {
835831 event . preventDefault ( ) ;
832+ const tabElement = $serverImg . closest ( ".tab" ) ;
833+ const tabId =
834+ tabElement instanceof HTMLElement
835+ ? tabElement . dataset . tabId
836+ : undefined ;
837+ if ( tabId === undefined ) return ;
838+
836839 const template = [
837840 {
838841 label : t . __ ( "Disconnect organization" ) ,
@@ -846,11 +849,11 @@ export class ServerManagerView {
846849 ) ,
847850 } ) ;
848851 if ( response === 0 ) {
849- if ( DomainUtil . removeDomain ( index ) ) {
852+ if ( DomainUtil . removeDomainById ( tabId ) ) {
850853 ipcRenderer . send ( "reload-full-app" ) ;
851854 } else {
852855 const { title, content} = Messages . orgRemovalError (
853- DomainUtil . getDomain ( index ) . url ,
856+ DomainUtil . getDomainById ( tabId ) ! . url ,
854857 ) ;
855858 dialog . showErrorBox ( title , content ) ;
856859 }
@@ -859,19 +862,19 @@ export class ServerManagerView {
859862 } ,
860863 {
861864 label : t . __ ( "Notification settings" ) ,
862- enabled : await this . isLoggedIn ( index ) ,
865+ enabled : await this . isLoggedIn ( tabId ) ,
863866 click : async ( ) => {
864867 // Switch to tab whose icon was right-clicked
865- const tab = this . tabs [ index ] ;
866- await this . activateTab ( tab . properties . tabId ) ;
868+ const tab = this . getTabById ( tabId ) ;
869+ await this . activateTab ( tabId ) ;
867870 if ( tab instanceof ServerTab )
868871 ( await tab . webview ) . showNotificationSettings ( ) ;
869872 } ,
870873 } ,
871874 {
872875 label : t . __ ( "Copy Zulip URL" ) ,
873876 click ( ) {
874- clipboard . writeText ( DomainUtil . getDomain ( index ) . url ) ;
877+ clipboard . writeText ( DomainUtil . getDomainById ( tabId ) ! . url ) ;
875878 } ,
876879 } ,
877880 ] ;
0 commit comments