2023-07-17 20:31:01 +08:00
|
|
|
import React from "react";
|
2023-07-17 20:31:01 +08:00
|
|
|
import { Text, Flex, Avatar, ActionIcon, Tooltip, CopyButton, SimpleGrid } from "@mantine/core";
|
2023-07-17 20:31:01 +08:00
|
|
|
|
|
|
|
// @ts-ignore
|
2023-08-03 17:16:15 +08:00
|
|
|
import SvgAvatarDevChat from './avatar_devchat.svg';
|
2023-07-17 20:31:01 +08:00
|
|
|
// @ts-ignore
|
2023-08-03 17:01:12 +08:00
|
|
|
import SvgAvatarUser from './avatar_spaceman.png';
|
2023-07-17 20:31:01 +08:00
|
|
|
import { IconCheck, IconCopy, Icon360, IconEdit, IconTrash } from "@tabler/icons-react";
|
2023-08-18 00:24:10 +08:00
|
|
|
import { observer } from "mobx-react-lite";
|
|
|
|
import { useMst } from "@/views/stores/RootStore";
|
2023-07-17 20:31:01 +08:00
|
|
|
|
2023-08-18 11:43:26 +08:00
|
|
|
import { IMessage } from "@/views/stores/ChatStore";
|
2023-07-17 20:31:01 +08:00
|
|
|
|
2023-08-18 11:43:26 +08:00
|
|
|
interface IProps {
|
|
|
|
item: IMessage,
|
2023-08-18 16:21:51 +08:00
|
|
|
showEdit?: boolean,
|
2023-08-18 11:43:26 +08:00
|
|
|
showDelete: boolean
|
|
|
|
}
|
|
|
|
|
2023-08-21 13:56:42 +08:00
|
|
|
const MessageAvatar = observer((props: IProps) => {
|
2023-07-17 21:07:42 +08:00
|
|
|
const { item, showEdit = false, showDelete = true } = props;
|
|
|
|
const { contexts, message, type, hash } = item;
|
2023-08-18 00:24:10 +08:00
|
|
|
const { input, chat } = useMst();
|
2023-07-17 20:31:01 +08:00
|
|
|
const [done, setDone] = React.useState(false);
|
|
|
|
return (<Flex
|
|
|
|
m='10px 0 10px 0'
|
|
|
|
gap="sm"
|
|
|
|
justify="flex-start"
|
|
|
|
align="center"
|
|
|
|
direction="row"
|
|
|
|
wrap="wrap">
|
|
|
|
{
|
|
|
|
type === 'bot'
|
|
|
|
? <Avatar
|
|
|
|
color="indigo"
|
|
|
|
size={25}
|
|
|
|
radius="xl"
|
|
|
|
src={SvgAvatarDevChat} />
|
|
|
|
: <Avatar
|
|
|
|
color="cyan"
|
|
|
|
size={25}
|
|
|
|
radius="xl"
|
|
|
|
src={SvgAvatarUser} />
|
|
|
|
}
|
|
|
|
<Text weight='bold'>{type === 'bot' ? 'DevChat' : 'User'}</Text>
|
|
|
|
{type === 'user'
|
2023-07-17 20:31:01 +08:00
|
|
|
? <Flex
|
|
|
|
gap="xs"
|
|
|
|
justify="flex-end"
|
|
|
|
align="center"
|
|
|
|
direction="row"
|
|
|
|
wrap="wrap"
|
2023-08-04 11:07:55 +08:00
|
|
|
style={{ marginLeft: 'auto', marginRight: '10px' }}>
|
2023-07-17 20:31:01 +08:00
|
|
|
<Tooltip sx={{ padding: '3px', fontSize: 'var(--vscode-editor-font-size)' }} label={done ? 'Refilled' : 'Refill prompt'} withArrow position="left" color="gray">
|
|
|
|
<ActionIcon size='sm'
|
|
|
|
onClick={() => {
|
2023-08-18 00:24:10 +08:00
|
|
|
input.setValue(message);
|
|
|
|
input.setContexts(contexts);
|
2023-07-17 20:31:01 +08:00
|
|
|
setDone(true);
|
|
|
|
setTimeout(() => { setDone(false); }, 2000);
|
|
|
|
}}>
|
|
|
|
{done ? <IconCheck size="1rem" /> : <Icon360 size="1.125rem" />}
|
|
|
|
</ActionIcon>
|
|
|
|
</Tooltip>
|
|
|
|
{showEdit && <Tooltip sx={{ padding: '3px', fontSize: 'var(--vscode-editor-font-size)' }} label="Edit message" withArrow position="left" color="gray">
|
|
|
|
<ActionIcon size='sm'
|
|
|
|
onClick={() => {
|
|
|
|
|
|
|
|
}}>
|
|
|
|
<IconEdit size="1.125rem" />
|
|
|
|
</ActionIcon>
|
|
|
|
</Tooltip >}
|
2023-07-19 16:03:39 +08:00
|
|
|
{showDelete && hash !== 'message' && <Tooltip sx={{ padding: '3px', fontSize: 'var(--vscode-editor-font-size)' }} label="Delete message" withArrow position="left" color="gray">
|
2023-07-17 20:31:01 +08:00
|
|
|
<ActionIcon size='sm'
|
|
|
|
onClick={() => {
|
2023-07-19 16:03:39 +08:00
|
|
|
if (item.hash) {
|
2023-08-18 00:29:03 +08:00
|
|
|
chat.deleteMessage(item).then();
|
2023-07-19 16:03:39 +08:00
|
|
|
} else {
|
2023-08-18 00:24:10 +08:00
|
|
|
chat.popMessage();
|
|
|
|
chat.popMessage();
|
2023-07-19 16:03:39 +08:00
|
|
|
}
|
2023-07-17 20:31:01 +08:00
|
|
|
}}>
|
|
|
|
<IconTrash size="1.125rem" />
|
|
|
|
</ActionIcon>
|
|
|
|
</Tooltip >}
|
2023-07-17 20:31:01 +08:00
|
|
|
</Flex >
|
2023-07-17 20:31:01 +08:00
|
|
|
: <CopyButton value={message} timeout={2000}>
|
|
|
|
{({ copied, copy }) => (
|
|
|
|
<Tooltip sx={{ padding: '3px', fontSize: 'var(--vscode-editor-font-size)' }} label={copied ? 'Copied' : 'Copy message'} withArrow position="left" color="gray">
|
2023-08-04 11:07:55 +08:00
|
|
|
<ActionIcon size='xs' color={copied ? 'teal' : 'gray'} onClick={copy} style={{ marginLeft: 'auto', marginRight: '10px' }}>
|
2023-07-17 20:31:01 +08:00
|
|
|
{copied ? <IconCheck size="1rem" /> : <IconCopy size="1rem" />}
|
|
|
|
</ActionIcon>
|
|
|
|
</Tooltip>
|
|
|
|
)}
|
|
|
|
</CopyButton>
|
|
|
|
}
|
|
|
|
</Flex >);
|
2023-08-18 00:24:10 +08:00
|
|
|
});
|
2023-07-17 20:31:01 +08:00
|
|
|
|
2023-08-21 13:56:42 +08:00
|
|
|
export default MessageAvatar;
|