"Back to main" option for bookmarks

This commit is contained in:
SillyLossy
2023-03-24 15:15:26 +02:00
parent 662cce8c6c
commit b5b9538c08
4 changed files with 77 additions and 37 deletions

View File

@@ -2,11 +2,11 @@ import {
characters,
saveChat,
sendSystemMessage,
deleteLastMessage,
token,
system_messages,
system_message_types,
this_chid,
openCharacterChat,
} from "../script.js";
import { selected_group } from "./group-chats.js";
@@ -14,14 +14,20 @@ import {
stringFormat,
} from "./utils.js";
export {
showBookmarksButtons,
}
const bookmarkNameToken = 'Bookmark #';
async function getExistingChatNames() {
const response = await fetch("/getallchatsofcharacter", {
const response = await fetch("/getallchatsofcharacter", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
"X-CSRF-Token": token,
},
body: JSON.stringify({ avatar_url: characters[this_chid].avatar})
body: JSON.stringify({ avatar_url: characters[this_chid].avatar })
});
if (response.ok) {
@@ -31,18 +37,14 @@ async function getExistingChatNames() {
}
async function getBookmarkName(currentChat) {
const nameToken = 'Bookmark #';
if (currentChat.includes(nameToken)) {
currentChat = currentChat.substring(0, currentChat.lastIndexOf(nameToken)).trim();
}
const chatNames = await getExistingChatNames();
let mainChat = getMainChatName(currentChat);
let newChat = Date.now();
let friendlyName = '';
for (let i = 0; i < 1000; i++) {
friendlyName = `${nameToken}${i}`;
newChat = `${currentChat} ${friendlyName}`;
friendlyName = `${bookmarkNameToken}${i}`;
newChat = `${mainChat} ${friendlyName}`;
if (!chatNames.includes(newChat)) {
break;
}
@@ -50,11 +52,37 @@ async function getBookmarkName(currentChat) {
return { newChat, friendlyName };
}
function getMainChatName(currentChat) {
if (currentChat.includes(bookmarkNameToken)) {
currentChat = currentChat.substring(0, currentChat.lastIndexOf(bookmarkNameToken)).trim();
}
return currentChat;
}
function showBookmarksButtons() {
// In groups
if (selected_group) {
$("#option_back_to_main").hide();
$("#option_new_bookmark").hide();
}
// In main chat
else if (!characters[this_chid].chat.includes(bookmarkNameToken)) {
$("#option_back_to_main").hide();
$("#option_new_bookmark").show();
}
// In bookmark chat
else {
$("#option_back_to_main").show();
$("#option_new_bookmark").show();
}
}
$(document).ready(function () {
$('#option_new_bookmark').on('click', async function () {
if (selected_group) {
alert('Unsupported for groups');
throw new Error('not yet implemented');
alert('Chat bookmarks unsupported for groups');
throw new Error();
}
let { newChat, friendlyName } = await getBookmarkName(characters[this_chid].chat);
@@ -64,5 +92,13 @@ $(document).ready(function () {
sendSystemMessage(system_message_types.BOOKMARK_CREATED, mainMessage);
saveChat();
});
});
$('#option_back_to_main').on('click', async function() {
const mainChatName = getMainChatName(characters[this_chid].chat);
const allChats = await getExistingChatNames();
if (allChats.includes(mainChatName)) {
openCharacterChat(mainChatName);
}
});
});