Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
fix conversation update
Browse files Browse the repository at this point in the history
  • Loading branch information
mkloubert committed Aug 2, 2023
1 parent a1092fe commit 81ce20f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 20 deletions.
16 changes: 12 additions & 4 deletions .ui-dev/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Chat from './components/Chat';
import Chatbar from './components/Chatbar';
import Promptbar from './components/Promptbar';
import type { ChatConversationItem, ChatPromptItem, IApiKeySettings, IAppContext, IChatConversation, ISettings } from './types';
import { replaceNewConversationInList } from './utils';

// styles
import './App.css';
Expand Down Expand Up @@ -118,7 +119,8 @@ const App: React.FC = () => {

const updateSettings = useCallback(async (
newConversationList: Nilable<ChatConversationItem[]>,
newPromptList: Nilable<ChatPromptItem[]>
newPromptList: Nilable<ChatPromptItem[]>,
force = false
) => {
if (!isInitialized) {
return;
Expand All @@ -133,7 +135,7 @@ const App: React.FC = () => {
promptItems: newPromptList
};

if (_.isEqual(currentSettings, newSettings)) {
if (!force && _.isEqual(currentSettings, newSettings)) {
return;
}

Expand Down Expand Up @@ -183,11 +185,17 @@ const App: React.FC = () => {

const handleRefresh = useCallback((newSelectedConversation: Nilable<IChatConversation>) => {
if (newSelectedConversation) {
updateSettings(
replaceNewConversationInList(currentSettings.conversationItems, newSelectedConversation),
promptItems,
true
);

setSelectedConversation(newSelectedConversation);
} else {
setSelectedConversation(newSelectedConversation === null ? undefined : null)
setSelectedConversation(newSelectedConversation === null ? undefined : null);
}
}, []);
}, [currentSettings.conversationItems, promptItems, updateSettings]);

const handleConversationUpdate = useCallback((conversation: IChatConversation) => {
if (!conversationItems) {
Expand Down
25 changes: 12 additions & 13 deletions .ui-dev/src/components/Chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ const Chat: React.FC<IChatProps> = ({

done(null);

onRefresh({
onRefresh(_.cloneDeep({
...selectedConversation,

messages: conversationMessages
});
messages: [...conversationMessages]
}));
} catch (error: any) {
console.error('[ERROR]', 'Chat.handleSend(1)', error);

Expand Down Expand Up @@ -309,24 +309,23 @@ const Chat: React.FC<IChatProps> = ({
message={message}
messageIndex={index}
onDelete={(messageIndex, conversation) => {
const copyOfConversation = {
...conversation
};
const copyOfConversation = _.clone({
...conversation,

copyOfConversation.messages = conversation.messages.slice(0, index);
messages: conversation.messages.slice(0, messageIndex)
});

onConversationUpdate(copyOfConversation);
if (copyOfConversation.id === selectedConversation?.id) {
onRefresh(copyOfConversation);
}
}}
onEdit={(editedMessage, conversation) => {
const copyOfConversation = {
...conversation
};
copyOfConversation.messages = [
...conversation.messages
];
const copyOfConversation = _.clone({
...conversation,

messages: [...conversation.messages]
});

copyOfConversation.messages[index] = editedMessage;

Expand Down
66 changes: 65 additions & 1 deletion .ui-dev/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ import type { Nilable, Optional } from "@egomobile/types";
import { parse as parseHtml } from 'node-html-parser';

// internal imports
import { IChatConversation, IChatPrompt, IFolder, IVariable, VariableInputType } from '../types';
import { ChatConversationItem, IChatConversation, IChatPrompt, IFolder, IVariable, VariableInputType } from '../types';

/**
* An action for a list iteration for an item.
*
* @param {T} item The current item.
* @param {number} index The zero-based index.
* @param {T[]} orgArr The original input array.
*/
export type ForEachAction<T = any> = (item: T, index: number, orgArr: T[]) => any;

/**
* Options for `parseFinalContentWithVariables()` function.
Expand Down Expand Up @@ -119,6 +128,18 @@ export function filterChatPrompts(prompts: IChatPrompt[], searchTerm: any): ICha
});
}

/**
* Creates a copy of an array and iterates over it.
*
* @param {T[]} arr The original input array.
* @param {ForEachAction<T>} action The action to invoke.
*/
export function forEachOfCopy<T>(arr: T[], action: ForEachAction<T>): void {
[...arr].forEach((item, index) => {
action(item, index, arr);
});
}

/**
* Generates a random string of a given length.
*
Expand Down Expand Up @@ -329,6 +350,49 @@ export function parseVariables(content: any): IVariable[] {
return foundVariables;
}

/**
* Replaces an `IChatConversation` in an list of `ChatConversationItem` items.
* @param {Nilable<ChatConversationItem[]>} items The input list.
* @param {IChatConversation} conversation The current item.
*
* @returns {Nilable<ChatConversationItem[]>} The copy of `items` with the new, original element from `conversation`.
*/
export function replaceNewConversationInList(
items: Nilable<ChatConversationItem[]>,
conversation: IChatConversation
): Nilable<ChatConversationItem[]> {
if (!items) {
return items;
}

const replaceWithItemIn = (list: ChatConversationItem[]) => {
forEachOfCopy(list, (item, index, orgArr) => {
if (!('conversations' in item) && item.id === conversation.id) {
orgArr[index] = conversation;
}
});
};

const cloneOfItems = [...items.map(_.cloneDeep)];

if (conversation.folderId) {
forEachOfCopy(cloneOfItems, (item, index, orgArr) => {
if ('conversations' in item && item.id === conversation.folderId) {
const clonedItem = _.cloneDeep(item);
replaceWithItemIn(clonedItem.conversations);

orgArr[index] = clonedItem;
}
});
} else {
replaceWithItemIn(cloneOfItems);
}

return JSON.parse(
JSON.stringify(cloneOfItems)
);
}

/**
* Creates a deep copy of a value with sorted properties.
*
Expand Down
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Change Log (e.GPT)

## 0.8.0
## 0.8.1

- implement import dialog, which downloads content from a website as additional text into input field

## 0.7.0
## 0.7.3

- reimplement UI, which is inspired by [Chatbot UI](https://github.com/mckaywrigley/chatbot-ui) project
- (bug-)fix and other improvements
Expand Down

0 comments on commit 81ce20f

Please sign in to comment.