Highlight just imported chats in the list

87d33b4d0befbb4fab22316846956adbb4348679

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

3 files changed, +34 -8Showing whitespace changes
public/script.js+24 -6
@@ -7360,8 +7360,9 @@ export function getCurrentChatDetails() {
73607360 * The function first fetches the chats, processes them, and then displays them in
73617361 * the HTML. It also has a built-in search functionality that allows filtering the
73627362 * displayed chats based on a search query.
7363+ * @param {string[]} hightlightNames - An array of chat names to highlight
73637364 */
73647365export async function displayPastChats(hightlightNames = []) {
73657366 $('#select_chat_div').empty();
73667367 $('#select_chat_search').val('').off('input');
73677368
@@ -7370,10 +7371,10 @@ export async function displayPastChats() {
73707371 const displayName = chatDetails.characterName;
73717372 const avatarImg = chatDetails.avatarImgURL;
73727373
73737374 await displayChats('', currentChat, displayName, avatarImg, selected_group, hightlightNames);
73747375
73757376 const debouncedDisplay = debounce((searchQuery) => {
73767377 displayChats(searchQuery, currentChat, displayName, avatarImg, selected_group, []);
73777378 });
73787379
73797380 // Define the search input listener
@@ -7389,7 +7390,7 @@ export async function displayPastChats() {
73897390 }, 200);
73907391}
73917392
73927393async function displayChats(searchQuery, currentChat, displayName, avatarImg, selected_group, highlightNames) {
73937394 try {
73947395 const trimExtension = (fileName) => String(fileName).replace('.jsonl', '');
73957396
@@ -7429,6 +7430,12 @@ async function displayChats(searchQuery, currentChat, displayName, avatarImg, se
74297430 }
74307431
74317432 $('#select_chat_div').append(template);
7433+
7434+ if (Array.isArray(highlightNames) && highlightNames.includes(chat.file_name)) {
7435+ const templateOffset = template.offset().top - template.parent().offset().top;
7436+ $('#select_chat_div').scrollTop(templateOffset);
7437+ flashHighlight(template, debounce_timeout.extended);
7438+ }
74327439 }
74337440 } catch (error) {
74347441 console.error('Error loading chats:', error);
@@ -8053,6 +8060,7 @@ export async function saveChatConditional() {
80538060 * @param {FormData} formData Form data to send to the server.
80548061 * @param {object} [options={}] Options for the import
80558062 * @param {boolean} [options.refresh] Whether to refresh the group chat list after import
8063+ * @returns {Promise<string[]>} List of imported file names.
80568064 */
80578065async function importCharacterChat(formData, { refresh = true } = {}) {
80588066 const fetchResult = await fetch('/api/chats/import', {
@@ -8067,7 +8075,10 @@ async function importCharacterChat(formData, { refresh = true } = {}) {
80678075 if (data.res && refresh) {
80688076 await displayPastChats();
80698077 }
8078+ return data?.fileNames || [];
80708079 }
8080+
8081+ return [];
80718082}
80728083
80738084function updateViewMessageIds(startFromZero = false) {
@@ -10616,6 +10627,8 @@ jQuery(async function () {
1061610627 return;
1061710628 }
1061810629
10630+ const importedFileNames = [];
10631+
1061910632 for (const file of targetElement.files) {
1062010633 const ext = file.name.match(/\.(\w+)$/);
1062110634 const format = ext?.[1]?.toLowerCase();
@@ -10636,10 +10649,15 @@ jQuery(async function () {
1063610649 formData.set('user_name', name1);
1063710650
1063810651 const importFn = selected_group ? importGroupChat : importCharacterChat;
1063910652 const result = await importFn(formData, { refresh: false });
10653+ importedFileNames.push(...result);
1064010654 }
1064110655
10642- await displayPastChats();
10656+ if (importedFileNames.length > 0) {
10657+ toastr.success(t`Successfully imported ${importedFileNames.length} chat(s).`);
10658+ }
10659+
10660+ await displayPastChats(importedFileNames);
1064310661
1064410662 targetElement.value = '';
1064510663 });
public/scripts/group-chats.js+5 -0
@@ -2066,6 +2066,7 @@ export async function deleteGroupChat(groupId, chatId, { jumpToNewChat = true }
20662066 * @param {FormData} formData Form data to send to the server
20672067 * @param {object} [options={}] Options for the import
20682068 * @param {boolean} [options.refresh] Whether to refresh the group chat list after import
2069+ * @returns {Promise<string[]>} List of imported file names
20692070 */
20702071export async function importGroupChat(formData, { refresh = true } = {}) {
20712072 const fetchResult = await fetch('/api/chats/group/import', {
@@ -2089,7 +2090,11 @@ export async function importGroupChat(formData, { refresh = true } = {}) {
20892090 }
20902091 }
20912092 }
2093+
2094+ return data?.fileNames || [];
20922095 }
2096+
2097+ return [];
20932098}
20942099
20952100export async function saveGroupBookmarkChat(groupId, name, metadata, mesId) {
src/endpoints/chats.js+5 -2
@@ -628,6 +628,7 @@ router.post('/import', validateAvatarUrlMiddleware, function (request, response)
628628 const avatarUrl = (request.body.avatar_url).replace('.png', '');
629629 const characterName = request.body.character_name;
630630 const userName = request.body.user_name || 'User';
631+ const fileNames = [];
631632
632633 if (!request.file) {
633634 return response.sendStatus(400);
@@ -662,6 +663,7 @@ router.post('/import', validateAvatarUrlMiddleware, function (request, response)
662663 const handleChat = (chat) => {
663664 const fileName = `${characterName} - ${humanizedISO8601DateTime()} imported.jsonl`;
664665 const filePath = path.join(request.user.directories.chats, avatarUrl, fileName);
666+ fileNames.push(fileName);
665667 writeFileAtomicSync(filePath, chat, 'utf8');
666668 };
667669
@@ -673,7 +675,7 @@ router.post('/import', validateAvatarUrlMiddleware, function (request, response)
673675 handleChat(chat);
674676 }
675677
676678 return response.send({ res: true, fileNames });
677679 }
678680
679681 if (format === 'jsonl') {
@@ -700,13 +702,14 @@ router.post('/import', validateAvatarUrlMiddleware, function (request, response)
700702
701703 const fileName = `${characterName} - ${humanizedISO8601DateTime()} imported.jsonl`;
702704 const filePath = path.join(request.user.directories.chats, avatarUrl, fileName);
705+ fileNames.push(fileName);
703706 if (flattenedChat !== data) {
704707 writeFileAtomicSync(filePath, flattenedChat, 'utf8');
705708 } else {
706709 fs.copyFileSync(pathToUpload, filePath);
707710 }
708711 fs.unlinkSync(pathToUpload);
709712 response.send({ res: true, fileNames });
710713 }
711714 } catch (error) {
712715 console.error(error);