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 * 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. * 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 * @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. * in descending order by file name. Returns `undefined` if the fetch request is unsuccessful.
*/ */
async function getPastCharacterChats() { export async function getPastCharacterChats(characterId = null) {
if (!characters[this_chid]) return; characterId = characterId ?? this_chid;
if (!characters[characterId]) return [];
const response = await fetch('/api/characters/chats', { const response = await fetch('/api/characters/chats', {
method: 'POST', method: 'POST',
body: JSON.stringify({ avatar_url: characters[this_chid].avatar }), body: JSON.stringify({ avatar_url: characters[characterId].avatar }),
headers: getRequestHeaders(), headers: getRequestHeaders(),
}); });
if (!response.ok) { if (!response.ok) {
return; return [];
} }
let data = await response.json(); 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} name - The name of the character to be deleted.
* @param {string} avatar - The avatar URL 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(); await clearChat();
$('#character_cross').click(); $('#character_cross').click();
this_chid = 'invalid-safety-id'; this_chid = 'invalid-safety-id';
@ -7664,7 +7668,7 @@ export async function deleteCharacter(name, avatar) {
$(document.getElementById('rm_button_selected_ch')).children('h2').text(''); $(document.getElementById('rm_button_selected_ch')).children('h2').text('');
this_chid = undefined; this_chid = undefined;
delete tag_map[avatar]; delete tag_map[avatar];
await getCharacters(); if (reloadCharacters) await getCharacters();
select_rm_info('char_delete', name); select_rm_info('char_delete', name);
await printMessages(); await printMessages();
saveSettingsDebounced(); saveSettingsDebounced();

View File

@ -7,9 +7,9 @@ import {
event_types, event_types,
eventSource, eventSource,
getCharacters, getCharacters,
getPastCharacterChats,
getRequestHeaders, getRequestHeaders,
printCharacters, printCharacters,
this_chid,
} from '../script.js'; } from '../script.js';
import { favsToHotswap } from './RossAscends-mods.js'; import { favsToHotswap } from './RossAscends-mods.js';
@ -123,27 +123,16 @@ class CharacterContextMenu {
cache: 'no-cache', cache: 'no-cache',
}).then(response => { }).then(response => {
if (response.ok) { if (response.ok) {
deleteCharacter(character.name, character.avatar).then(() => { return deleteCharacter(character.name, character.avatar, false).then(() => {
if (deleteChats) { eventSource.emit('characterDeleted', { id: characterId, character: characters[characterId] });
fetch('/api/characters/chats', { if (deleteChats) getPastCharacterChats(characterId).then(pastChats => {
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();
for (const chat of pastChats) { for (const chat of pastChats) {
const name = chat.file_name.replace('.jsonl', ''); const name = chat.file_name.replace('.jsonl', '');
eventSource.emit(event_types.CHAT_DELETED, name); eventSource.emit(event_types.CHAT_DELETED, name);
} }
}); });
}
}); });
} }
eventSource.emit('characterDeleted', { id: this_chid, character: characters[this_chid] });
}); });
}; };
@ -626,12 +615,11 @@ class BulkEditOverlay {
showLoader(); showLoader();
toastr.info('We\'re deleting your characters, please wait...', 'Working on it'); 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(() => getCharacters())
.then(() => this.browseState()) .then(() => this.browseState())
.finally(() => hideLoader()); .finally(() => hideLoader());
}, });
);
}; };
/** /**