index.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. }
  115. #textentry input {
  116. font-size: inherit;
  117. width: 100%;
  118. padding: 0.5em;
  119. margin-right: 0.5em;
  120. border: none;
  121. border-radius: 0.3em;
  122. background-color: var(--col-3);
  123. color: var(--col-text-1);
  124. }
  125. .user-item,
  126. .message-item {
  127. margin-bottom: 0.5em;
  128. }
  129. .channel-item {
  130. cursor: pointer;
  131. padding: 0.25em 0.5em;
  132. border-radius: 0.3em;
  133. margin-bottom: 0.2em;
  134. }
  135. .channel-item:hover {
  136. background-color: var(--col-3);
  137. }
  138. .channel-item.active {
  139. background-color: var(--col-accent);
  140. color: var(--col-text-2);
  141. }
  142. </style>
  143. </head>
  144. <body>
  145. <div class="panel-host" style="flex-direction: column;">
  146. <!-- <div class="panel-host">
  147. <div class="panel">
  148. <button onclick=switchTheme()>Appearance</button>
  149. </div>
  150. </div> -->
  151. <div class="panel-host">
  152. <div class="panel" id="channel-list">
  153. <h2>Channels</h2>
  154. <div id="channels">
  155. <!-- Add channel list here -->
  156. </div>
  157. <div id="channel-create" style="margin-top: auto; padding-top: 0.5em;">
  158. <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;">
  159. <button style="width: 100%;" onclick="createChannel()">+ Create Channel</button>
  160. </div>
  161. </div>
  162. <div class="panel" id="user-list">
  163. <h2>Users</h2>
  164. <div id="users">
  165. <!-- Add user list here -->
  166. </div>
  167. <div id="username-set" style="margin-top: auto; padding-top: 0.5em;">
  168. <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;">
  169. <button style="width: 100%;" onclick="setUsername()">Set Username</button>
  170. </div>
  171. </div>
  172. <div class="panel" id="chat-area">
  173. <h2>Messages</h2>
  174. <div id="messages">
  175. <!-- Add messages here -->
  176. </div>
  177. <div id="textentry">
  178. <input id="message-input" type="text" placeholder="Message #channel-name">
  179. <button onclick="submitMessageToServer()">Send</button>
  180. </div>
  181. </div>
  182. </div>
  183. </div>
  184. <script>
  185. /// (time, channel, username, content, isPending)
  186. messages = [];
  187. selectedChannel = null;
  188. selectedUsers = new Set();
  189. username = `Myst#${randomNumericString(4)}`;
  190. customChannels = new Set();
  191. function randomNumericString(length) {
  192. let v = 10 ** length;
  193. return Math.floor(Math.random() * v * 0.9 + v * 0.1);
  194. }
  195. function switchTheme() {
  196. const e = document.querySelector('body');
  197. e.setAttribute('data-theme', e.getAttribute('data-theme') === 'light' ? 'dark' : 'light');
  198. }
  199. function createChannel() {
  200. const input = document.querySelector('#channel-input');
  201. const name = input.value.trim().replace(/\s+/g, '-').toLowerCase();
  202. if (name) {
  203. customChannels.add(name);
  204. selectedChannel = name;
  205. input.value = '';
  206. visualUpdate();
  207. }
  208. }
  209. function setUsername() {
  210. const input = document.querySelector('#username-input');
  211. const name = input.value.trim();
  212. if (name) {
  213. username = name;
  214. input.value = '';
  215. visualUpdate();
  216. }
  217. }
  218. function bindEnterToAction(inputSelector, action) {
  219. const input = document.querySelector(inputSelector);
  220. if (!input) return;
  221. input.addEventListener('keydown', (event) => {
  222. if (event.key === 'Enter') {
  223. event.preventDefault();
  224. action();
  225. }
  226. });
  227. }
  228. function visualUpdate() {
  229. const messagesDiv = document.querySelector('#messages');
  230. messagesDiv.innerHTML = "";
  231. const visibleMessages = selectedChannel
  232. ? messages.filter(m => m[1] === selectedChannel)
  233. : messages;
  234. const filteredMessages = selectedUsers.size
  235. ? visibleMessages.filter(m => selectedUsers.has(m[2]))
  236. : visibleMessages;
  237. for (const [time, channel, username, message] of filteredMessages.slice().reverse()) {
  238. const messageDiv = document.createElement('div');
  239. messageDiv.classList.add('message-item');
  240. const timeString = new Date(time).toLocaleTimeString();
  241. messageDiv.textContent = `[${timeString}] ${username}: ${message}`;
  242. messagesDiv.appendChild(messageDiv);
  243. }
  244. // Disable input when no channel or "all" is selected
  245. const input = document.querySelector('#message-input');
  246. const sendButton = document.querySelector('#textentry button');
  247. const isAllSelected = !selectedChannel;
  248. input.disabled = isAllSelected;
  249. sendButton.disabled = isAllSelected;
  250. input.placeholder = isAllSelected ? 'Select a channel to send messages' : `Message #${selectedChannel} as ${username}`;
  251. // Update channels
  252. const channelsDiv = document.querySelector('#channels');
  253. // Remove empty channels that aren't selected
  254. customChannels = new Set(
  255. [...customChannels].filter(channel =>
  256. channel === selectedChannel || messages.some(m => m[1] === channel)
  257. )
  258. );
  259. const channels = new Set([...customChannels, ...messages.map(m => m[1])]);
  260. channelsDiv.innerHTML = "";
  261. const allChannelsDiv = document.createElement('div');
  262. allChannelsDiv.classList.add('channel-item');
  263. if (!selectedChannel) {
  264. allChannelsDiv.classList.add('active');
  265. }
  266. allChannelsDiv.textContent = '#all';
  267. allChannelsDiv.onclick = () => {
  268. selectedChannel = null;
  269. selectedUsers.clear();
  270. visualUpdate();
  271. };
  272. channelsDiv.appendChild(allChannelsDiv);
  273. for (const channel of channels) {
  274. const channelDiv = document.createElement('div');
  275. channelDiv.classList.add('channel-item');
  276. if (channel === selectedChannel) {
  277. channelDiv.classList.add('active');
  278. }
  279. channelDiv.textContent = `#${channel}`;
  280. channelDiv.onclick = () => {
  281. selectedChannel = channel;
  282. selectedUsers.clear();
  283. visualUpdate();
  284. };
  285. channelsDiv.appendChild(channelDiv);
  286. }
  287. // Update users
  288. const userList = document.querySelector('#users');
  289. const users = new Set(
  290. (selectedChannel ? messages.filter(m => m[1] === selectedChannel) : messages).map(m => m[2])
  291. );
  292. userList.innerHTML = "";
  293. if (selectedUsers.size) {
  294. const clearDiv = document.createElement('div');
  295. clearDiv.classList.add('user-item');
  296. clearDiv.style.cssText = 'cursor: pointer; color: var(--col-accent); margin-bottom: 0.5em;';
  297. clearDiv.textContent = '✕ Clear filters';
  298. clearDiv.onclick = () => {
  299. selectedUsers.clear();
  300. visualUpdate();
  301. };
  302. userList.appendChild(clearDiv);
  303. }
  304. for (const user of users) {
  305. const userDiv = document.createElement('div');
  306. userDiv.classList.add('user-item');
  307. userDiv.textContent = user;
  308. userDiv.style.cursor = 'pointer';
  309. if (selectedUsers.has(user)) {
  310. userDiv.style.color = 'var(--col-accent)';
  311. userDiv.style.fontWeight = 'bold';
  312. }
  313. userDiv.onclick = () => {
  314. if (selectedUsers.has(user)) {
  315. selectedUsers.delete(user);
  316. } else {
  317. selectedUsers.add(user);
  318. }
  319. visualUpdate();
  320. };
  321. userList.appendChild(userDiv);
  322. }
  323. }
  324. // TODO: Send to server
  325. async function submitMessageToServer() {
  326. const input = document.querySelector('#message-input');
  327. const message = input.value.trim();
  328. if (message) {
  329. messages.push([Date.now(), selectedChannel, username, message, true]);
  330. input.value = '';
  331. }
  332. visualUpdate();
  333. await window.__TAURI_INTERNALS__.invoke(
  334. "send_message",
  335. {
  336. channel: selectedChannel,
  337. sender: username,
  338. content: message
  339. }
  340. );
  341. // Scroll to bottom
  342. const messagesDiv = document.querySelector('#messages');
  343. messagesDiv.scrollTop = messagesDiv.scrollHeight;
  344. }
  345. async function receiveMessagesFromServer() {
  346. const result = await window.__TAURI_INTERNALS__.invoke(
  347. "receive_messages",
  348. {}
  349. );
  350. console.log("Received messages from server:", result);
  351. messages = result.map(m => [m[0], m[1], m[2], m[3], false]);
  352. }
  353. function main() {
  354. receiveMessagesFromServer();
  355. visualUpdate();
  356. bindEnterToAction('#message-input', submitMessageToServer);
  357. bindEnterToAction('#channel-input', createChannel);
  358. bindEnterToAction('#username-input', setUsername);
  359. setInterval(() => {
  360. receiveMessagesFromServer();
  361. // Don't update if user has scrolled up
  362. const messagesDiv = document.querySelector('#messages');
  363. if (messagesDiv.scrollTop + messagesDiv.clientHeight < messagesDiv.clientHeight) {
  364. return;
  365. }
  366. visualUpdate();
  367. }, 1000);
  368. }
  369. main();
  370. function simulateReceivingMessages() {
  371. // Simulate receiving messages
  372. setInterval(() => {
  373. const channels = ["root", "general", "random"];
  374. const channel = channels[Math.floor(Math.random() * channels.length)];
  375. const username = `User${randomNumericString(4)}`;
  376. const message = `Hello ${channel} from ${username}!`;
  377. messages.push([Date.now(), channel, username, message]);
  378. }, 100);
  379. }
  380. </script>
  381. </body>
  382. </html>