index.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. <!-- COPYRIGHT (C) AMY <RAIN@SKULD.NETWORK> & IDK THIS IS MOSTLY WRITTEN WITH COPILOT LOL -->
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Chatroom</title>
  8. <style>
  9. :root {
  10. --col-1: #2f3136;
  11. --col-2: #36393f;
  12. --col-3: #40444b;
  13. --col-accent: #7289da;
  14. --col-text-1: rgb(200, 200, 200);
  15. --col-text-2: white;
  16. }
  17. /* SCROLLBARS */
  18. /* Firefox */
  19. * {
  20. scrollbar-width: thin;
  21. scrollbar-color: var(--col-3) var(--col-1);
  22. }
  23. /* WebKit browsers */
  24. *::-webkit-scrollbar {
  25. width: 0.75em;
  26. height: 0.75em;
  27. }
  28. *::-webkit-scrollbar-track {
  29. background: var(--col-1);
  30. }
  31. *::-webkit-scrollbar-thumb {
  32. background-color: var(--col-3);
  33. border-radius: 0.5em;
  34. border: 0.15em solid var(--col-1);
  35. }
  36. *::-webkit-scrollbar-thumb:hover {
  37. background-color: var(--col-accent);
  38. }
  39. /* MAIN */
  40. html {
  41. font-family: Arial, Helvetica, sans-serif;
  42. font-size: 1rem;
  43. background-color: var(--col-1);
  44. }
  45. body {
  46. background-color: var(--col-1);
  47. color: var(--col-text-1);
  48. height: 100vh;
  49. margin: 0;
  50. padding: 0;
  51. }
  52. .panel-host {
  53. display: flex;
  54. width: 100%;
  55. height: 100%;
  56. }
  57. .panel {
  58. display: flex;
  59. flex-direction: column;
  60. overflow-y: auto;
  61. padding: 1em;
  62. background-color: var(--col-1);
  63. border-style: solid;
  64. border-collapse: collapse;
  65. border-color: var(--col-3);
  66. border-width: 0 0.1em 0 0;
  67. }
  68. #user-list,
  69. #channel-list {
  70. flex-shrink: 0;
  71. flex-grow: 1;
  72. min-width: 15%;
  73. max-width: 15%;
  74. width: 15%;
  75. }
  76. #chat-area {
  77. flex-grow: 5;
  78. display: flex;
  79. flex-direction: column;
  80. }
  81. #channels,
  82. #users,
  83. #messages {
  84. flex-shrink: 0;
  85. flex: 1;
  86. overflow-y: auto;
  87. display: flex;
  88. flex-direction: column;
  89. }
  90. #messages {
  91. flex-direction: column-reverse;
  92. }
  93. h2 {
  94. margin: 0 0 1em 0;
  95. }
  96. button {
  97. background-color: var(--col-accent);
  98. color: var(--col-text-2);
  99. border: none;
  100. border-radius: 0.3em;
  101. padding: 0.5em;
  102. font-size: inherit;
  103. cursor: pointer;
  104. user-select: none;
  105. -webkit-user-select: none;
  106. }
  107. button:disabled {
  108. background-color: var(--col-3);
  109. color: var(--col-text-1);
  110. cursor: not-allowed;
  111. }
  112. #textentry {
  113. display: flex;
  114. align-items: center;
  115. }
  116. #textentry input {
  117. font-size: inherit;
  118. width: 100%;
  119. padding: 0.5em;
  120. margin-right: 0.5em;
  121. border: none;
  122. border-radius: 0.3em;
  123. background-color: var(--col-3);
  124. color: var(--col-text-1);
  125. }
  126. .icon-button {
  127. width: 2em;
  128. height: 2em;
  129. margin-right: 0.5em;
  130. padding: 1em;
  131. display: inline-flex;
  132. align-items: center;
  133. justify-content: center;
  134. font-size: 1.2em;
  135. }
  136. .settings-modal-overlay {
  137. position: fixed;
  138. inset: 0;
  139. background: rgba(0, 0, 0, 0.45);
  140. display: none;
  141. align-items: center;
  142. justify-content: center;
  143. z-index: 1000;
  144. }
  145. .settings-modal {
  146. width: min(26em, calc(100vw - 2em));
  147. background: var(--col-2);
  148. border: 0.1em solid var(--col-3);
  149. border-radius: 0.5em;
  150. padding: 1em;
  151. box-sizing: border-box;
  152. }
  153. .settings-modal h3 {
  154. margin: 0 0 0.8em 0;
  155. color: var(--col-text-2);
  156. }
  157. .settings-field {
  158. margin-bottom: 0.75em;
  159. }
  160. .settings-field label {
  161. display: block;
  162. margin-bottom: 0.3em;
  163. }
  164. .settings-field input {
  165. width: 100%;
  166. box-sizing: border-box;
  167. font-size: inherit;
  168. padding: 0.5em;
  169. border: none;
  170. border-radius: 0.3em;
  171. background-color: var(--col-3);
  172. color: var(--col-text-1);
  173. }
  174. .settings-actions {
  175. display: flex;
  176. justify-content: flex-end;
  177. gap: 0.5em;
  178. margin-top: 1em;
  179. }
  180. .user-item,
  181. .message-item {
  182. margin-bottom: 0.5em;
  183. }
  184. .channel-item {
  185. cursor: pointer;
  186. padding: 0.25em 0.5em;
  187. border-radius: 0.3em;
  188. margin-bottom: 0.2em;
  189. }
  190. .channel-item:hover {
  191. background-color: var(--col-3);
  192. }
  193. .channel-item.active {
  194. background-color: var(--col-accent);
  195. color: var(--col-text-2);
  196. }
  197. </style>
  198. </head>
  199. <body>
  200. <div class="panel-host" style="flex-direction: column;">
  201. <!-- <div class="panel-host">
  202. <div class="panel">
  203. <button onclick=switchTheme()>Appearance</button>
  204. </div>
  205. </div> -->
  206. <div class="panel-host">
  207. <div class="panel" id="channel-list">
  208. <h2>Channels</h2>
  209. <div id="channels">
  210. <!-- Add channel list here -->
  211. </div>
  212. <div id="channel-create" style="margin-top: auto; padding-top: 0.5em;">
  213. <input id="channel-input" type="text" placeholder="New channel name" style="font-size: inherit; width: 100%; padding: 0.5em; margin-bottom: 0.4em; border: none; border-radius: 0.3em; background-color: var(--col-3); color: var(--col-text-1); box-sizing: border-box;">
  214. <button style="width: 100%;" onclick="createChannel()">+ Create Channel</button>
  215. </div>
  216. </div>
  217. <div class="panel" id="user-list">
  218. <h2>Users</h2>
  219. <div id="users">
  220. <!-- Add user list here -->
  221. </div>
  222. <div id="username-set" style="margin-top: auto; padding-top: 0.5em;">
  223. <input id="username-input" type="text" placeholder="Set username" style="font-size: inherit; width: 100%; padding: 0.5em; margin-bottom: 0.4em; border: none; border-radius: 0.3em; background-color: var(--col-3); color: var(--col-text-1); box-sizing: border-box;">
  224. <button style="width: 100%;" onclick="setUsername()">Set Username</button>
  225. </div>
  226. </div>
  227. <div class="panel" id="chat-area">
  228. <h2>Messages</h2>
  229. <div id="messages">
  230. <!-- Add messages here -->
  231. </div>
  232. <div id="textentry">
  233. <button id="settings-button" class="icon-button" type="button" title="Settings" aria-label="Open settings">⚙</button>
  234. <input id="message-input" type="text" placeholder="Message #channel-name">
  235. <button id="send-button" onclick="submitMessageToServer()">Send</button>
  236. </div>
  237. </div>
  238. </div>
  239. </div>
  240. <div id="settings-modal-overlay" class="settings-modal-overlay" role="dialog" aria-modal="true" aria-labelledby="settings-modal-title">
  241. <div class="settings-modal">
  242. <h3 id="settings-modal-title">Connection Settings</h3>
  243. <div class="settings-field">
  244. <label for="hostname-input">Hostname</label>
  245. <input id="hostname-input" type="text" placeholder="example.com">
  246. </div>
  247. <div class="settings-field">
  248. <label for="password-input">Password</label>
  249. <input id="password-input" placeholder="Enter password">
  250. </div>
  251. <div class="settings-actions">
  252. <button type="button" id="settings-cancel-button">Cancel</button>
  253. <button type="button" id="settings-save-button">Save</button>
  254. </div>
  255. </div>
  256. </div>
  257. <script>
  258. /// (time, channel, username, content, isPending)
  259. messages = [];
  260. selectedChannel = null;
  261. selectedUsers = new Set();
  262. username = `Myst#${randomNumericString(4)}`;
  263. customChannels = new Set();
  264. settings = {
  265. hostname: '',
  266. password: 'null'
  267. };
  268. function randomNumericString(length) {
  269. let v = 10 ** length;
  270. return Math.floor(Math.random() * v * 0.9 + v * 0.1);
  271. }
  272. function switchTheme() {
  273. const e = document.querySelector('body');
  274. e.setAttribute('data-theme', e.getAttribute('data-theme') === 'light' ? 'dark' : 'light');
  275. }
  276. function createChannel() {
  277. const input = document.querySelector('#channel-input');
  278. const name = input.value.trim().replace(/\s+/g, '-').toLowerCase();
  279. if (name) {
  280. customChannels.add(name);
  281. selectedChannel = name;
  282. input.value = '';
  283. visualUpdate();
  284. }
  285. }
  286. function setUsername() {
  287. const input = document.querySelector('#username-input');
  288. const name = input.value.trim();
  289. if (name) {
  290. username = name;
  291. input.value = '';
  292. visualUpdate();
  293. }
  294. }
  295. function bindEnterToAction(inputSelector, action) {
  296. const input = document.querySelector(inputSelector);
  297. if (!input) return;
  298. input.addEventListener('keydown', (event) => {
  299. if (event.key === 'Enter') {
  300. event.preventDefault();
  301. action();
  302. }
  303. });
  304. }
  305. function openSettingsModal() {
  306. const overlay = document.querySelector('#settings-modal-overlay');
  307. const hostnameInput = document.querySelector('#hostname-input');
  308. const passwordInput = document.querySelector('#password-input');
  309. hostnameInput.value = settings.hostname;
  310. if (settings.password === '') {
  311. passwordInput.value = 'null';
  312. } else {
  313. passwordInput.value = settings.password;
  314. }
  315. overlay.style.display = 'flex';
  316. hostnameInput.focus();
  317. }
  318. function closeSettingsModal() {
  319. const overlay = document.querySelector('#settings-modal-overlay');
  320. overlay.style.display = 'none';
  321. }
  322. function saveSettings() {
  323. const hostnameInput = document.querySelector('#hostname-input');
  324. const passwordInput = document.querySelector('#password-input');
  325. settings.hostname = hostnameInput.value.trim();
  326. settings.password = passwordInput.value;
  327. closeSettingsModal();
  328. }
  329. function visualUpdate() {
  330. const messagesDiv = document.querySelector('#messages');
  331. messagesDiv.innerHTML = "";
  332. const visibleMessages = selectedChannel
  333. ? messages.filter(m => m[1] === selectedChannel)
  334. : messages;
  335. const filteredMessages = selectedUsers.size
  336. ? visibleMessages.filter(m => selectedUsers.has(m[2]))
  337. : visibleMessages;
  338. for (const [time, channel, username, message, isPending] of filteredMessages.slice().reverse()) {
  339. const messageDiv = document.createElement('div');
  340. messageDiv.classList.add('message-item');
  341. if (isPending) messageDiv.style.opacity = '0.5';
  342. const timeString = new Date(time).toLocaleTimeString();
  343. messageDiv.textContent = `[${timeString}] ${username}: ${message}`;
  344. messagesDiv.appendChild(messageDiv);
  345. }
  346. // Disable input when no channel or "all" is selected
  347. const input = document.querySelector('#message-input');
  348. const sendButton = document.querySelector('#send-button');
  349. const isAllSelected = !selectedChannel;
  350. input.disabled = isAllSelected;
  351. sendButton.disabled = isAllSelected;
  352. input.placeholder = isAllSelected ? 'Select a channel to send messages' : `Message #${selectedChannel} as ${username}`;
  353. // Update channels
  354. const channelsDiv = document.querySelector('#channels');
  355. // Remove empty channels that aren't selected
  356. customChannels = new Set(
  357. [...customChannels].filter(channel =>
  358. channel === selectedChannel || messages.some(m => m[1] === channel)
  359. )
  360. );
  361. const channels = new Set([...customChannels, ...messages.map(m => m[1])]);
  362. channelsDiv.innerHTML = "";
  363. const allChannelsDiv = document.createElement('div');
  364. allChannelsDiv.classList.add('channel-item');
  365. if (!selectedChannel) {
  366. allChannelsDiv.classList.add('active');
  367. }
  368. allChannelsDiv.textContent = '#all';
  369. allChannelsDiv.onclick = () => {
  370. selectedChannel = null;
  371. selectedUsers.clear();
  372. visualUpdate();
  373. };
  374. channelsDiv.appendChild(allChannelsDiv);
  375. for (const channel of channels) {
  376. const channelDiv = document.createElement('div');
  377. channelDiv.classList.add('channel-item');
  378. if (channel === selectedChannel) {
  379. channelDiv.classList.add('active');
  380. }
  381. channelDiv.textContent = `#${channel}`;
  382. channelDiv.onclick = () => {
  383. selectedChannel = channel;
  384. selectedUsers.clear();
  385. visualUpdate();
  386. };
  387. channelsDiv.appendChild(channelDiv);
  388. }
  389. // Update users
  390. const userList = document.querySelector('#users');
  391. const users = new Set(
  392. (selectedChannel ? messages.filter(m => m[1] === selectedChannel) : messages).map(m => m[2])
  393. );
  394. userList.innerHTML = "";
  395. if (selectedUsers.size) {
  396. const clearDiv = document.createElement('div');
  397. clearDiv.classList.add('user-item');
  398. clearDiv.style.cssText = 'cursor: pointer; color: var(--col-accent); margin-bottom: 0.5em;';
  399. clearDiv.textContent = '✕ Clear filters';
  400. clearDiv.onclick = () => {
  401. selectedUsers.clear();
  402. visualUpdate();
  403. };
  404. userList.appendChild(clearDiv);
  405. }
  406. for (const user of users) {
  407. const userDiv = document.createElement('div');
  408. userDiv.classList.add('user-item');
  409. userDiv.textContent = user;
  410. userDiv.style.cursor = 'pointer';
  411. if (selectedUsers.has(user)) {
  412. userDiv.style.color = 'var(--col-accent)';
  413. userDiv.style.fontWeight = 'bold';
  414. }
  415. userDiv.onclick = () => {
  416. if (selectedUsers.has(user)) {
  417. selectedUsers.delete(user);
  418. } else {
  419. selectedUsers.add(user);
  420. }
  421. visualUpdate();
  422. };
  423. userList.appendChild(userDiv);
  424. }
  425. }
  426. async function submitMessageToServer() {
  427. const input = document.querySelector('#message-input');
  428. const message = input.value.trim();
  429. if (message) {
  430. messages.push([Date.now(), selectedChannel, username, message, true]);
  431. input.value = '';
  432. }
  433. visualUpdate();
  434. await window.__TAURI_INTERNALS__.invoke(
  435. "send_message",
  436. {
  437. channel: selectedChannel,
  438. sender: username,
  439. content: message,
  440. hostname: settings.hostname,
  441. password: settings.password
  442. }
  443. );
  444. // Scroll to bottom
  445. const messagesDiv = document.querySelector('#messages');
  446. messagesDiv.scrollTop = messagesDiv.scrollHeight;
  447. }
  448. async function receiveMessagesFromServer() {
  449. const result = await window.__TAURI_INTERNALS__.invoke(
  450. "receive_messages",
  451. {
  452. hostname: settings.hostname,
  453. password: settings.password
  454. }
  455. );
  456. console.log("Received messages from server:", result);
  457. messages = result.map(m => [m[0], m[1], m[2], m[3], false]);
  458. }
  459. function main() {
  460. receiveMessagesFromServer();
  461. visualUpdate();
  462. const settingsButton = document.querySelector('#settings-button');
  463. const settingsSaveButton = document.querySelector('#settings-save-button');
  464. const settingsCancelButton = document.querySelector('#settings-cancel-button');
  465. const settingsOverlay = document.querySelector('#settings-modal-overlay');
  466. settingsButton.addEventListener('click', openSettingsModal);
  467. settingsSaveButton.addEventListener('click', saveSettings);
  468. settingsCancelButton.addEventListener('click', closeSettingsModal);
  469. settingsOverlay.addEventListener('click', (event) => {
  470. if (event.target === settingsOverlay) {
  471. closeSettingsModal();
  472. }
  473. });
  474. document.addEventListener('keydown', (event) => {
  475. if (event.key === 'Escape' && settingsOverlay.style.display === 'flex') {
  476. closeSettingsModal();
  477. }
  478. });
  479. bindEnterToAction('#message-input', submitMessageToServer);
  480. bindEnterToAction('#channel-input', createChannel);
  481. bindEnterToAction('#username-input', setUsername);
  482. bindEnterToAction('#hostname-input', saveSettings);
  483. bindEnterToAction('#password-input', saveSettings);
  484. setInterval(() => {
  485. receiveMessagesFromServer();
  486. // Don't update if user has scrolled up
  487. const messagesDiv = document.querySelector('#messages');
  488. if (messagesDiv.scrollTop + messagesDiv.clientHeight < messagesDiv.clientHeight) {
  489. return;
  490. }
  491. visualUpdate();
  492. }, 1000);
  493. }
  494. main();
  495. function simulateReceivingMessages() {
  496. // Simulate receiving messages
  497. setInterval(() => {
  498. const channels = ["root", "general", "random"];
  499. const channel = channels[Math.floor(Math.random() * channels.length)];
  500. const username = `User${randomNumericString(4)}`;
  501. const message = `Hello ${channel} from ${username}!`;
  502. messages.push([Date.now(), channel, username, message]);
  503. }, 100);
  504. }
  505. </script>
  506. </body>
  507. </html>