import { c as create_ssr_component, h as subscribe, o as onDestroy, g as add_attribute, e as escape, i as each } from "../../../chunks/index3.js"; import { w as writable } from "../../../chunks/index2.js"; import { p as public_env } from "../../../chunks/shared-server.js"; import { t as toast } from "../../../chunks/Toaster.svelte_svelte_type_style_lang.js"; import "../../../chunks/index.js"; const eth_private_key = writable(""); const group = writable(""); const createNewRoom = writable(false); function guard(name) { return () => { throw new Error(`Cannot call ${name}(...) on the server`); }; } const goto = guard("goto"); const Page = create_ssr_component(($$result, $$props, $$bindings, slots) => { let $group, $$unsubscribe_group; let $eth_private_key, $$unsubscribe_eth_private_key; let $createNewRoom, $$unsubscribe_createNewRoom; $$unsubscribe_group = subscribe(group, (value) => $group = value); $$unsubscribe_eth_private_key = subscribe(eth_private_key, (value) => $eth_private_key = value); $$unsubscribe_createNewRoom = subscribe(createNewRoom, (value) => $createNewRoom = value); let status = "๐Ÿ”ด"; let statusTip = "Disconnected"; let message = ""; let messages = []; let socket; let interval; let delay = 2e3; let timeout = false; let currentVotingProposal = null; let showVotingUI = false; function connect() { socket = new WebSocket(`${public_env.PUBLIC_WEBSOCKET_URL}/ws`); socket.addEventListener("open", () => { status = "๐ŸŸข"; statusTip = "Connected"; timeout = false; socket.send(JSON.stringify({ eth_private_key: $eth_private_key, group_id: $group, should_create: $createNewRoom })); }); socket.addEventListener("close", () => { status = "๐Ÿ”ด"; statusTip = "Disconnected"; if (timeout == false) { delay = 2e3; timeout = true; } }); socket.addEventListener("message", function(event) { if (event.data == "Username already taken.") { toast.error(event.data); goto("/"); } else { try { const data = JSON.parse(event.data); if (data.type === "voting_proposal") { currentVotingProposal = data.proposal; showVotingUI = true; toast.success("New voting proposal received!"); } else { messages = [...messages, event.data]; } } catch (e) { messages = [...messages, event.data]; } } }); } onDestroy(() => { if (socket) { socket.close(); } if (interval) { clearInterval(interval); } timeout = false; }); { { if (interval || !timeout && interval) { clearInterval(interval); } if (timeout == true) { interval = setInterval( () => { if (delay < 3e4) delay = delay * 2; console.log("reconnecting in:", delay); connect(); }, delay ); } } } $$unsubscribe_group(); $$unsubscribe_eth_private_key(); $$unsubscribe_createNewRoom(); return `

MLS Chat ${escape(status)}

${showVotingUI && currentVotingProposal ? `

Voting Proposal

Group Name: ${escape(currentVotingProposal.group_name)}

Proposal ID: ${escape(currentVotingProposal.proposal_id)}

Proposal Payload:

${escape(currentVotingProposal.payload)}
` : ``}
${each(messages, (msg) => { return `
${escape(msg)}
`; })}
`; }); export { Page as default };