Skip to content

Commit

Permalink
Merge branch 'ztjhz:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Limour-dev authored Mar 28, 2023
2 parents 627ceb9 + 325da54 commit 9f84db7
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 39 deletions.
16 changes: 16 additions & 0 deletions src/components/ImportExportChat/ImportExportChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const ImportExportChat = () => {
const ImportChat = () => {
const { t } = useTranslation();
const setChats = useStore.getState().setChats;
const setFoldersName = useStore.getState().setFoldersName;
const setFoldersExpanded = useStore.getState().setFoldersExpanded;
const inputRef = useRef<HTMLInputElement>(null);
const [alert, setAlert] = useState<{
message: string;
Expand All @@ -62,6 +64,20 @@ const ImportChat = () => {
try {
const parsedData = JSON.parse(data);
if (validateAndFixChats(parsedData)) {
const parsedFolders: string[] = [];
parsedData.forEach((data) => {
if (data.folder && !parsedFolders.includes(data.folder))
parsedFolders.push(data.folder);
});
setFoldersName([
...parsedFolders,
...useStore.getState().foldersName,
]);
setFoldersExpanded([
...new Array(parsedFolders.length).fill(false),
...useStore.getState().foldersExpanded,
]);

const prevChats = useStore.getState().chats;
if (prevChats) {
const updatedChats: ChatInterface[] = JSON.parse(
Expand Down
68 changes: 44 additions & 24 deletions src/components/Menu/ChatFolder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,42 +30,61 @@ const ChatFolder = ({
const [_folderName, _setFolderName] = useState<string>(folderName);
const [isEdit, setIsEdit] = useState<boolean>(false);
const [isDelete, setIsDelete] = useState<boolean>(false);
// const [isExpanded, setIsExpanded] = useState<boolean>(
// useStore.getState().foldersExpanded[folderIndex]
// );
const [isHover, setIsHover] = useState<boolean>(false);

const handleTick = (e: React.MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();
const editTitle = () => {
const updatedChats: ChatInterface[] = JSON.parse(
JSON.stringify(useStore.getState().chats)
);

if (isEdit) {
updatedChats.forEach((chat) => {
if (chat.folder === folderName) chat.folder = _folderName;
});
setChats(updatedChats);
updatedChats.forEach((chat) => {
if (chat.folder === folderName) chat.folder = _folderName;
});
setChats(updatedChats);

const updatedFolderNames = [...useStore.getState().foldersName];
const pos = updatedFolderNames.indexOf(folderName);
if (pos !== -1) updatedFolderNames[pos] = _folderName;
setFoldersName(updatedFolderNames);
const updatedFolderNames = [...useStore.getState().foldersName];
const pos = updatedFolderNames.indexOf(folderName);
if (pos !== -1) updatedFolderNames[pos] = _folderName;
setFoldersName(updatedFolderNames);

setIsEdit(false);
} else if (isDelete) {
updatedChats.forEach((chat) => {
if (chat.folder === folderName) delete chat.folder;
});
setChats(updatedChats);
setIsEdit(false);
};

setFoldersName(
useStore.getState().foldersName.filter((name) => name !== folderName)
);
setIsDelete(false);
const deleteFolder = () => {
const updatedChats: ChatInterface[] = JSON.parse(
JSON.stringify(useStore.getState().chats)
);
updatedChats.forEach((chat) => {
if (chat.folder === folderName) delete chat.folder;
});
setChats(updatedChats);

const updatedFoldersName = [...useStore.getState().foldersName];
const updatedFoldersExpanded = [...useStore.getState().foldersExpanded];

const i = updatedFoldersName.findIndex((name) => name === folderName);
updatedFoldersName.splice(i, 1);
updatedFoldersExpanded.splice(i, 1);

setFoldersName(updatedFoldersName);
setFoldersExpanded(updatedFoldersExpanded);
setIsDelete(false);
};

const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') {
e.preventDefault();
editTitle();
}
};

const handleTick = (e: React.MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();

if (isEdit) editTitle();
else if (isDelete) deleteFolder();
};

const handleCross = () => {
setIsDelete(false);
setIsEdit(false);
Expand Down Expand Up @@ -127,6 +146,7 @@ const ChatFolder = ({
_setFolderName(e.target.value);
}}
onClick={(e) => e.stopPropagation()}
onKeyDown={handleKeyDown}
ref={inputRef}
/>
) : (
Expand Down
45 changes: 31 additions & 14 deletions src/components/Menu/ChatHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,41 @@ const ChatHistory = React.memo(
const [_title, _setTitle] = useState<string>(title);
const inputRef = useRef<HTMLInputElement>(null);

const handleTick = (e: React.MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();
const editTitle = () => {
const updatedChats = JSON.parse(
JSON.stringify(useStore.getState().chats)
);
if (isEdit) {
updatedChats[chatIndex].title = _title;
updatedChats[chatIndex].title = _title;
setChats(updatedChats);
setIsEdit(false);
};

const deleteChat = () => {
const updatedChats = JSON.parse(
JSON.stringify(useStore.getState().chats)
);
updatedChats.splice(chatIndex, 1);
if (updatedChats.length > 0) {
setCurrentChatIndex(0);
setChats(updatedChats);
setIsEdit(false);
} else if (isDelete) {
updatedChats.splice(chatIndex, 1);
if (updatedChats.length > 0) {
setCurrentChatIndex(0);
setChats(updatedChats);
} else {
initialiseNewChat();
}
setIsDelete(false);
} else {
initialiseNewChat();
}
setIsDelete(false);
};

const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') {
e.preventDefault();
editTitle();
}
};

const handleTick = (e: React.MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();

if (isEdit) editTitle();
else if (isDelete) deleteChat();
};

const handleCross = () => {
Expand Down Expand Up @@ -94,6 +110,7 @@ const ChatHistory = React.memo(
onChange={(e) => {
_setTitle(e.target.value);
}}
onKeyDown={handleKeyDown}
ref={inputRef}
/>
) : (
Expand Down
5 changes: 5 additions & 0 deletions src/components/Menu/MenuOptions/ClearConversation.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import useStore from '@store/store';

import PopupModal from '@components/PopupModal';
import DeleteIcon from '@icon/DeleteIcon';
Expand All @@ -9,10 +10,14 @@ const ClearConversation = () => {
const { t } = useTranslation();

const initialiseNewChat = useInitialiseNewChat();
const setFoldersName = useStore((state) => state.setFoldersName);
const setFoldersExpanded = useStore((state) => state.setFoldersExpanded);
const [isModalOpen, setIsModalOpen] = useState<boolean>(false);

const handleConfirm = () => {
setIsModalOpen(false);
setFoldersName([]);
setFoldersExpanded([]);
initialiseNewChat();
};

Expand Down
2 changes: 2 additions & 0 deletions src/components/Menu/NewFolder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const NewFolder = () => {
const { t } = useTranslation();
const generating = useStore((state) => state.generating);
const setFoldersName = useStore((state) => state.setFoldersName);
const setFoldersExpanded = useStore((state) => state.setFoldersExpanded);

const addFolder = () => {
let folderIndex = 1;
Expand All @@ -23,6 +24,7 @@ const NewFolder = () => {
}

setFoldersName([name, ...useStore.getState().foldersName]);
setFoldersExpanded([false, ...useStore.getState().foldersExpanded]);
};

return (
Expand Down
9 changes: 8 additions & 1 deletion src/utils/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import html2canvas from 'html2canvas';
import { ChatInterface, ConfigInterface, MessageInterface } from '@type/chat';
import { roles } from '@type/chat';
import { Theme } from '@type/theme';
import { _defaultChatConfig } from '@constants/chat';
import {
defaultModel,
modelOptions,
_defaultChatConfig,
} from '@constants/chat';

export const validateAndFixChats = (chats: any): chats is ChatInterface[] => {
if (!Array.isArray(chats)) return false;
Expand Down Expand Up @@ -49,6 +53,9 @@ const validateAndFixChatConfig = (config: ConfigInterface) => {
config.frequency_penalty = _defaultChatConfig.frequency_penalty;
if (!(typeof config.frequency_penalty === 'number')) return false;

if (!config.model) config.model = defaultModel;
if (!modelOptions.includes(config.model)) return false;

return true;
};

Expand Down

0 comments on commit 9f84db7

Please sign in to comment.