added tab control to chat, When user leaves tab open on chat
This commit is contained in:
parent
40980bedeb
commit
422c0b26c4
9 changed files with 151 additions and 119 deletions
|
|
@ -2,7 +2,6 @@ apis/OpenapiOtherApi.ts
|
|||
apis/index.ts
|
||||
index.ts
|
||||
models/ChatTest200Response.ts
|
||||
models/ChatTest500Response.ts
|
||||
models/DisableOtp200Response.ts
|
||||
models/DisableOtp401Response.ts
|
||||
models/DisableOtp500Response.ts
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@
|
|||
import * as runtime from '../runtime';
|
||||
import type {
|
||||
ChatTest200Response,
|
||||
ChatTest500Response,
|
||||
DisableOtp200Response,
|
||||
DisableOtp401Response,
|
||||
DisableOtp500Response,
|
||||
|
|
@ -50,8 +49,6 @@ import type {
|
|||
import {
|
||||
ChatTest200ResponseFromJSON,
|
||||
ChatTest200ResponseToJSON,
|
||||
ChatTest500ResponseFromJSON,
|
||||
ChatTest500ResponseToJSON,
|
||||
DisableOtp200ResponseFromJSON,
|
||||
DisableOtp200ResponseToJSON,
|
||||
DisableOtp401ResponseFromJSON,
|
||||
|
|
@ -135,7 +132,7 @@ export class OpenapiOtherApi extends runtime.BaseAPI {
|
|||
|
||||
/**
|
||||
*/
|
||||
async chatTestRaw(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<ChatTest200Response | StatusOtp401Response | ChatTest500Response>> {
|
||||
async chatTestRaw(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<ChatTest200Response | StatusOtp401Response>> {
|
||||
const queryParameters: any = {};
|
||||
|
||||
const headerParameters: runtime.HTTPHeaders = {};
|
||||
|
|
@ -162,19 +159,15 @@ export class OpenapiOtherApi extends runtime.BaseAPI {
|
|||
// Object response for status 401
|
||||
return new runtime.JSONApiResponse(response, (jsonValue) => StatusOtp401ResponseFromJSON(jsonValue));
|
||||
}
|
||||
if (response.status === 500) {
|
||||
// Object response for status 500
|
||||
return new runtime.JSONApiResponse(response, (jsonValue) => ChatTest500ResponseFromJSON(jsonValue));
|
||||
}
|
||||
// CHANGED: Throw error if status code is not handled by any of the defined responses
|
||||
// This ensures all code paths return a value and provides clear error messages for unexpected status codes
|
||||
// Only throw if responses were defined but none matched the actual status code
|
||||
throw new runtime.ResponseError(response, `Unexpected status code: ${response.status}. Expected one of: 200, 401, 500`);
|
||||
throw new runtime.ResponseError(response, `Unexpected status code: ${response.status}. Expected one of: 200, 401`);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
async chatTest(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<ChatTest200Response | StatusOtp401Response | ChatTest500Response> {
|
||||
async chatTest(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<ChatTest200Response | StatusOtp401Response> {
|
||||
const response = await this.chatTestRaw(initOverrides);
|
||||
return await response.value();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
export * from './ChatTest200Response';
|
||||
export * from './ChatTest500Response';
|
||||
export * from './DisableOtp200Response';
|
||||
export * from './DisableOtp401Response';
|
||||
export * from './DisableOtp500Response';
|
||||
|
|
|
|||
|
|
@ -16,29 +16,49 @@ document.addEventListener('ft:pageChange', () => {
|
|||
|
||||
document.addEventListener("visibilitychange", async () => {
|
||||
|
||||
// When user leaves tab
|
||||
const socketId = __socket || undefined;
|
||||
let oldName = localStorage.getItem("oldName") || undefined;
|
||||
if (socketId == undefined) return;
|
||||
if (document.visibilityState === "hidden") {
|
||||
console.log("User LEFT this tab");
|
||||
|
||||
// if (__socket) {
|
||||
// __socket.close();
|
||||
// __socket = undefined;
|
||||
// }
|
||||
|
||||
let userName = await updateUser();
|
||||
oldName = userName?.name || "undefined";
|
||||
localStorage.setItem("oldName", oldName);
|
||||
socketId.emit("client_left");
|
||||
return;
|
||||
}
|
||||
|
||||
// When user returns to tab → soft reload using imported HTML file
|
||||
if (document.visibilityState === "visible") {
|
||||
// location.reload();
|
||||
//console.log(location.replace(location.href));
|
||||
|
||||
|
||||
console.log('Chat Visible')
|
||||
const res = await client.guestLogin();
|
||||
let user = await updateUser();
|
||||
socketId.emit('client_entered', {
|
||||
userName: oldName,
|
||||
user: user?.name,
|
||||
});
|
||||
setTitle('Chat Page');
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
async function getUserName(): Promise<string | null> {
|
||||
try {
|
||||
const res = await client.guestLogin();
|
||||
|
||||
if (res.kind !== "success") {
|
||||
console.error("Login failed:", res.msg);
|
||||
return null;
|
||||
}
|
||||
|
||||
const user = await updateUser();
|
||||
if (!user) return null;
|
||||
|
||||
return user.name; // <-- return the username
|
||||
} catch (err) {
|
||||
console.error("getUserName error:", err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function getSocket(): Socket {
|
||||
if (__socket === undefined)
|
||||
__socket = io("wss://localhost:8888", {
|
||||
|
|
@ -142,8 +162,8 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
|
|||
};
|
||||
|
||||
|
||||
socket.on("welcome", (data) => {
|
||||
addMessage(`${data.msg}`);
|
||||
socket.once('welcome', (data) => {
|
||||
addMessage (`${data.msg} ` + getUser()?.name);
|
||||
});
|
||||
|
||||
// Send button
|
||||
|
|
@ -161,7 +181,7 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
|
|||
timestamp: Date.now(),
|
||||
SenderWindowID: socket.id,
|
||||
};
|
||||
socket.send(JSON.stringify(message));
|
||||
socket.emit('message', JSON.stringify(message));
|
||||
}
|
||||
sendtextbox.value = "";
|
||||
}
|
||||
|
|
@ -182,6 +202,9 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
|
|||
const loggedIn = await isLoggedIn();
|
||||
|
||||
if (loggedIn?.name === undefined) return ;
|
||||
const res = await client.guestLogin();
|
||||
let user = await updateUser();
|
||||
console.log('USER ', user?.name);
|
||||
if (chatWindow) {
|
||||
addMessage('@list - lists all connected users in the chat');
|
||||
socket.emit('list');
|
||||
|
|
@ -228,3 +251,4 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
|
|||
}
|
||||
};
|
||||
addRoute('/chat', handleChat, { bypass_auth: true });
|
||||
addRoute('/chat/', handleChat, { bypass_auth: true });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue