Merge pull request #1498 from artisticMink/bulkedit-delete-optimization

Only refresh character list after all deletions have been processed.
This commit is contained in:
Cohee
2023-12-09 18:07:44 +02:00
committed by GitHub
2 changed files with 21 additions and 29 deletions

View File

@ -5921,20 +5921,23 @@ export async function getChatsFromFiles(data, isGroupChat) {
* The function sends a POST request to the server to retrieve all chats for the character. It then
* processes the received data, sorts it by the file name, and returns the sorted data.
*
* @param {null|number} [characterId=null] - When set, the function will use this character id instead of this_chid.
*
* @returns {Promise<Array>} - An array containing metadata of all past chats of the character, sorted
* in descending order by file name. Returns `undefined` if the fetch request is unsuccessful.
*/
async function getPastCharacterChats() {
if (!characters[this_chid]) return;
export async function getPastCharacterChats(characterId = null) {
characterId = characterId ?? this_chid;
if (!characters[characterId]) return [];
const response = await fetch('/api/characters/chats', {
method: 'POST',
body: JSON.stringify({ avatar_url: characters[this_chid].avatar }),
body: JSON.stringify({ avatar_url: characters[characterId].avatar }),
headers: getRequestHeaders(),
});
if (!response.ok) {
return;
return [];
}
let data = await response.json();
@ -7652,8 +7655,9 @@ export async function handleDeleteCharacter(popup_type, this_chid, delete_chats)
*
* @param {string} name - The name of the character to be deleted.
* @param {string} avatar - The avatar URL of the character to be deleted.
* @param {boolean} reloadCharacters - Whether the character list should be refreshed after deletion.
*/
export async function deleteCharacter(name, avatar) {
export async function deleteCharacter(name, avatar, reloadCharacters = true) {
await clearChat();
$('#character_cross').click();
this_chid = 'invalid-safety-id';
@ -7664,7 +7668,7 @@ export async function deleteCharacter(name, avatar) {
$(document.getElementById('rm_button_selected_ch')).children('h2').text('');
this_chid = undefined;
delete tag_map[avatar];
await getCharacters();
if (reloadCharacters) await getCharacters();
select_rm_info('char_delete', name);
await printMessages();
saveSettingsDebounced();

View File

@ -7,9 +7,9 @@ import {
event_types,
eventSource,
getCharacters,
getPastCharacterChats,
getRequestHeaders,
printCharacters,
this_chid,
} from '../script.js';
import { favsToHotswap } from './RossAscends-mods.js';
@ -123,27 +123,16 @@ class CharacterContextMenu {
cache: 'no-cache',
}).then(response => {
if (response.ok) {
deleteCharacter(character.name, character.avatar).then(() => {
if (deleteChats) {
fetch('/api/characters/chats', {
method: 'POST',
body: JSON.stringify({ avatar_url: character.avatar }),
headers: getRequestHeaders(),
}).then((response) => {
let data = response.json();
data = Object.values(data);
const pastChats = data.sort((a, b) => a['file_name'].localeCompare(b['file_name'])).reverse();
return deleteCharacter(character.name, character.avatar, false).then(() => {
eventSource.emit('characterDeleted', { id: characterId, character: characters[characterId] });
if (deleteChats) getPastCharacterChats(characterId).then(pastChats => {
for (const chat of pastChats) {
const name = chat.file_name.replace('.jsonl', '');
eventSource.emit(event_types.CHAT_DELETED, name);
}
});
}
});
}
eventSource.emit('characterDeleted', { id: this_chid, character: characters[this_chid] });
});
};
@ -626,12 +615,11 @@ class BulkEditOverlay {
showLoader();
toastr.info('We\'re deleting your characters, please wait...', 'Working on it');
Promise.all(this.selectedCharacters.map(async characterId => CharacterContextMenu.delete(characterId, deleteChats)))
Promise.allSettled(this.selectedCharacters.map(async characterId => CharacterContextMenu.delete(characterId, deleteChats)))
.then(() => getCharacters())
.then(() => this.browseState())
.finally(() => hideLoader());
},
);
});
};
/**