Fix: Close chat before performing deletion (#4545)

85fc7fe971a459a568df6afb26d4c8b438c57fc6

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

Signed
1 files changed, +45 -18Ignore whitespace
public/script.js+45 -18
@@ -9080,6 +9080,34 @@ export async function renameChat(oldFileName, newName) {
90809080}
90819081
90829082/**
9083+ * Closes the current chat, clearing all associated data and resetting the UI.
9084+ * If a message generation is in progress, it prompts the user to stop it first.
9085+ * @returns {Promise<boolean>} True if the chat was successfully closed, false otherwise.
9086+ */
9087+export async function closeCurrentChat() {
9088+ if (is_send_press == false) {
9089+ await waitUntilCondition(() => !isChatSaving, debounce_timeout.extended, 10);
9090+ await clearChat();
9091+ chat.length = 0;
9092+ resetSelectedGroup();
9093+ setCharacterId(undefined);
9094+ setCharacterName('');
9095+ setActiveCharacter(null);
9096+ setActiveGroup(null);
9097+ this_edit_mes_id = undefined;
9098+ chat_metadata = {};
9099+ selected_button = 'characters';
9100+ $('#rm_button_selected_ch').children('h2').text('');
9101+ select_rm_characters();
9102+ await eventSource.emit(event_types.CHAT_CHANGED, getCurrentChatId());
9103+ return true;
9104+ } else {
9105+ toastr.info(t`Please stop the message generation first.`);
9106+ return false;
9107+ }
9108+}
9109+
9110+/**
90839111 * Forces the update of the chat name for a remote character.
90849112 * @param {string|number} characterId Character ID to update chat name for
90859113 * @param {string} newName New name for the chat
@@ -9144,6 +9172,22 @@ export async function deleteCharacter(characterKey, { deleteChats = true } = {})
91449172 characterKey = [characterKey];
91459173 }
91469174
9175+ const inTempChat = this_chid === undefined && name2 === neutralCharacterName;
9176+ if (inTempChat) {
9177+ const confirmClose = await Popup.show.confirm(
9178+ t`You are currently in a temporary chat.`,
9179+ t`Deleting this character will close the chat and you will lose any unsaved messages. Do you want to proceed?`,
9180+ );
9181+ if (!confirmClose) {
9182+ return;
9183+ }
9184+ }
9185+
9186+ const closeChatResult = await closeCurrentChat();
9187+ if (!closeChatResult) {
9188+ return;
9189+ }
9190+
91479191 for (const key of characterKey) {
91489192 const character = characters.find(x => x.avatar == key);
91499193 if (!character) {
@@ -9947,24 +9991,7 @@ jQuery(async function () {
99479991 }
99489992
99499993 else if (id == 'option_close_chat') {
9950- if (is_send_press == false) {
9994+ await closeCurrentChat();
9951- await waitUntilCondition(() => !isChatSaving, debounce_timeout.extended, 10);
9952- await clearChat();
9953- chat.length = 0;
9954- resetSelectedGroup();
9955- setCharacterId(undefined);
9956- setCharacterName('');
9957- setActiveCharacter(null);
9958- setActiveGroup(null);
9959- this_edit_mes_id = undefined;
9960- chat_metadata = {};
9961- selected_button = 'characters';
9962- $('#rm_button_selected_ch').children('h2').text('');
9963- select_rm_characters();
9964- await eventSource.emit(event_types.CHAT_CHANGED, getCurrentChatId());
9965- } else {
9966- toastr.info(t`Please stop the message generation first.`);
9967- }
99689995 }
99699996
99709997 else if (id === 'option_settings') {