Fix rect get wrong and optimize logic

This commit is contained in:
fengzai6 2024-08-16 23:20:20 +08:00
parent 0416c449a5
commit 4e280ca1b5

View File

@ -256,7 +256,7 @@ function ChatMessageActions(props: {
index: number; index: number;
message: ChatMessage; message: ChatMessage;
position: { x: number; y: number }; position: { x: number; y: number };
isHover: boolean; hoveringMessage: { hover: boolean; index: number };
onUserStop: (messageId: string) => void; onUserStop: (messageId: string) => void;
onResend: (message: ChatMessage) => void; onResend: (message: ChatMessage) => void;
onDelete: (messageId: string) => void; onDelete: (messageId: string) => void;
@ -266,7 +266,7 @@ function ChatMessageActions(props: {
index, index,
message, message,
position, position,
isHover, hoveringMessage,
onUserStop, onUserStop,
onResend, onResend,
onDelete, onDelete,
@ -275,39 +275,28 @@ function ChatMessageActions(props: {
const actionsRef = useRef<HTMLDivElement>(null); const actionsRef = useRef<HTMLDivElement>(null);
const [rect, setRect] = useState<DOMRect>({
x: 0,
y: 0,
width: 0,
height: 0,
top: 0,
right: 0,
bottom: 0,
left: 0,
toJSON: () => ({}),
});
const isUserMessage = message.role === "user"; const isUserMessage = message.role === "user";
const [translate, setTranslate] = useState({ x: 0, y: 0 }); const [translate, setTranslate] = useState({ x: 0, y: 0 });
useEffect(() => { useEffect(() => {
if (position.x && position.y && isHover) { const rect = actionsRef.current?.getBoundingClientRect();
if (
rect &&
position.x &&
position.y &&
hoveringMessage.hover &&
hoveringMessage.index === index
) {
const x = position.x - rect.x - (isUserMessage ? rect.width : 0); const x = position.x - rect.x - (isUserMessage ? rect.width : 0);
const y = position.y - rect.y - (isUserMessage ? rect.height : 0); const y = position.y - rect.y - rect.height;
setTranslate({ x, y }); setTranslate({ x, y });
} else { } else {
setTranslate({ x: 0, y: 0 }); setTranslate({ x: 0, y: 0 });
} }
}, [position, isHover]); }, [position, hoveringMessage.index]);
useEffect(() => {
const div = actionsRef.current;
if (div) {
const rect = div.getBoundingClientRect();
setRect(rect);
}
}, []);
return ( return (
<div ref={actionsRef} className={styles["chat-message-actions"]}> <div ref={actionsRef} className={styles["chat-message-actions"]}>
@ -958,8 +947,13 @@ function _Chat() {
const [attachImages, setAttachImages] = useState<string[]>([]); const [attachImages, setAttachImages] = useState<string[]>([]);
const [uploading, setUploading] = useState(false); const [uploading, setUploading] = useState(false);
const [isMessageContainerHover, setIsMessageContainerHover] = const [hoveringMessage, setHoveringMessage] = useState<{
useState<boolean>(false); hover: boolean;
index: number;
}>({
hover: false,
index: -1,
});
const [actionsPosition, setActionsPosition] = useState<{ const [actionsPosition, setActionsPosition] = useState<{
x: number; x: number;
y: number; y: number;
@ -1132,12 +1126,10 @@ function _Chat() {
}); });
} }
}; };
// handle mouse hover to reset actions position // handle change msg hover to reset actions position
useEffect(() => { useEffect(() => {
if (!isMessageContainerHover) { setActionsPosition({ x: 0, y: 0 });
setActionsPosition({ x: 0, y: 0 }); }, [hoveringMessage.index]);
}
}, [isMessageContainerHover]);
const deleteMessage = (msgId?: string) => { const deleteMessage = (msgId?: string) => {
chatStore.updateCurrentSession( chatStore.updateCurrentSession(
@ -1591,8 +1583,12 @@ function _Chat() {
onClick={() => { onClick={() => {
setActionsPosition({ x: 0, y: 0 }); setActionsPosition({ x: 0, y: 0 });
}} }}
onMouseEnter={() => setIsMessageContainerHover(true)} onMouseEnter={() =>
onMouseLeave={() => setIsMessageContainerHover(false)} setHoveringMessage({ hover: true, index: i })
}
onMouseLeave={() =>
setHoveringMessage({ hover: false, index: -1 })
}
> >
<div className={styles["chat-message-header"]}> <div className={styles["chat-message-header"]}>
<div className={styles["chat-message-avatar"]}> <div className={styles["chat-message-avatar"]}>
@ -1659,7 +1655,7 @@ function _Chat() {
index={i} index={i}
message={message} message={message}
position={actionsPosition} position={actionsPosition}
isHover={isMessageContainerHover} hoveringMessage={hoveringMessage}
onUserStop={onUserStop} onUserStop={onUserStop}
onResend={onResend} onResend={onResend}
onDelete={onDelete} onDelete={onDelete}