1 import { io } from 'socket.io-client';
2 import { TimeManager } from '../shared/time';
4 function $<T>(selector: string, root: any = document): T {
5 return root.querySelector(selector) as T;
8 function $$<T>(selector: string, root: any = document): T[] {
9 return Array.from(root.querySelectorAll(selector)) as T[];
12 export function authToken(): string {
13 const token = localStorage.getItem('authToken');
17 function setTimeGradient() {
18 const gradientName = time.gradientName();
19 const $body = $<HTMLElement>('body');
20 $body.classList.value = gradientName;
22 $body.classList.add(time.getTimePeriod());
25 if(time.get24Hour() >= 5 && time.get24Hour() < 9) {
28 else if(time.get24Hour() >= 9 && time.get24Hour() < 17) {
31 else if(time.get24Hour() >= 17 && time.get24Hour() < 20) {
38 $<HTMLElement>('#time-of-day').innerHTML = `<img src="/assets/img/icons/time-of-day/${icon}.png"> ${time.getHour()}${time.getAmPm()}`;
41 function renderChatMessage(msg: string) {
42 $<HTMLElement>('#chat-messages').innerHTML += msg;
43 $<HTMLElement>('#chat-messages').scrollTop = $<HTMLElement>('#chat-messages').scrollHeight;
46 const time = new TimeManager();
49 'x-authtoken': authToken()
53 setInterval(setTimeGradient, 60 * 1000);
55 socket.on('connect', () => {
56 console.log(`Connected: ${socket.id}`);
59 socket.on('authToken', (authToken: string) => {
60 console.log(`recv auth token ${authToken}`);
61 if(localStorage.getItem('authToken') !== authToken) {
62 localStorage.setItem('authToken', authToken);
63 window.location.reload();
67 socket.on('status', html => $<HTMLElement>('#server-stats').innerHTML = html);
69 socket.on('chat', renderChatMessage);
70 socket.on('ready', bootstrap);
73 $$<HTMLElement>('nav a').forEach(el => {
74 el.addEventListener('click', e => {
75 const el = e.target as HTMLElement;
77 $$<HTMLElement>('a', el.closest('nav')).forEach(el => {
78 el.classList.remove('active');
81 el.classList.add('active');
83 const targetEl = $<HTMLElement>(el.getAttribute('hx-target')!.toString());
85 Array.from(targetEl.parentElement!.children).forEach(el => {
86 el.classList.remove('active');
88 targetEl.classList.add('active');
92 $<HTMLElement>('body').addEventListener('click', e => {
93 const target = e.target as HTMLElement;
95 if(target!.parentElement!.classList.contains('filter')) {
96 // ok this is afunky filter object!
97 const children = Array.from(target!.parentElement!.children);
98 children.forEach(el => el.classList.remove('active'));
99 target.classList.add('active');
101 const dataFilter = target.getAttribute('data-filter');
103 const targetPane = $<HTMLElement>(`.filter-result[data-filter="${dataFilter}"]`, target.closest('.filter-container'));
106 Array.from(targetPane!.parentElement!.children).forEach(el => {
107 if(el.getAttribute('data-filter') === dataFilter) {
108 el.classList.remove('hidden');
109 el.classList.add('active');
112 el.classList.add('hidden');
113 el.classList.remove('active');
119 if(target.getAttribute('formmethod') === 'dialog' && target.getAttribute('value') === 'cancel') {
120 target.closest('dialog')?.close();
124 const modalMutations = new MutationObserver((list, observer) => {
130 list.forEach(mutation => {
131 switch(((mutation.target) as HTMLElement).id) {
132 case 'modal-wrapper':
143 if($<HTMLElement>('#modal-wrapper').children.length) {
144 $$<HTMLDialogElement>('#modal-wrapper dialog').forEach(el => {
153 if($<HTMLElement>('#alerts').children.length) {
154 $$<HTMLElement>('#alerts .alert').forEach(el => {
155 if(!el.getAttribute('data-dismiss-at')) {
156 const dismiss = Date.now() + 3000;
157 el.setAttribute('data-dismiss-at', dismiss.toString());
158 setTimeout(() => { el.remove(); }, 3000);
166 modalMutations.observe($<HTMLElement>('#modal-wrapper'), { childList: true });
168 modalMutations.observe($<HTMLElement>('#alerts'), { childList: true });
171 function bootstrap() {
172 console.log('Server connection verified');
173 // target the explore tab
174 $$<HTMLElement>('nav a')[3].click();
176 document.body.addEventListener('htmx:configRequest', function(evt) {
178 evt.detail.headers['x-authtoken'] = authToken();
180 document.body.addEventListener('htmx:load', function(evt) {
181 $$<HTMLElement>('.disabled[data-block]').forEach(el => {
182 setTimeout(() => { el.removeAttribute('disabled'); }, 3000);
185 document.body.addEventListener('htmx:beforeSwap', function(e) {
186 const el = e.target as HTMLElement;
187 if(el.id === 'chat-form') {
188 $<HTMLInputElement>('#message').value = '';
191 else if(e.detail.serverResponse === 'logout') {
192 localStorage.removeItem('authToken');
193 window.location.reload();