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 localStorage.setItem('authToken', authToken);
64 socket.on('chat', renderChatMessage);
65 socket.on('ready', bootstrap);
68 $$<HTMLElement>('nav a').forEach(el => {
69 el.addEventListener('click', e => {
70 const el = e.target as HTMLElement;
72 $$<HTMLElement>('a', el.closest('nav')).forEach(el => {
73 el.classList.remove('active');
76 el.classList.add('active');
78 const targetEl = $<HTMLElement>(el.getAttribute('hx-target')!.toString());
80 Array.from(targetEl.parentElement!.children).forEach(el => {
81 el.classList.remove('active');
83 targetEl.classList.add('active');
87 $<HTMLElement>('body').addEventListener('click', e => {
88 const target = e.target as HTMLElement;
90 if(target!.parentElement!.classList.contains('filter')) {
91 // ok this is afunky filter object!
92 const children = Array.from(target!.parentElement!.children);
93 children.forEach(el => el.classList.remove('active'));
94 target.classList.add('active');
96 const dataFilter = target.getAttribute('data-filter');
98 const targetPane = $<HTMLElement>(`.filter-result[data-filter="${dataFilter}"]`, target.closest('.filter-container'));
101 Array.from(targetPane!.parentElement!.children).forEach(el => {
102 if(el.getAttribute('data-filter') === dataFilter) {
103 el.classList.remove('hidden');
104 el.classList.add('active');
107 el.classList.add('hidden');
108 el.classList.remove('active');
114 if(target.getAttribute('formmethod') === 'dialog' && target.getAttribute('value') === 'cancel') {
115 target.closest('dialog')?.close();
119 const modalMutations = new MutationObserver((list, observer) => {
125 list.forEach(mutation => {
126 switch(((mutation.target) as HTMLElement).id) {
127 case 'modal-wrapper':
138 if($<HTMLElement>('#modal-wrapper').children.length) {
139 $$<HTMLDialogElement>('#modal-wrapper dialog').forEach(el => {
148 if($<HTMLElement>('#alerts').children.length) {
149 $$<HTMLElement>('#alerts .alert').forEach(el => {
150 if(!el.getAttribute('data-dismiss-at')) {
151 const dismiss = Date.now() + 3000;
152 el.setAttribute('data-dismiss-at', dismiss.toString());
153 setTimeout(() => { el.remove(); }, 3000);
161 modalMutations.observe($<HTMLElement>('#modal-wrapper'), { childList: true });
163 modalMutations.observe($<HTMLElement>('#alerts'), { childList: true });
166 function bootstrap() {
167 console.log('Server connection verified');
168 $$<HTMLElement>('nav a')[0].click();
170 document.body.addEventListener('htmx:configRequest', function(evt) {
172 evt.detail.headers['x-authtoken'] = authToken();
174 document.body.addEventListener('htmx:load', function(evt) {
175 $$<HTMLElement>('.disabled[data-block]').forEach(el => {
176 setTimeout(() => { el.removeAttribute('disabled'); }, 3000);
179 document.body.addEventListener('htmx:beforeSwap', function(e) {
180 const el = e.target as HTMLElement;
181 if(el.id === 'chat-form') {
182 $<HTMLInputElement>('#message').value = '';
185 else if(e.detail.serverResponse === 'logout') {
186 localStorage.removeItem('authToken');
187 window.location.reload();