Merge pull request #337 from RecentRunner/web-lastfixes
web last fixes
This commit was merged in pull request #337.
This commit is contained in:
@@ -8,10 +8,12 @@ import { createStompClient } from "@/lib/chatSocket";
|
||||
|
||||
const API_BASE = "";
|
||||
|
||||
//Checks if a filename looks like an image based on its extension
|
||||
function isImageFilename(name) {
|
||||
return /\.(jpe?g|png|gif|webp|bmp|svg)$/i.test(name || "");
|
||||
}
|
||||
|
||||
//Shows an image inline or a download link for other file types
|
||||
function AttachmentPreview({ url, name, token }) {
|
||||
const [blobUrl, setBlobUrl] = useState(null);
|
||||
const isImage = isImageFilename(name);
|
||||
@@ -68,6 +70,7 @@ function AttachmentPreview({ url, name, token }) {
|
||||
);
|
||||
}
|
||||
|
||||
//Live support chat page with a conversation sidebar and real-time messaging via WebSocket
|
||||
function ChatPage() {
|
||||
const { user, token, loading: authLoading } = useAuth();
|
||||
const router = useRouter();
|
||||
@@ -113,6 +116,7 @@ function ChatPage() {
|
||||
if (nearBottom) area.scrollTop = area.scrollHeight;
|
||||
}, [messages]);
|
||||
|
||||
//Loads all messages for a conversation and scrolls to the bottom
|
||||
const fetchMessages = useCallback(async (convId) => {
|
||||
if (!token || !convId) return;
|
||||
initialLoadDoneRef.current = false;
|
||||
@@ -145,6 +149,7 @@ function ChatPage() {
|
||||
}
|
||||
}, [token]);
|
||||
|
||||
//Fetches a single conversation's details and stores it in state
|
||||
const fetchConversation = useCallback(async (convId) => {
|
||||
if (!token || !convId) return null;
|
||||
try {
|
||||
@@ -183,6 +188,7 @@ function ChatPage() {
|
||||
}
|
||||
}, [token]);
|
||||
|
||||
//Connects the WebSocket and subscribes to new messages and conversation updates
|
||||
const connectStomp = useCallback((convId) => {
|
||||
if (stompRef.current) {
|
||||
stompRef.current.deactivate();
|
||||
@@ -268,6 +274,7 @@ function ChatPage() {
|
||||
};
|
||||
}, [token, authLoading, conversationIdParam, fetchConversation, fetchMessages, connectStomp, fetchConversations]);
|
||||
|
||||
//Decides whether to send a text message or an attachment
|
||||
async function handleSend(e) {
|
||||
e?.preventDefault();
|
||||
const text = input.trim();
|
||||
@@ -279,6 +286,7 @@ function ChatPage() {
|
||||
}
|
||||
}
|
||||
|
||||
//Sends a plain text message to the support agent
|
||||
async function handleSendText(text) {
|
||||
setInput("");
|
||||
setSending(true);
|
||||
@@ -321,6 +329,7 @@ function ChatPage() {
|
||||
}
|
||||
}
|
||||
|
||||
//Uploads a file as an attachment, with an optional caption
|
||||
async function handleSendAttachment(optionalText) {
|
||||
setSending(true);
|
||||
setError(null);
|
||||
@@ -381,6 +390,7 @@ function ChatPage() {
|
||||
}
|
||||
}
|
||||
|
||||
//Creates a new live support conversation and requests a human agent straight away
|
||||
async function handleNewConversation() {
|
||||
setError(null);
|
||||
setLoadingConv(true);
|
||||
@@ -429,6 +439,7 @@ function ChatPage() {
|
||||
router.replace(`/chat?id=${convId}`, { scroll: false });
|
||||
}
|
||||
|
||||
//Closes the current conversation so no more messages can be sent
|
||||
async function handleCloseConversation() {
|
||||
if (!conversation || conversation.status === "CLOSED") return;
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user