@@ -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 (
@@ -813,8 +809,8 @@ export class ServerManagerView {
813809 }
814810 }
815811
816- async isLoggedIn ( index : number ) : Promise < boolean > {
817- const tab = this . tabs [ index ] ;
812+ async isLoggedIn ( tabId : string ) : Promise < boolean > {
813+ const tab = this . getTabById ( tabId ) ;
818814 if ( ! ( tab instanceof ServerTab ) ) return false ;
819815 const webview = await tab . webview ;
820816 const url = webview . getWebContents ( ) . getURL ( ) ;
@@ -825,9 +821,16 @@ export class ServerManagerView {
825821 return this . tabs . find ( ( tab ) => tab . properties . tabId === tabId ) ;
826822 }
827823
828- addContextMenu ( $serverImg : HTMLElement , index : number ) : void {
824+ addContextMenu ( $serverImg : HTMLElement ) : void {
829825 $serverImg . addEventListener ( "contextmenu" , async ( event ) => {
830826 event . preventDefault ( ) ;
827+ const tabElement = $serverImg . closest ( ".tab" ) ;
828+ const tabId =
829+ tabElement instanceof HTMLElement
830+ ? tabElement . dataset . tabId
831+ : undefined ;
832+ if ( tabId === undefined ) return ;
833+
831834 const template = [
832835 {
833836 label : t . __ ( "Disconnect organization" ) ,
@@ -841,11 +844,11 @@ export class ServerManagerView {
841844 ) ,
842845 } ) ;
843846 if ( response === 0 ) {
844- if ( DomainUtil . removeDomain ( index ) ) {
847+ if ( DomainUtil . removeDomainById ( tabId ) ) {
845848 ipcRenderer . send ( "reload-full-app" ) ;
846849 } else {
847850 const { title, content} = Messages . orgRemovalError (
848- DomainUtil . getDomain ( index ) . url ,
851+ DomainUtil . getDomainById ( tabId ) ! . url ,
849852 ) ;
850853 dialog . showErrorBox ( title , content ) ;
851854 }
@@ -854,19 +857,19 @@ export class ServerManagerView {
854857 } ,
855858 {
856859 label : t . __ ( "Notification settings" ) ,
857- enabled : await this . isLoggedIn ( index ) ,
860+ enabled : await this . isLoggedIn ( tabId ) ,
858861 click : async ( ) => {
859862 // Switch to tab whose icon was right-clicked
860- const tab = this . tabs [ index ] ;
861- await this . activateTab ( tab . properties . tabId ) ;
863+ const tab = this . getTabById ( tabId ) ;
864+ await this . activateTab ( tabId ) ;
862865 if ( tab instanceof ServerTab )
863866 ( await tab . webview ) . showNotificationSettings ( ) ;
864867 } ,
865868 } ,
866869 {
867870 label : t . __ ( "Copy Zulip URL" ) ,
868871 click ( ) {
869- clipboard . writeText ( DomainUtil . getDomain ( index ) . url ) ;
872+ clipboard . writeText ( DomainUtil . getDomainById ( tabId ) ! . url ) ;
870873 } ,
871874 } ,
872875 ] ;
0 commit comments