|
| 1 | +import { cn } from "@vyn/cn"; |
| 2 | +import { useEffect, useState } from "preact/hooks"; |
| 3 | + |
| 4 | +interface Message { |
| 5 | + user: 0 | 1 | 2; |
| 6 | + content: string; |
| 7 | +} |
| 8 | + |
| 9 | +function generateRandomWord(length: number): string { |
| 10 | + const alphabet = "abcdefghijklmnopqrstuvwxyz"; |
| 11 | + let result = ""; |
| 12 | + for (let i = 0; i < length; i++) { |
| 13 | + result += alphabet.charAt(Math.floor(Math.random() * alphabet.length)); |
| 14 | + } |
| 15 | + return result; |
| 16 | +} |
| 17 | + |
| 18 | +function generateGibberish(words: number): string { |
| 19 | + const parts: string[] = []; |
| 20 | + for (let i = 0; i < words; i++) { |
| 21 | + const len = Math.floor(Math.random() * 7) + 2; |
| 22 | + parts.push(generateRandomWord(len)); |
| 23 | + } |
| 24 | + return parts.join(" "); |
| 25 | +} |
| 26 | + |
| 27 | +function transformSentence(str: string): string { |
| 28 | + if (str.length === 0) return ""; |
| 29 | + return str.charAt(0).toUpperCase() + str.slice(1) + "."; |
| 30 | +} |
| 31 | + |
| 32 | +const users = [ |
| 33 | + generateRandomWord(2).toUpperCase(), |
| 34 | + generateRandomWord(2).toUpperCase(), |
| 35 | + "ME", |
| 36 | +]; |
| 37 | + |
| 38 | +export default function () { |
| 39 | + const [messages, setMessages] = useState<Message[]>([]); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + const intervalId = setInterval(() => { |
| 43 | + const seed = Math.random(); |
| 44 | + if (seed >= 0.1) return; |
| 45 | + |
| 46 | + const simpleSeed = Math.trunc(seed * 100); |
| 47 | + |
| 48 | + setMessages([{ |
| 49 | + user: simpleSeed % 2 === 0 ? 0 : 1, |
| 50 | + content: transformSentence(generateGibberish((simpleSeed + 1) * 2)), |
| 51 | + }, ...messages]); |
| 52 | + }, 500); |
| 53 | + |
| 54 | + return () => clearInterval(intervalId); |
| 55 | + }, [messages]); |
| 56 | + |
| 57 | + return ( |
| 58 | + <div class="col-span-full md:col-span-3 lg:col-span-4"> |
| 59 | + <div class="p-1-2 bg-base-200"> |
| 60 | + Gibberish Chat |
| 61 | + </div> |
| 62 | + <div class="p-1-2 dotted h-96 overflow-y-scroll flex flex-col-reverse"> |
| 63 | + {messages.map((message) => ( |
| 64 | + <div |
| 65 | + class={cn("chat", message.user === 2 ? "chat-end" : "chat-start")} |
| 66 | + > |
| 67 | + <div class="chat-image avatar avatar-placeholder"> |
| 68 | + <div class="bg-neutral text-neutral-content w-8 rounded-full"> |
| 69 | + <span class="text-xs">{users[message.user]}</span> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + <div |
| 73 | + class={cn( |
| 74 | + "chat-bubble", |
| 75 | + message.user === 2 ? "chat-bubble-primary" : null, |
| 76 | + )} |
| 77 | + > |
| 78 | + {message.content} |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + ))} |
| 82 | + </div> |
| 83 | + <div class="p-1-2 bg-base-200"> |
| 84 | + <div class="join"> |
| 85 | + <input |
| 86 | + id="gibberish-input" |
| 87 | + class="join-item input input-sm" |
| 88 | + type="text" |
| 89 | + /> |
| 90 | + <button |
| 91 | + type="button" |
| 92 | + class="join-item btn btn-sm" |
| 93 | + onClick={() => |
| 94 | + setMessages([{ |
| 95 | + user: 2, |
| 96 | + content: (document.getElementById( |
| 97 | + "gibberish-input", |
| 98 | + ) as HTMLInputElement).value, |
| 99 | + }, ...messages])} |
| 100 | + > |
| 101 | + Send |
| 102 | + </button> |
| 103 | + </div> |
| 104 | + </div> |
| 105 | + </div> |
| 106 | + ); |
| 107 | +} |
0 commit comments