fix: loading messages
This commit is contained in:
parent
76f72aa9d3
commit
be6949022f
@ -17,7 +17,6 @@ import {
|
|||||||
useTicketMessages,
|
useTicketMessages,
|
||||||
getMessageFromFetchError,
|
getMessageFromFetchError,
|
||||||
useSSESubscription,
|
useSSESubscription,
|
||||||
useEventListener,
|
|
||||||
createTicket,
|
createTicket,
|
||||||
} from "@frontend/kitui";
|
} from "@frontend/kitui";
|
||||||
import { enqueueSnackbar } from "notistack";
|
import { enqueueSnackbar } from "notistack";
|
||||||
@ -40,7 +39,7 @@ import {
|
|||||||
addOrUpdateUnauthMessages,
|
addOrUpdateUnauthMessages,
|
||||||
setUnauthTicketMessageFetchState,
|
setUnauthTicketMessageFetchState,
|
||||||
setUnauthIsPreventAutoscroll,
|
setUnauthIsPreventAutoscroll,
|
||||||
incrementUnauthMessageApiPage,
|
incrementUnauthMessage,
|
||||||
setIsMessageSending,
|
setIsMessageSending,
|
||||||
} from "@root/stores/tickets";
|
} from "@root/stores/tickets";
|
||||||
import { useUserStore } from "@root/stores/user";
|
import { useUserStore } from "@root/stores/user";
|
||||||
@ -49,6 +48,8 @@ import ChatDocument from "./ChatDocument";
|
|||||||
import ChatImage from "./ChatImage";
|
import ChatImage from "./ChatImage";
|
||||||
import ChatVideo from "./ChatVideo";
|
import ChatVideo from "./ChatVideo";
|
||||||
|
|
||||||
|
import type { WheelEvent, TouchEvent } from "react";
|
||||||
|
|
||||||
type ModalWarningType =
|
type ModalWarningType =
|
||||||
| "errorType"
|
| "errorType"
|
||||||
| "errorSize"
|
| "errorSize"
|
||||||
@ -171,14 +172,12 @@ export default function Chat({ open = false, onclickArrow, sx }: Props) {
|
|||||||
if (fetchState !== "idle") return;
|
if (fetchState !== "idle") return;
|
||||||
|
|
||||||
if (chatBox.scrollTop < chatBox.clientHeight) {
|
if (chatBox.scrollTop < chatBox.clientHeight) {
|
||||||
incrementUnauthMessageApiPage();
|
incrementUnauthMessage();
|
||||||
}
|
}
|
||||||
}, 200),
|
}, 200),
|
||||||
[fetchState]
|
[fetchState]
|
||||||
);
|
);
|
||||||
|
|
||||||
useEventListener("scroll", throttledScrollHandler, chatBoxRef);
|
|
||||||
|
|
||||||
useEffect(
|
useEffect(
|
||||||
function scrollOnNewMessage() {
|
function scrollOnNewMessage() {
|
||||||
if (!chatBoxRef.current) return;
|
if (!chatBoxRef.current) return;
|
||||||
@ -202,6 +201,14 @@ export default function Chat({ open = false, onclickArrow, sx }: Props) {
|
|||||||
}
|
}
|
||||||
}, [open, messages]);
|
}, [open, messages]);
|
||||||
|
|
||||||
|
const loadNewMessages = (
|
||||||
|
event: WheelEvent<HTMLDivElement> | TouchEvent<HTMLDivElement>
|
||||||
|
) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
|
||||||
|
throttledScrollHandler();
|
||||||
|
};
|
||||||
|
|
||||||
async function handleSendMessage() {
|
async function handleSendMessage() {
|
||||||
if (!messageField || isMessageSending) return;
|
if (!messageField || isMessageSending) return;
|
||||||
|
|
||||||
@ -386,6 +393,8 @@ export default function Chat({ open = false, onclickArrow, sx }: Props) {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Box
|
<Box
|
||||||
|
onWheel={loadNewMessages}
|
||||||
|
onTouchMove={loadNewMessages}
|
||||||
ref={chatBoxRef}
|
ref={chatBoxRef}
|
||||||
sx={{
|
sx={{
|
||||||
display: "flex",
|
display: "flex",
|
||||||
|
@ -169,11 +169,10 @@ export const setUnauthIsPreventAutoscroll = (isPreventAutoscroll: boolean) =>
|
|||||||
ticket.isPreventAutoscroll = isPreventAutoscroll;
|
ticket.isPreventAutoscroll = isPreventAutoscroll;
|
||||||
});
|
});
|
||||||
|
|
||||||
export const incrementUnauthMessageApiPage = () => {
|
export const incrementUnauthMessage = () =>
|
||||||
const state = useTicketStore.getState();
|
updateTicket((ticket) => {
|
||||||
|
ticket.apiPage++;
|
||||||
useTicketStore.setState({ apiPage: state.apiPage + 1 });
|
});
|
||||||
};
|
|
||||||
|
|
||||||
export const setIsMessageSending = (
|
export const setIsMessageSending = (
|
||||||
isMessageSending: AuthData["isMessageSending"]
|
isMessageSending: AuthData["isMessageSending"]
|
||||||
|
Loading…
Reference in New Issue
Block a user