Commit e39747be by chenway

update

parent b85c93fd
Pipeline #158870 passed with stages
in 1 minute 31 seconds
...@@ -2,6 +2,7 @@ import React from 'react'; ...@@ -2,6 +2,7 @@ import React from 'react';
import { Route, Routes } from "react-router-dom"; import { Route, Routes } from "react-router-dom";
import ChatTrans from './Components/ChatTrans'; import ChatTrans from './Components/ChatTrans';
import Kodi from './Components/Kodi'; import Kodi from './Components/Kodi';
import Test from './Components/Test';
function App() { function App() {
return ( return (
...@@ -9,6 +10,7 @@ function App() { ...@@ -9,6 +10,7 @@ function App() {
<Route path="/" element={<Kodi />} /> <Route path="/" element={<Kodi />} />
<Route path="/web" element={<Kodi />} /> <Route path="/web" element={<Kodi />} />
<Route path="/chat" element={<ChatTrans />} /> <Route path="/chat" element={<ChatTrans />} />
<Route path="/test" element={<Test />} />
</Routes> </Routes>
); );
} }
......
// import Chat, { Bubble, useMessages } from '@chatui/core';
// import '@chatui/core/dist/index.css';
import { useState } from "react"; import { useState } from "react";
import { import {
Button, Button,
...@@ -7,15 +5,15 @@ import { ...@@ -7,15 +5,15 @@ import {
Container, Container,
Form, Form,
FormLabel, FormLabel,
Navbar,
Row, Row,
ListGroup ListGroup,
} from "react-bootstrap"; } from "react-bootstrap";
import io from "socket.io-client"; import io from "socket.io-client";
import "../App.css"; import "../App.css";
import React, { useEffect } from "react";
const url = 'https://staging.strk-ai-support.ngrok.app/' const url = 'https://staging.strk-ai-support.ngrok.app/'
// const url = 'http://localhost:8000' // const url = "http://localhost:8000";
const socket = io(url, { const socket = io(url, {
extraHeaders: { extraHeaders: {
...@@ -23,10 +21,6 @@ const socket = io(url, { ...@@ -23,10 +21,6 @@ const socket = io(url, {
}, },
}); });
socket.on("connect", (data) => {
console.log(data);
});
function ChatTrans() { function ChatTrans() {
const [languageSupport, setLanguageSupport] = useState("English"); const [languageSupport, setLanguageSupport] = useState("English");
const [languageUser, setLanguageUser] = useState("Simple Chinese"); const [languageUser, setLanguageUser] = useState("Simple Chinese");
...@@ -37,7 +31,42 @@ function ChatTrans() { ...@@ -37,7 +31,42 @@ function ChatTrans() {
const [userMessages, setUserMessages] = useState([]); const [userMessages, setUserMessages] = useState([]);
const [supportMessages, setSupportMessages] = useState([]); const [supportMessages, setSupportMessages] = useState([]);
const [room, setRoom] = useState("");
const generateToken = () => {
return Math.random().toString(36).substr(2, 9);
};
useEffect(() => {
// 检查URL是否已经有token
const currentUrl = new URL(window.location.href);
if (!currentUrl.searchParams.get("token")) {
// 生成新的token
const token = generateToken();
// 更新URL
currentUrl.searchParams.set("token", token);
window.history.replaceState({}, "", currentUrl.toString());
// 保存token到sessionStorage
sessionStorage.setItem("sessionToken", token);
}
console.log(currentUrl.searchParams.get("token"));
setRoom(currentUrl.searchParams.get("token"));
socket.emit("join", {
room: currentUrl.searchParams.get("token"),
});
if (userMessages.length === 0 && supportMessages.length === 0) {
socket.emit("history", {
room: currentUrl.searchParams.get("token"),
});
}
}, []);
socket.on("send msg", function (data) { socket.on("send msg", function (data) {
let messge = data.user + ": " + data.message; let messge = data.user + ": " + data.message;
...@@ -48,87 +77,76 @@ function ChatTrans() { ...@@ -48,87 +77,76 @@ function ChatTrans() {
} }
}); });
const sendUserMessage = (event, user) => { socket.on("history", function (data) {
if (userMessage) { let user = data.user;
socket.emit("send msg", { let messages = data.messages;
user: "user",
message: userMessage, if (user === "user") {
languageSupport: languageSupport, setUserMessages(messages);
languageUser: languageUser,
});
setUserMessage("");
setUserMessages([...userMessages, "user: " +userMessage]);
} else { } else {
alert("Message cannot be empty"); setSupportMessages(messages);
} }
}; });
const sendSupportMessage = (event, user) => { const sendMessage = (
if (supportMessage) { event,
user,
message,
messages,
setMessage,
setMessages
) => {
if (message) {
socket.emit("send msg", { socket.emit("send msg", {
user: "support", user: user,
message: supportMessage, message: message,
languageSupport: languageSupport, languageSupport: languageSupport,
languageUser: languageUser, languageUser: languageUser,
room: room,
}); });
setSupportMessage(""); setMessage("");
setSupportMessages([...supportMessages, "support: " + supportMessage]); setMessages([...messages, user + ": " + message]);
} else {
alert("Message cannot be empty");
} }
}; };
const setName = () => { const onKeyDown = function (e, user) {
console.log(supportName); if (e.keyCode === 13) {
console.log(userName); if (user === "user") {
if (supportName && userName) { sendMessage(
socket.emit("set room", { e,
support_name: supportName, "user",
user_name: userName, userMessage,
}); userMessages,
setUserMessage,
socket.emit("join", { setUserMessages
username: supportName, );
room: supportName + '-' + userName, } else {
}); sendMessage(
} e,
"support",
supportMessage,
supportMessages,
setSupportMessage,
setSupportMessages
);
} }
const onUserKeyDown = function (e) {
if (e.keyCode == 13) {
sendUserMessage(e, "user");
} }
}; };
const onSupportKeyDown = function (e) { const newPage = () => {
if (e.keyCode == 13) { window.open("/chat");
sendSupportMessage(e, "support");
} }
};
return ( return (
<Container> <Container>
<Row> <Row>
<Col> <button
<FormLabel>user name</FormLabel> id="ChatButton"
<Form.Control className="btn btn-success"
type="text" onClick={newPage}
value={userName} // disabled={buttonStatus === "generating"}
onChange={(e) => setUserName(e.target.value)} >
placeholder="Enter user name" New Chat
/> </button> </Row>
</Col>
<Col>
<FormLabel>support name</FormLabel>
<Form.Control
type="text"
value={supportName}
onChange={(e) => setSupportName(e.target.value)}
placeholder="Enter support name"
/>
</Col>
<Button onClick={(e) => setName()}>Submit(You should set user name and support name first!)</Button>
</Row>
<Row> <Row>
<Col> <Col>
<FormLabel>user</FormLabel> <FormLabel>user</FormLabel>
...@@ -161,7 +179,7 @@ function ChatTrans() { ...@@ -161,7 +179,7 @@ function ChatTrans() {
value={userMessage} value={userMessage}
onChange={(e) => setUserMessage(e.target.value)} onChange={(e) => setUserMessage(e.target.value)}
placeholder="Enter message" placeholder="Enter message"
onKeyDown={(e) => onUserKeyDown(e) } onKeyDown={(e) => onKeyDown(e, "user")}
/> />
</Col> </Col>
<Col> <Col>
...@@ -178,7 +196,7 @@ function ChatTrans() { ...@@ -178,7 +196,7 @@ function ChatTrans() {
value={supportMessage} value={supportMessage}
onChange={(e) => setSupportMessage(e.target.value)} onChange={(e) => setSupportMessage(e.target.value)}
placeholder="Enter message" placeholder="Enter message"
onKeyDown={(e) => onSupportKeyDown(e) } onKeyDown={(e) => onKeyDown(e, "support")}
/> />
</Col> </Col>
...@@ -198,8 +216,6 @@ function ChatTrans() { ...@@ -198,8 +216,6 @@ function ChatTrans() {
</ListGroup> </ListGroup>
</Col> </Col>
</Row> </Row>
</Row> </Row>
</Container> </Container>
); );
......
import React, { useEffect } from 'react';
function Test() {
useEffect(() => {
// 检查URL是否已经有token
const currentUrl = new URL(window.location.href);
if (!currentUrl.searchParams.get('token')) {
// 生成新的token
const token = generateToken();
// 更新URL
currentUrl.searchParams.set('token', token);
window.history.replaceState({}, '', currentUrl.toString());
// 保存token到sessionStorage
sessionStorage.setItem('sessionToken', token);
}
}, []);
// 这只是一个简单的token生成函数,真正的应用中,您可能需要更加复杂和安全的生成机制
const generateToken = () => {
return Math.random().toString(36).substr(2, 9);
};
return (
<div className="App">
<h1>Welcome to my React App</h1>
</div>
);
}
export default Test;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment