Refactor and cleanup code

This commit is contained in:
Zhongheng Liu 2024-01-20 11:26:57 +02:00
commit 7d4953fea6
No known key found for this signature in database
9 changed files with 7 additions and 54 deletions

View file

@ -1,8 +1,8 @@
import React, { useContext, useEffect, useState } from "react";
import Chat from "./Chat/Chat";
import "./App.css";
import { LangType, Message } from "./Chat/messageTypes";
import { MessageDisplay } from "./Chat/MessageDisplay";
import { LangType, Message } from "./type/messageTypes";
import { MessageDisplay } from "./MessageDisplay/MessageDisplay";
import strings from "./Intl/strings.json";
import { LangContext, LoginContext, LoginType } from "./context";
import { contentTypes, domain, endpoints, port } from "./consts";

View file

@ -5,9 +5,9 @@ import React, {
useRef,
useState,
} from "react";
import { MessageDisplay } from "./MessageDisplay";
import { MessageDisplay } from "../MessageDisplay/MessageDisplay";
import { Client } from "@stomp/stompjs";
import { Message, MessageType } from "./messageTypes";
import { Message, MessageType } from "../type/messageTypes";
import "./Chat.css";
import strings from "../Intl/strings.json";
import { LangContext } from "../context";

View file

@ -1,7 +1,7 @@
import { useState } from "react";
import { contentTypes, domain, endpoints, port } from "../consts";
import { LoginType } from "../context";
import { User } from "../Chat/userTypes";
import { User } from "../type/userTypes";
import "./Login.css";
const encrypt = (rawPasswordString: string) => {
// TODO Encryption method stub

View file

@ -1,5 +1,5 @@
import React, { useContext } from "react";
import { Message, MessageType } from "./messageTypes";
import { Message, MessageType } from "../type/messageTypes";
import { LangContext } from "../context";
import strings from "../Intl/strings.json";
import "./MessageDisplay.css";

View file

@ -1,5 +1,5 @@
import { createContext } from "react";
import { LangType } from "./Chat/messageTypes";
import { LangType } from "./type/messageTypes";
export type LoginType = {
username: string;
lastSeen: number;

View file

@ -1,47 +0,0 @@
import { Client } from "@stomp/stompjs";
import { Message, MessageType } from "../Chat/messageTypes";
const domain = window.location.hostname;
const port = "8080";
const connectionAddress = `ws://${domain}:${port}/ws`;
const endpoints = {
destination: "/app/chat",
subscription: "/sub/chat",
history: "/api/v1/msg/",
};
export const createStompConnection = (
user: string,
subUpdateHandler: (message: Message) => void
) => {
const stompClient = new Client({
brokerURL: connectionAddress,
});
stompClient.onConnect = (frame) => {
stompClient.subscribe(endpoints.subscription, (message) => {
console.log(`Collected new message: ${message.body}`);
const messageBody = JSON.parse(message.body) as Message;
console.log(messageBody);
subUpdateHandler(messageBody);
});
stompClient.publish({
body: JSON.stringify({
type: MessageType.HELLO,
fromUserId: user,
toUserId: "everyone",
content: `${user} has joined the server!`,
timeMillis: Date.now(),
}),
destination: endpoints.destination,
});
};
// Generic error handlers
stompClient.onWebSocketError = (error) => {
console.error("Error with websocket", error);
};
stompClient.onStompError = (frame) => {
console.error("Broker reported error: " + frame.headers["message"]);
console.error("Additional details: " + frame.body);
};
return stompClient;
};