Upload files to "kronic-chat/js"
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
window.Kronick = window.Kronick || {};
|
||||
|
||||
Kronick.account = { user: null, rooms: [], groups: [] };
|
||||
|
||||
Kronick.loadAccountContext = async function loadAccountContext() {
|
||||
const me = await fetch('/api/auth/me', { cache: 'no-store', credentials: 'same-origin' });
|
||||
if (!me.ok) {
|
||||
window.location.replace('/login');
|
||||
return false;
|
||||
}
|
||||
const meData = await me.json();
|
||||
Kronick.account.user = meData.user;
|
||||
|
||||
const roomsResponse = await fetch('/api/my/rooms', { cache: 'no-store', credentials: 'same-origin' });
|
||||
if (!roomsResponse.ok) throw new Error('Could not load assigned rooms.');
|
||||
const data = await roomsResponse.json();
|
||||
Kronick.account.rooms = data.rooms || [];
|
||||
Kronick.account.groups = data.groups || [];
|
||||
|
||||
const userInput = document.getElementById('userName');
|
||||
if (userInput) {
|
||||
userInput.value = Kronick.account.user.username;
|
||||
userInput.readOnly = true;
|
||||
userInput.title = 'Your account name is used for chat identity.';
|
||||
}
|
||||
|
||||
const roomInput = document.getElementById('roomName');
|
||||
const roomList = document.getElementById('savedRooms');
|
||||
if (roomList) {
|
||||
roomList.innerHTML = '';
|
||||
Kronick.account.rooms.forEach(room => {
|
||||
const option = document.createElement('option');
|
||||
option.value = room.room_name;
|
||||
option.label = `${room.group_name} — ${room.display_name || room.room_name}`;
|
||||
roomList.appendChild(option);
|
||||
});
|
||||
}
|
||||
if (roomInput) {
|
||||
roomInput.placeholder = Kronick.account.rooms.length ? 'Choose an assigned room' : 'No rooms assigned';
|
||||
roomInput.readOnly = Kronick.account.rooms.length === 0;
|
||||
let selected = new URLSearchParams(location.search).get('room');
|
||||
if (window.parent !== window) {
|
||||
try { selected = new URLSearchParams(window.parent.location.search).get('room') || selected; } catch (_) {}
|
||||
}
|
||||
const valid = Kronick.account.rooms.find(r => r.room_name === selected);
|
||||
if (selected) {
|
||||
roomInput.value = valid ? valid.room_name : '';
|
||||
roomInput.readOnly = true;
|
||||
if (!valid) roomInput.placeholder = 'Selected room unavailable for this account';
|
||||
} else if (Kronick.account.rooms.length === 1) {
|
||||
roomInput.value = Kronick.account.rooms[0].room_name;
|
||||
} else if (!Kronick.account.rooms.some(r => r.room_name === roomInput.value)) {
|
||||
roomInput.value = '';
|
||||
}
|
||||
}
|
||||
const join = document.getElementById('joinBtn');
|
||||
if (join && (Kronick.account.rooms.length === 0 || (roomInput?.readOnly && !roomInput.value))) join.disabled = true;
|
||||
|
||||
const accountLabel = document.getElementById('accountLabel');
|
||||
if (accountLabel) accountLabel.textContent = Kronick.account.user.username;
|
||||
return true;
|
||||
};
|
||||
|
||||
Kronick.logoutAccount = async function logoutAccount() {
|
||||
try { await fetch('/api/auth/logout', { method: 'POST', credentials: 'same-origin' }); } catch (_) {}
|
||||
window.location.replace('/login');
|
||||
};
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
Kronick.loadAccountContext().catch(error => {
|
||||
console.error('Account bootstrap failed:', error);
|
||||
window.location.replace('/login');
|
||||
});
|
||||
});
|
||||
|
||||
window.logoutAccount = () => Kronick.logoutAccount();
|
||||
Reference in New Issue
Block a user