Return to using string type for this_chid
| @@ -1378,7 +1378,7 @@ export async function selectCharacterById(id) { | ||
| 1378 | 1378 | return; |
| 1379 | 1379 | } |
| 1380 | 1380 | |
| 1381 | 1381 | if (selected_group || String(this_chid) !== String(id)) { |
| 1382 | 1382 | //if clicked on a different character from what was currently selected |
| 1383 | 1383 | if (!is_send_press) { |
| 1384 | 1384 | await clearChat(); |
| @@ -1386,7 +1386,7 @@ export async function selectCharacterById(id) { | ||
| 1386 | 1386 | resetSelectedGroup(); |
| 1387 | 1387 | this_edit_mes_id = undefined; |
| 1388 | 1388 | selected_button = 'character_edit'; |
| 1389 | - this_chid = id; | |
| 1389 | + setCharacterId(id); | |
| 1390 | 1390 | chat.length = 0; |
| 1391 | 1391 | chat_metadata = {}; |
| 1392 | 1392 | await getChat(); |
| @@ -6233,8 +6233,25 @@ export function setExternalAbortController(controller) { | ||
| 6233 | 6233 | abortController = controller; |
| 6234 | 6234 | } |
| 6235 | 6235 | |
| 6236 | +/** | |
| 6237 | + * Sets a character array index. | |
| 6238 | + * @param {number|string|undefined} value | |
| 6239 | + */ | |
| 6236 | 6240 | export function setCharacterId(value) { |
| 6237 | - this_chid = value; | |
| 6241 | + switch (typeof value) { | |
| 6242 | + case 'number': | |
| 6243 | + this_chid = String(value); | |
| 6244 | + break; | |
| 6245 | + case 'string': | |
| 6246 | + this_chid = !isNaN(parseInt(value)) ? value : undefined; | |
| 6247 | + break; | |
| 6248 | + case 'undefined': | |
| 6249 | + this_chid = undefined; | |
| 6250 | + break; | |
| 6251 | + default: | |
| 6252 | + console.error('Invalid character ID type:', value); | |
| 6253 | + break; | |
| 6254 | + } | |
| 6238 | 6255 | } |
| 6239 | 6256 | |
| 6240 | 6257 | export function setCharacterName(value) { |
| @@ -6350,13 +6367,13 @@ export async function renameCharacter(name = null, { silent = false, renameChats | ||
| 6350 | 6367 | |
| 6351 | 6368 | if (newChId !== -1) { |
| 6352 | 6369 | // Select the character after the renaming |
| 6353 | - this_chid = -1; | |
| 6370 | + setCharacterId(undefined); | |
| 6354 | 6371 | await selectCharacterById(newChId); |
| 6355 | 6372 | |
| 6356 | 6373 | // Async delay to update UI |
| 6357 | 6374 | await delay(1); |
| 6358 | 6375 | |
| 6359 | 6376 | if (this_chid === -1undefined) { |
| 6360 | 6377 | throw new Error('New character not selected'); |
| 6361 | 6378 | } |
| 6362 | 6379 | |
| @@ -7658,7 +7675,7 @@ export function select_rm_info(type, charId, previousCharId = null) { | ||
| 7658 | 7675 | if (previousCharId) { |
| 7659 | 7676 | const newId = characters.findIndex((x) => x.avatar == previousCharId); |
| 7660 | 7677 | if (newId >= 0) { |
| 7661 | - this_chid = newId; | |
| 7678 | + setCharacterId(newId); | |
| 7662 | 7679 | } |
| 7663 | 7680 | } |
| 7664 | 7681 | } |