11let accounts = [ ] ;
2+ let restoringSelectedAccount = false ;
23let iii = null ;
34let lastAccountsReadError = null ;
45let cleanupAccountsDropdownListeners = null ;
@@ -110,6 +111,18 @@ async function safeReadAccounts() {
110111 }
111112
112113 lastAccountsReadError = null ;
114+ // Sort alphabetically by displayName (case-insensitive); fallback to accountId
115+ try {
116+ result . sort ( ( a , b ) => {
117+ const nameA = ( a ?. displayName || a ?. accountId || '' ) . toString ( ) . trim ( ) . toLowerCase ( ) ;
118+ const nameB = ( b ?. displayName || b ?. accountId || '' ) . toString ( ) . trim ( ) . toLowerCase ( ) ;
119+ if ( nameA < nameB ) return - 1 ;
120+ if ( nameA > nameB ) return 1 ;
121+ return 0 ;
122+ } ) ;
123+ } catch ( _ ) {
124+ // Ignore sort errors
125+ }
113126 return result ;
114127}
115128
@@ -232,6 +245,8 @@ async function handleJagexAccountLogic(properties) {
232245 updateCharacterSelection ( accounts [ 0 ] . accountId ) ;
233246 }
234247 }
248+ // Attempt to restore persisted selection after any file change
249+ await restoreSelectedAccountIfAny ( ) ;
235250 }
236251 } , 1000 ) ;
237252}
@@ -339,6 +354,9 @@ window.addEventListener('load', async () => {
339354 const profile = nonJagexProfile || 'default' ;
340355 document . getElementById ( 'profile' ) . value = profile ;
341356 }
357+ if ( ! restoringSelectedAccount ) {
358+ await persistSelectedAccount ( selectedAccount ) ;
359+ }
342360 } ) ;
343361
344362 //Init buttons and UI
@@ -864,6 +882,48 @@ function setupAddAccountsButton() {
864882 addAccountsButton ?. addEventListener ( 'click' , addAccountsHandler ) ;
865883}
866884
885+ function setupRefreshAccountsButton ( ) {
886+ const refreshBtn = document . getElementById ( 'refresh-accounts' ) ;
887+ if ( ! refreshBtn ) return ;
888+ refreshBtn . addEventListener ( 'click' , async ( ) => {
889+ refreshBtn . disabled = true ;
890+ try {
891+ const result = await window . electron . refreshAccounts ( ) ;
892+ if ( result ?. error ) {
893+ window . electron . errorAlert ( result . error ) ;
894+ return ;
895+ }
896+ const updated = result ?. accounts ;
897+ if ( Array . isArray ( updated ) ) {
898+ // Sort updated accounts alphabetically
899+ accounts = [ ...updated ] . sort ( ( a , b ) => {
900+ const nameA = ( a ?. displayName || a ?. accountId || '' ) . toString ( ) . trim ( ) . toLowerCase ( ) ;
901+ const nameB = ( b ?. displayName || b ?. accountId || '' ) . toString ( ) . trim ( ) . toLowerCase ( ) ;
902+ if ( nameA < nameB ) return - 1 ;
903+ if ( nameA > nameB ) return 1 ;
904+ return 0 ;
905+ } ) ;
906+ await setupSidebarLayout ( accounts . length ) ;
907+ await updateProfileBasedOnCharacter ( ) ;
908+ await restoreSelectedAccountIfAny ( ) ;
909+ } else {
910+ // fallback: re-read accounts via existing flow
911+ const latestAccounts = await safeReadAccounts ( ) ;
912+ if ( latestAccounts ) {
913+ accounts = latestAccounts ; // already sorted in safeReadAccounts
914+ await setupSidebarLayout ( accounts . length ) ;
915+ await updateProfileBasedOnCharacter ( ) ;
916+ await restoreSelectedAccountIfAny ( ) ;
917+ }
918+ }
919+ } catch ( err ) {
920+ window . electron . errorAlert ( err ?. message || String ( err ) ) ;
921+ } finally {
922+ refreshBtn . disabled = false ;
923+ }
924+ } ) ;
925+ }
926+
867927/**
868928 * Extracts the version number from a string.
869929 * e.g., "microbot-1.9.6.1.jar" becomes "1.9.6.1"
@@ -1142,6 +1202,45 @@ async function updateProfileBasedOnCharacter() {
11421202 }
11431203}
11441204
1205+ async function persistSelectedAccount ( accountId ) {
1206+ try {
1207+ const props = await window . electron . readProperties ( ) ;
1208+ if ( ! accountId || accountId === 'none' ) {
1209+ if ( props [ 'selected_account' ] ) {
1210+ delete props [ 'selected_account' ] ;
1211+ await window . electron . writeProperties ( props ) ;
1212+ }
1213+ return ;
1214+ }
1215+ if ( props [ 'selected_account' ] !== accountId ) {
1216+ props [ 'selected_account' ] = accountId ;
1217+ await window . electron . writeProperties ( props ) ;
1218+ }
1219+ } catch ( err ) {
1220+ window . electron . logError ( `Failed to persist selected account: ${ err ?. message || err } ` ) ;
1221+ }
1222+ }
1223+
1224+ async function restoreSelectedAccountIfAny ( ) {
1225+ try {
1226+ const props = await window . electron . readProperties ( ) ;
1227+ const saved = props [ 'selected_account' ] ;
1228+ if ( ! saved ) return ;
1229+ const exists = accounts . some ( ( a ) => a . accountId === saved ) ;
1230+ if ( ! exists ) {
1231+ delete props [ 'selected_account' ] ;
1232+ await window . electron . writeProperties ( props ) ;
1233+ return ;
1234+ }
1235+ restoringSelectedAccount = true ;
1236+ updateCharacterSelection ( saved , { suppressRender : true } ) ;
1237+ restoringSelectedAccount = false ;
1238+ await updateProfileBasedOnCharacter ( ) ;
1239+ } catch ( err ) {
1240+ window . electron . logError ( `Failed to restore selected account: ${ err ?. message || err } ` ) ;
1241+ }
1242+ }
1243+
11451244async function initUI ( properties ) {
11461245 updateNowBtn ( ) ;
11471246 reminderMeLaterBtn ( ) ;
@@ -1159,6 +1258,7 @@ async function initUI(properties) {
11591258 const accountsData = await safeReadAccounts ( ) ;
11601259 accounts = accountsData ?? [ ] ;
11611260 await setupSidebarLayout ( accounts . length ) ;
1261+ await restoreSelectedAccountIfAny ( ) ;
11621262
11631263 const orderedClientJars = await orderClientJarsByVersion ( ) ;
11641264 populateSelectElement ( 'client' , orderedClientJars ) ;
@@ -1175,6 +1275,8 @@ async function initUI(properties) {
11751275
11761276 await setupRamInput ( ) ;
11771277 await setupProxyInput ( ) ;
1278+
1279+ setupRefreshAccountsButton ( ) ;
11781280}
11791281
11801282/**
0 commit comments