feat: change tab title to current chat title

This commit is contained in:
Jing Hua 2023-03-10 15:09:23 +08:00
parent f58382137d
commit bdc385158b

View file

@ -11,12 +11,22 @@ import CrossIcon from '@icon/CrossIcon';
import useInitialiseNewChat from '@hooks/useInitialiseNewChat';
const ChatHistoryList = () => {
const setCurrentChatIndex = useStore((state) => state.setCurrentChatIndex);
const currentChatIndex = useStore((state) => state.currentChatIndex);
const chatTitles = useStore(
(state) => state.chats?.map((chat) => chat.title),
shallow
);
useEffect(() => {
if (
chatTitles &&
currentChatIndex >= 0 &&
currentChatIndex < chatTitles.length
) {
document.title = chatTitles[currentChatIndex];
}
}, [currentChatIndex, chatTitles]);
return (
<div className='flex-col flex-1 overflow-y-auto border-b border-white/20'>
<div className='flex flex-col gap-2 text-gray-100 text-sm'>
@ -26,9 +36,6 @@ const ChatHistoryList = () => {
title={title}
key={`${title}-${index}`}
chatIndex={index}
onClick={() => {
setCurrentChatIndex(index);
}}
/>
))}
{/* <ShowMoreButton /> */}
@ -56,30 +63,23 @@ const ChatHistoryClass = {
'absolute inset-y-0 right-0 w-8 z-10 bg-gradient-to-l from-gray-800',
};
const ChatHistory = ({
title,
chatIndex,
onClick,
}: {
title: string;
chatIndex: number;
onClick?: React.MouseEventHandler<HTMLElement>;
}) => {
const ChatHistory = React.memo(
({ title, chatIndex }: { title: string; chatIndex: number }) => {
const initialiseNewChat = useInitialiseNewChat();
const setCurrentChatIndex = useStore((state) => state.setCurrentChatIndex);
const setChats = useStore((state) => state.setChats);
const currentChatIndex = useStore((state) => state.currentChatIndex);
const active = useStore((state) => state.currentChatIndex === chatIndex);
const [isDelete, setIsDelete] = useState<boolean>(false);
const [isEdit, setIsEdit] = useState<boolean>(false);
const [_title, _setTitle] = useState<string>(title);
const inputRef = useRef<HTMLInputElement>(null);
const active = currentChatIndex === chatIndex;
const handleTick = (e: React.MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();
const updatedChats = JSON.parse(JSON.stringify(useStore.getState().chats));
const updatedChats = JSON.parse(
JSON.stringify(useStore.getState().chats)
);
if (isEdit) {
updatedChats[chatIndex].title = _title;
setChats(updatedChats);
@ -108,7 +108,9 @@ const ChatHistory = ({
return (
<a
className={active ? ChatHistoryClass.active : ChatHistoryClass.normal}
onClick={(e) => onClick && onClick(e)}
onClick={(e) => {
setCurrentChatIndex(chatIndex);
}}
>
<ChatIcon />
<div className='flex-1 text-ellipsis max-h-5 overflow-hidden break-all relative'>
@ -167,6 +169,7 @@ const ChatHistory = ({
)}
</a>
);
};
}
);
export default ChatHistoryList;