feat(chat): added base for chat service
added front html nigel in the mud route function with openapi - gen clean up the code a little after pull request
This commit is contained in:
parent
73a4946d17
commit
9ce9fa44e4
122 changed files with 9354 additions and 2615 deletions
|
|
@ -1,5 +1,84 @@
|
|||
import { addRoute, type RouteHandlerParams } from "@app/routing";
|
||||
import { addRoute, setTitle, type RouteHandlerParams, type RouteHandlerReturn } from "@app/routing";
|
||||
import { showError } from "@app/toast";
|
||||
import authHtml from './chat.html?raw';
|
||||
import client from '@app/api'
|
||||
import { updateUser } from "@app/auth";
|
||||
|
||||
addRoute('/chat', function (_url: string, _args: RouteHandlerParams) {
|
||||
return "this is the chat page !"
|
||||
})
|
||||
type Providers = {
|
||||
name: string,
|
||||
display_name: string,
|
||||
icon_url?: string,
|
||||
color?: { default: string, hover: string },
|
||||
};
|
||||
|
||||
function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn {
|
||||
|
||||
setTitle('Chat Page');
|
||||
return {
|
||||
|
||||
html: authHtml, postInsert: async (app) => {
|
||||
|
||||
const sendButton = document.getElementById('b-send') as HTMLButtonElement;
|
||||
const chatWindow = document.getElementById('t-chatbox') as HTMLDivElement;
|
||||
const sendtextbox= document.getElementById('t-chat-window') as HTMLButtonElement;
|
||||
const blogout = document.getElementById('b-logout') as HTMLButtonElement;
|
||||
const bwhoami = document.getElementById('b-whoami') as HTMLButtonElement;
|
||||
const username = document.getElementById('username') as HTMLDivElement;
|
||||
|
||||
|
||||
const value = await client.chatTest();
|
||||
if (value.kind === "success")
|
||||
{
|
||||
console.log(value.payload);
|
||||
}
|
||||
else if (value.kind === "notLoggedIn")
|
||||
{
|
||||
|
||||
} else {
|
||||
console.log('unknown response: ', value);
|
||||
}
|
||||
|
||||
// Add a new message to the chat display
|
||||
const addMessage = (text: any) => {
|
||||
const messageElement = document.createElement('div');
|
||||
messageElement.textContent = JSON.stringify(text, null, 2);
|
||||
chatWindow.appendChild(messageElement);
|
||||
chatWindow.scrollTop = chatWindow.scrollHeight; //puts scroll to the bottom
|
||||
};
|
||||
|
||||
sendButton!.addEventListener('click', async () => {
|
||||
let msgtext: string = sendtextbox.value;
|
||||
|
||||
if (msgtext) {
|
||||
addMessage(msgtext);
|
||||
sendtextbox.value = "";
|
||||
}
|
||||
});
|
||||
|
||||
chatWindow.textContent = "helloWorld";
|
||||
|
||||
// Whoami button to display user name
|
||||
bwhoami?.addEventListener('click', async () => {
|
||||
try {
|
||||
const res = await client.guestLogin();
|
||||
switch (res.kind) {
|
||||
case 'success': {
|
||||
let user = await updateUser();
|
||||
if (user === null)
|
||||
return showError('Failed to get user: no user ?');
|
||||
setTitle(`Welcome ${user.guest ? '[GUEST] ' : ''}${user.name}`);
|
||||
break;
|
||||
}
|
||||
case 'failed': {
|
||||
showError(`Failed to login: ${res.msg}`);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Login error:", e);
|
||||
showError('Failed to login: Unknown error');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
addRoute('/chat', handleChat, { bypass_auth: true });
|
||||
Loading…
Add table
Add a link
Reference in a new issue