Return to using string type for this_chid

b2ce76c84c480b318426b45f6673759e63784a25

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +23 -6Ignore whitespace
public/script.js+23 -6
@@ -1378,7 +1378,7 @@ export async function selectCharacterById(id) {
13781378 return;
13791379 }
13801380
13811381 if (selected_group || String(this_chid) !== String(id)) {
13821382 //if clicked on a different character from what was currently selected
13831383 if (!is_send_press) {
13841384 await clearChat();
@@ -1386,7 +1386,7 @@ export async function selectCharacterById(id) {
13861386 resetSelectedGroup();
13871387 this_edit_mes_id = undefined;
13881388 selected_button = 'character_edit';
1389- this_chid = id;
1389+ setCharacterId(id);
13901390 chat.length = 0;
13911391 chat_metadata = {};
13921392 await getChat();
@@ -6233,8 +6233,25 @@ export function setExternalAbortController(controller) {
62336233 abortController = controller;
62346234}
62356235
6236+/**
6237+ * Sets a character array index.
6238+ * @param {number|string|undefined} value
6239+ */
62366240export 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+ }
62386255}
62396256
62406257export function setCharacterName(value) {
@@ -6350,13 +6367,13 @@ export async function renameCharacter(name = null, { silent = false, renameChats
63506367
63516368 if (newChId !== -1) {
63526369 // Select the character after the renaming
6353- this_chid = -1;
6370+ setCharacterId(undefined);
63546371 await selectCharacterById(newChId);
63556372
63566373 // Async delay to update UI
63576374 await delay(1);
63586375
63596376 if (this_chid === -1undefined) {
63606377 throw new Error('New character not selected');
63616378 }
63626379
@@ -7658,7 +7675,7 @@ export function select_rm_info(type, charId, previousCharId = null) {
76587675 if (previousCharId) {
76597676 const newId = characters.findIndex((x) => x.avatar == previousCharId);
76607677 if (newId >= 0) {
7661- this_chid = newId;
7678+ setCharacterId(newId);
76627679 }
76637680 }
76647681}