index.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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] of filteredMessages.slice().reverse()) {
  339. const messageDiv = document.createElement('div');
  340. messageDiv.classList.add('message-item');
  341. const timeString = new Date(time).toLocaleTimeString();
  342. messageDiv.textContent = `[${timeString}] ${username}: ${message}`;
  343. messagesDiv.appendChild(messageDiv);
  344. }
  345. // Disable input when no channel or "all" is selected
  346. const input = document.querySelector('#message-input');
  347. const sendButton = document.querySelector('#send-button');
  348. const isAllSelected = !selectedChannel;
  349. input.disabled = isAllSelected;
  350. sendButton.disabled = isAllSelected;
  351. input.placeholder = isAllSelected ? 'Select a channel to send messages' : `Message #${selectedChannel} as ${username}`;
  352. // Update channels
  353. const channelsDiv = document.querySelector('#channels');
  354. // Remove empty channels that aren't selected
  355. customChannels = new Set(
  356. [...customChannels].filter(channel =>
  357. channel === selectedChannel || messages.some(m => m[1] === channel)
  358. )
  359. );
  360. const channels = new Set([...customChannels, ...messages.map(m => m[1])]);
  361. channelsDiv.innerHTML = "";
  362. const allChannelsDiv = document.createElement('div');
  363. allChannelsDiv.classList.add('channel-item');
  364. if (!selectedChannel) {
  365. allChannelsDiv.classList.add('active');
  366. }
  367. allChannelsDiv.textContent = '#all';
  368. allChannelsDiv.onclick = () => {
  369. selectedChannel = null;
  370. selectedUsers.clear();
  371. visualUpdate();
  372. };
  373. channelsDiv.appendChild(allChannelsDiv);
  374. for (const channel of channels) {
  375. const channelDiv = document.createElement('div');
  376. channelDiv.classList.add('channel-item');
  377. if (channel === selectedChannel) {
  378. channelDiv.classList.add('active');
  379. }
  380. channelDiv.textContent = `#${channel}`;
  381. channelDiv.onclick = () => {
  382. selectedChannel = channel;
  383. selectedUsers.clear();
  384. visualUpdate();
  385. };
  386. channelsDiv.appendChild(channelDiv);
  387. }
  388. // Update users
  389. const userList = document.querySelector('#users');
  390. const users = new Set(
  391. (selectedChannel ? messages.filter(m => m[1] === selectedChannel) : messages).map(m => m[2])
  392. );
  393. userList.innerHTML = "";
  394. if (selectedUsers.size) {
  395. const clearDiv = document.createElement('div');
  396. clearDiv.classList.add('user-item');
  397. clearDiv.style.cssText = 'cursor: pointer; color: var(--col-accent); margin-bottom: 0.5em;';
  398. clearDiv.textContent = '✕ Clear filters';
  399. clearDiv.onclick = () => {
  400. selectedUsers.clear();
  401. visualUpdate();
  402. };
  403. userList.appendChild(clearDiv);
  404. }
  405. for (const user of users) {
  406. const userDiv = document.createElement('div');
  407. userDiv.classList.add('user-item');
  408. userDiv.textContent = user;
  409. userDiv.style.cursor = 'pointer';
  410. if (selectedUsers.has(user)) {
  411. userDiv.style.color = 'var(--col-accent)';
  412. userDiv.style.fontWeight = 'bold';
  413. }
  414. userDiv.onclick = () => {
  415. if (selectedUsers.has(user)) {
  416. selectedUsers.delete(user);
  417. } else {
  418. selectedUsers.add(user);
  419. }
  420. visualUpdate();
  421. };
  422. userList.appendChild(userDiv);
  423. }
  424. }
  425. async function submitMessageToServer() {
  426. const input = document.querySelector('#message-input');
  427. const message = input.value.trim();
  428. if (message) {
  429. messages.push([Date.now(), selectedChannel, username, message, true]);
  430. input.value = '';
  431. }
  432. visualUpdate();
  433. await window.__TAURI_INTERNALS__.invoke(
  434. "send_message",
  435. {
  436. channel: selectedChannel,
  437. sender: username,
  438. content: message,
  439. hostname: settings.hostname,
  440. password: settings.password
  441. }
  442. );
  443. // Scroll to bottom
  444. const messagesDiv = document.querySelector('#messages');
  445. messagesDiv.scrollTop = messagesDiv.scrollHeight;
  446. }
  447. async function receiveMessagesFromServer() {
  448. const result = await window.__TAURI_INTERNALS__.invoke(
  449. "receive_messages",
  450. {
  451. hostname: settings.hostname,
  452. password: settings.password
  453. }
  454. );
  455. console.log("Received messages from server:", result);
  456. messages = result.map(m => [m[0], m[1], m[2], m[3], false]);
  457. }
  458. function main() {
  459. receiveMessagesFromServer();
  460. visualUpdate();
  461. const settingsButton = document.querySelector('#settings-button');
  462. const settingsSaveButton = document.querySelector('#settings-save-button');
  463. const settingsCancelButton = document.querySelector('#settings-cancel-button');
  464. const settingsOverlay = document.querySelector('#settings-modal-overlay');
  465. settingsButton.addEventListener('click', openSettingsModal);
  466. settingsSaveButton.addEventListener('click', saveSettings);
  467. settingsCancelButton.addEventListener('click', closeSettingsModal);
  468. settingsOverlay.addEventListener('click', (event) => {
  469. if (event.target === settingsOverlay) {
  470. closeSettingsModal();
  471. }
  472. });
  473. document.addEventListener('keydown', (event) => {
  474. if (event.key === 'Escape' && settingsOverlay.style.display === 'flex') {
  475. closeSettingsModal();
  476. }
  477. });
  478. bindEnterToAction('#message-input', submitMessageToServer);
  479. bindEnterToAction('#channel-input', createChannel);
  480. bindEnterToAction('#username-input', setUsername);
  481. bindEnterToAction('#hostname-input', saveSettings);
  482. bindEnterToAction('#password-input', saveSettings);
  483. setInterval(() => {
  484. receiveMessagesFromServer();
  485. // Don't update if user has scrolled up
  486. const messagesDiv = document.querySelector('#messages');
  487. if (messagesDiv.scrollTop + messagesDiv.clientHeight < messagesDiv.clientHeight) {
  488. return;
  489. }
  490. visualUpdate();
  491. }, 1000);
  492. }
  493. main();
  494. function simulateReceivingMessages() {
  495. // Simulate receiving messages
  496. setInterval(() => {
  497. const channels = ["root", "general", "random"];
  498. const channel = channels[Math.floor(Math.random() * channels.length)];
  499. const username = `User${randomNumericString(4)}`;
  500. const message = `Hello ${channel} from ${username}!`;
  501. messages.push([Date.now(), channel, username, message]);
  502. }, 100);
  503. }
  504. </script>
  505. </body>
  506. </html>