2023-02-06 13:27:28 -05:00
|
|
|
import React from 'react';
|
2023-02-06 21:17:46 -05:00
|
|
|
import RenameButton from './RenameButton';
|
|
|
|
|
import DeleteButton from './DeleteButton';
|
2023-02-07 00:05:00 -05:00
|
|
|
import { useSelector, useDispatch } from 'react-redux';
|
2023-02-07 10:26:19 -05:00
|
|
|
import { setConversation } from '~/store/convoSlice';
|
|
|
|
|
import { setMessages } from '~/store/messageSlice';
|
2023-02-08 15:26:42 -05:00
|
|
|
import { setText } from '~/store/textSlice';
|
2023-02-07 16:22:35 -05:00
|
|
|
import manualSWR from '~/utils/fetchers';
|
2023-02-07 00:05:00 -05:00
|
|
|
|
|
|
|
|
export default function Conversation({ id, parentMessageId, title = 'New conversation' }) {
|
|
|
|
|
const dispatch = useDispatch();
|
2023-02-08 15:26:42 -05:00
|
|
|
const { conversationId } = useSelector((state) => state.convo);
|
2023-02-08 11:05:54 -05:00
|
|
|
const { trigger, isMutating } = manualSWR(`http://localhost:3050/messages/${id}`, 'get');
|
2023-02-07 00:05:00 -05:00
|
|
|
|
2023-02-07 19:07:48 -05:00
|
|
|
const clickHandler = async () => {
|
2023-02-08 08:27:23 -05:00
|
|
|
if (conversationId === id) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-07 16:22:35 -05:00
|
|
|
|
2023-02-08 15:26:42 -05:00
|
|
|
dispatch(setConversation({ error: false, conversationId: id, parentMessageId }));
|
2023-02-07 19:07:48 -05:00
|
|
|
const data = await trigger();
|
|
|
|
|
dispatch(setMessages(data));
|
2023-02-08 15:26:42 -05:00
|
|
|
dispatch(setText(''));
|
2023-02-07 00:05:00 -05:00
|
|
|
};
|
2023-02-06 13:27:28 -05:00
|
|
|
|
2023-02-08 11:05:54 -05:00
|
|
|
const aProps = {
|
|
|
|
|
className:
|
|
|
|
|
'animate-flash group relative flex cursor-pointer items-center gap-3 break-all rounded-md bg-gray-800 py-3 px-3 pr-14 hover:bg-gray-800'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (conversationId !== id) {
|
|
|
|
|
aProps.className =
|
|
|
|
|
'group relative flex cursor-pointer items-center gap-3 break-all rounded-md py-3 px-3 hover:bg-[#2A2B32] hover:pr-4';
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 13:27:28 -05:00
|
|
|
return (
|
2023-02-06 18:25:11 -05:00
|
|
|
<a
|
2023-02-07 16:22:35 -05:00
|
|
|
onClick={() => clickHandler()}
|
2023-02-08 11:05:54 -05:00
|
|
|
{...aProps}
|
2023-02-06 18:25:11 -05:00
|
|
|
>
|
2023-02-06 13:27:28 -05:00
|
|
|
<svg
|
|
|
|
|
stroke="currentColor"
|
|
|
|
|
fill="none"
|
|
|
|
|
strokeWidth="2"
|
|
|
|
|
viewBox="0 0 24 24"
|
|
|
|
|
strokeLinecap="round"
|
|
|
|
|
strokeLinejoin="round"
|
|
|
|
|
className="h-4 w-4"
|
|
|
|
|
height="1em"
|
|
|
|
|
width="1em"
|
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
|
>
|
2023-02-06 16:00:59 -05:00
|
|
|
<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z" />
|
2023-02-06 13:27:28 -05:00
|
|
|
</svg>
|
|
|
|
|
<div className="relative max-h-5 flex-1 overflow-hidden text-ellipsis break-all">
|
|
|
|
|
{title}
|
|
|
|
|
</div>
|
2023-02-08 15:26:42 -05:00
|
|
|
{conversationId === id ? (
|
2023-02-08 11:05:54 -05:00
|
|
|
<div className="visible absolute right-1 z-10 flex text-gray-300">
|
|
|
|
|
<RenameButton conversationId={id} />
|
|
|
|
|
<DeleteButton conversationId={id} />
|
|
|
|
|
</div>
|
2023-02-08 15:26:42 -05:00
|
|
|
) : (
|
|
|
|
|
<div className="absolute inset-y-0 right-0 z-10 w-8 bg-gradient-to-l from-gray-900 group-hover:from-[#2A2B32]" />
|
2023-02-08 11:05:54 -05:00
|
|
|
)}
|
2023-02-06 13:27:28 -05:00
|
|
|
</a>
|
|
|
|
|
);
|
|
|
|
|
}
|