Compare commits
1 Commits
proposal-8
...
proposal-d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
69464a16b6 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +0,0 @@
|
|||||||
node_modules/
|
|
||||||
69
README.md
69
README.md
@@ -1,55 +1,24 @@
|
|||||||
# P2P Poll App
|
# 🗳️ Simple P2P Poll & Chat
|
||||||
|
|
||||||
A peer-to-peer polling app built with [Automerge](https://automerge.org/) and Vanilla JS. No backend required — polls sync in real time across browsers using CRDTs and a WebSocket relay.
|
This is a minimal prototype of a decentralized governance tool built for the **Demcode Evocracy** experiment. It allows for basic deliberation and voting without a central database or server-side logic.
|
||||||
|
|
||||||
## Features
|
## What it does
|
||||||
|
- **P2P Connectivity:** Uses WebRTC (via PeerJS) to link browsers directly.
|
||||||
|
- **Mesh-capable:** Allows multiple connections to sync the same poll state.
|
||||||
|
- **Live Sync:** Changes to the poll topic, options, or votes are broadcast to all connected peers.
|
||||||
|
- **Chat:** A simple sidebar to discuss the poll in real-time.
|
||||||
|
|
||||||
- **Add options** — type an option and press Enter or click "Add Option". Duplicate options (case-insensitive) are rejected with an inline error message.
|
## How to use it
|
||||||
- **Vote on options** — click the vote button next to any option. You can vote on multiple options, but only once per option.
|
1. Open the page: [https://andrewlehr.github.io/evocracy-p2p-poll/]
|
||||||
- **Toggle votes** — click your vote again to remove it (the button turns green with a checkmark when you've voted).
|
2. Set your **Nickname** and click "Set".
|
||||||
- **Live vote counts** — options are sorted by vote count in descending order and update in real time as votes come in from other users.
|
3. Share your nickname with a friend.
|
||||||
- **2-minute countdown timer** — click "Start 2-min Vote" to begin a shared countdown. Voting is open before and during the countdown. Once the timer expires, all vote buttons are disabled.
|
4. One person enters the other's nickname in the **Connect** box and clicks "Add Peer".
|
||||||
- **Shared timer** — the deadline is stored in the Automerge document, so all peers see the same countdown and voting closes simultaneously for everyone.
|
5. Once connected, you can set a topic, add options, and vote.
|
||||||
- **Copy URL** — click "Copy URL" to copy the poll link to your clipboard. Share it with others to join the same poll.
|
|
||||||
- **P2P sync** — all state (options, votes, timer) syncs across browsers in real time with no central server handling your data.
|
|
||||||
|
|
||||||
## How it works
|
## Technical Notes
|
||||||
|
- **Data Persistence:** There is no database. If everyone closes their browser, the data is lost.
|
||||||
|
- **Networking:** It uses a public signaling server to find peers, but the actual data (votes/chat) stays between the browsers.
|
||||||
|
- **Conflict Handling:** Uses a simple "Last Write Wins" broadcast to keep peers in sync.
|
||||||
|
|
||||||
### Tech stack
|
## Transparency
|
||||||
|
This code was generated with the assistance of **Gemini (Google AI)** to quickly prototype the WebRTC networking logic. The goal was to create a clean, readable foundation that is easy for others to merge or modify during the next phase of the experiment.
|
||||||
- **[Automerge](https://automerge.org/)** via `@automerge/vanillajs` — a CRDT library that lets multiple clients concurrently edit shared documents and merge changes without conflicts.
|
|
||||||
- **BroadcastChannelNetworkAdapter** — syncs between tabs in the same browser instantly via the browser's BroadcastChannel API.
|
|
||||||
- **WebSocketClientAdapter** — syncs across different browsers/devices via a WebSocket relay (`wss://sync.automerge.org`). The relay only forwards CRDT messages; it does not store or interpret your data.
|
|
||||||
- **IndexedDBStorageAdapter** — persists the document locally so it survives page refreshes.
|
|
||||||
- **Vite** — bundles the app with WebAssembly support (required by Automerge).
|
|
||||||
|
|
||||||
### Data model
|
|
||||||
|
|
||||||
The poll is a single Automerge document with three fields:
|
|
||||||
|
|
||||||
```js
|
|
||||||
{
|
|
||||||
options: string[], // list of poll options in insertion order
|
|
||||||
votes: { [option]: number }, // vote count per option
|
|
||||||
deadline: number | null // Unix timestamp (ms) when voting closes, or null
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### URL-based sharing
|
|
||||||
|
|
||||||
Each document gets a unique Automerge URL (e.g. `automerge:abc123...`) stored in the page's URL hash. Opening the same URL in any browser loads and syncs the same document. Creating a new poll generates a new document and updates the hash automatically.
|
|
||||||
|
|
||||||
### Voting rules
|
|
||||||
|
|
||||||
- Voted options are tracked in `localStorage` (keyed by document URL), so they persist across page refreshes but are isolated per browser.
|
|
||||||
- One vote per option per user. Votes can be toggled off and back on until the timer expires.
|
|
||||||
- The timer (`doc.deadline`) is set once by whoever clicks "Start 2-min Vote" and is shared via the Automerge document, so all peers use the same deadline.
|
|
||||||
|
|
||||||
## Running locally
|
|
||||||
|
|
||||||
```bash
|
|
||||||
pnpm install
|
|
||||||
pnpm dev
|
|
||||||
```
|
|
||||||
|
|
||||||
Open the printed localhost URL. To test cross-browser sync, copy the URL (including the `#automerge:...` hash) and open it in a different browser.
|
|
||||||
214
app.js
214
app.js
@@ -1,214 +0,0 @@
|
|||||||
import {
|
|
||||||
Repo,
|
|
||||||
BroadcastChannelNetworkAdapter,
|
|
||||||
WebSocketClientAdapter,
|
|
||||||
IndexedDBStorageAdapter,
|
|
||||||
isValidAutomergeUrl,
|
|
||||||
} from "@automerge/vanillajs";
|
|
||||||
|
|
||||||
const REPO = new Repo({
|
|
||||||
network: [
|
|
||||||
new BroadcastChannelNetworkAdapter(),
|
|
||||||
new WebSocketClientAdapter("wss://sync.automerge.org"),
|
|
||||||
],
|
|
||||||
storage: new IndexedDBStorageAdapter(),
|
|
||||||
});
|
|
||||||
|
|
||||||
let HANDLE;
|
|
||||||
|
|
||||||
function getVotedKey(docUrl) {
|
|
||||||
return `p2p-poll-voted:${docUrl}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getVotedOptions() {
|
|
||||||
const key = getVotedKey(document.location.hash);
|
|
||||||
return new Set(JSON.parse(localStorage.getItem(key) || "[]"));
|
|
||||||
}
|
|
||||||
|
|
||||||
function markVoted(opt) {
|
|
||||||
const key = getVotedKey(document.location.hash);
|
|
||||||
const voted = getVotedOptions();
|
|
||||||
voted.add(opt);
|
|
||||||
localStorage.setItem(key, JSON.stringify([...voted]));
|
|
||||||
}
|
|
||||||
|
|
||||||
function unmarkVoted(opt) {
|
|
||||||
const key = getVotedKey(document.location.hash);
|
|
||||||
const voted = getVotedOptions();
|
|
||||||
voted.delete(opt);
|
|
||||||
localStorage.setItem(key, JSON.stringify([...voted]));
|
|
||||||
}
|
|
||||||
|
|
||||||
const locationHash = document.location.hash.substring(1);
|
|
||||||
const statusEl = document.getElementById("status");
|
|
||||||
const optionsList = document.getElementById("options-list");
|
|
||||||
const optionInput = document.getElementById("option-input");
|
|
||||||
const addBtn = document.getElementById("add-btn");
|
|
||||||
const duplicateMsg = document.getElementById("duplicate-msg");
|
|
||||||
const copyBtn = document.getElementById("copy-btn");
|
|
||||||
const timerEl = document.getElementById("timer");
|
|
||||||
const startVotingBtn = document.getElementById("start-voting-btn");
|
|
||||||
|
|
||||||
if (isValidAutomergeUrl(locationHash)) {
|
|
||||||
statusEl.textContent = "Loading document…";
|
|
||||||
try {
|
|
||||||
HANDLE = await REPO.find(locationHash);
|
|
||||||
statusEl.textContent = "";
|
|
||||||
} catch (err) {
|
|
||||||
statusEl.textContent = "Could not load document. Starting a new one.";
|
|
||||||
HANDLE = REPO.create({ options: [], votes: {}, deadline: null });
|
|
||||||
document.location.hash = HANDLE.url;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
HANDLE = REPO.create({ options: [], votes: {}, deadline: null });
|
|
||||||
document.location.hash = HANDLE.url;
|
|
||||||
}
|
|
||||||
|
|
||||||
HANDLE.on("change", ({ doc }) => render(doc));
|
|
||||||
|
|
||||||
const initialDoc = HANDLE.doc();
|
|
||||||
if (initialDoc) render(initialDoc);
|
|
||||||
|
|
||||||
let timerInterval = null;
|
|
||||||
|
|
||||||
function render(doc) {
|
|
||||||
renderOptions(doc);
|
|
||||||
renderTimer(doc);
|
|
||||||
}
|
|
||||||
|
|
||||||
function renderOptions(doc) {
|
|
||||||
const votes = doc.votes || {};
|
|
||||||
const deadline = doc.deadline ?? null;
|
|
||||||
const now = Date.now();
|
|
||||||
const votingClosed = deadline !== null && now >= deadline;
|
|
||||||
const votedOptions = getVotedOptions();
|
|
||||||
|
|
||||||
const sorted = [...(doc.options || [])].sort(
|
|
||||||
(a, b) => (votes[b] || 0) - (votes[a] || 0),
|
|
||||||
);
|
|
||||||
|
|
||||||
optionsList.innerHTML = "";
|
|
||||||
sorted.forEach((opt) => {
|
|
||||||
const count = votes[opt] || 0;
|
|
||||||
const alreadyVoted = votedOptions.has(opt);
|
|
||||||
|
|
||||||
const item = document.createElement("div");
|
|
||||||
item.className = "option-item";
|
|
||||||
|
|
||||||
const label = document.createElement("span");
|
|
||||||
label.className = "option-label";
|
|
||||||
label.textContent = opt;
|
|
||||||
|
|
||||||
const voteBtn = document.createElement("button");
|
|
||||||
voteBtn.className = alreadyVoted ? "vote-btn voted" : "vote-btn";
|
|
||||||
voteBtn.textContent = alreadyVoted ? `✓ ${count}` : `▲ ${count}`;
|
|
||||||
voteBtn.disabled = votingClosed;
|
|
||||||
if (votingClosed) voteBtn.title = "Voting is closed";
|
|
||||||
else if (alreadyVoted) voteBtn.title = "Click to remove your vote";
|
|
||||||
|
|
||||||
voteBtn.addEventListener("click", () => {
|
|
||||||
if (alreadyVoted) {
|
|
||||||
unmarkVoted(opt);
|
|
||||||
HANDLE.change((d) => {
|
|
||||||
if (!d.votes) d.votes = {};
|
|
||||||
d.votes[opt] = Math.max(0, (d.votes[opt] || 0) - 1);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
markVoted(opt);
|
|
||||||
HANDLE.change((d) => {
|
|
||||||
if (!d.votes) d.votes = {};
|
|
||||||
d.votes[opt] = (d.votes[opt] || 0) + 1;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
item.appendChild(label);
|
|
||||||
item.appendChild(voteBtn);
|
|
||||||
optionsList.appendChild(item);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function renderTimer(doc) {
|
|
||||||
const deadline = doc?.deadline ?? null;
|
|
||||||
const now = Date.now();
|
|
||||||
|
|
||||||
if (deadline === null) {
|
|
||||||
timerEl.textContent = "";
|
|
||||||
startVotingBtn.hidden = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
startVotingBtn.hidden = true;
|
|
||||||
|
|
||||||
if (now >= deadline) {
|
|
||||||
timerEl.textContent = "Voting closed.";
|
|
||||||
timerEl.className = "timer closed";
|
|
||||||
clearInterval(timerInterval);
|
|
||||||
timerInterval = null;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const remaining = Math.ceil((deadline - now) / 1000);
|
|
||||||
const mins = Math.floor(remaining / 60);
|
|
||||||
const secs = remaining % 60;
|
|
||||||
timerEl.textContent = `Voting closes in ${mins}:${secs.toString().padStart(2, "0")}`;
|
|
||||||
timerEl.className = "timer active";
|
|
||||||
|
|
||||||
if (!timerInterval) {
|
|
||||||
timerInterval = setInterval(() => {
|
|
||||||
const d = HANDLE.doc();
|
|
||||||
if (!d) return;
|
|
||||||
renderTimer(d);
|
|
||||||
renderOptions(d);
|
|
||||||
if (Date.now() >= d.deadline) {
|
|
||||||
clearInterval(timerInterval);
|
|
||||||
timerInterval = null;
|
|
||||||
}
|
|
||||||
}, 1000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
startVotingBtn.addEventListener("click", () => {
|
|
||||||
HANDLE.change((d) => {
|
|
||||||
d.deadline = Date.now() + 2 * 60 * 1000;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
addBtn.addEventListener("click", () => {
|
|
||||||
const value = optionInput.value.trim();
|
|
||||||
if (!value) return;
|
|
||||||
|
|
||||||
const doc = HANDLE.doc();
|
|
||||||
const valueLower = value.toLowerCase();
|
|
||||||
const isDuplicate = (doc?.options || []).some(
|
|
||||||
(opt) => opt.toLowerCase() === valueLower,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (isDuplicate) {
|
|
||||||
duplicateMsg.textContent = `"${value}" is already in the list.`;
|
|
||||||
duplicateMsg.hidden = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
duplicateMsg.hidden = true;
|
|
||||||
HANDLE.change((d) => {
|
|
||||||
if (!d.options) d.options = [];
|
|
||||||
d.options.push(value);
|
|
||||||
});
|
|
||||||
optionInput.value = "";
|
|
||||||
});
|
|
||||||
|
|
||||||
optionInput.addEventListener("keydown", (e) => {
|
|
||||||
if (e.key === "Enter") addBtn.click();
|
|
||||||
});
|
|
||||||
|
|
||||||
optionInput.addEventListener("input", () => {
|
|
||||||
duplicateMsg.hidden = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
copyBtn.addEventListener("click", () => {
|
|
||||||
navigator.clipboard.writeText(window.location.href).then(() => {
|
|
||||||
copyBtn.textContent = "Copied!";
|
|
||||||
setTimeout(() => (copyBtn.textContent = "Copy URL"), 1500);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
239
index.html
239
index.html
@@ -1,31 +1,212 @@
|
|||||||
<!doctype html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
||||||
<title>P2P Polling App</title>
|
<title>Mesh Governance Tool</title>
|
||||||
<link rel="stylesheet" href="./styles.css" />
|
<script src="https://unpkg.com/peerjs@1.5.2/dist/peerjs.min.js"></script>
|
||||||
</head>
|
<style>
|
||||||
<body>
|
:root { --primary: #007AFF; --bg: #f2f2f7; --success: #34c759; --secondary: #8e8e93; }
|
||||||
<div id="app">
|
* { box-sizing: border-box; }
|
||||||
<h1>P2P Polling App</h1>
|
body { font-family: -apple-system, sans-serif; margin: 0; background: var(--bg); height: 100vh; display: flex; flex-direction: column; overflow: hidden; }
|
||||||
<div id="status"></div>
|
|
||||||
<div id="add-option">
|
/* Header & Peer List */
|
||||||
<input
|
.header { background: #fff; padding: 12px; border-bottom: 1px solid #d1d1d6; flex-shrink: 0; }
|
||||||
type="text"
|
.setup-row { display: flex; gap: 8px; margin-bottom: 10px; align-items: center; }
|
||||||
id="option-input"
|
.peer-list-container { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
|
||||||
placeholder="Enter an option..."
|
.peer-list-container::-webkit-scrollbar { display: none; }
|
||||||
/>
|
.peer-chip { background: #e9ecef; padding: 4px 10px; border-radius: 12px; font-size: 11px; white-space: nowrap; border: 1px solid #dee2e6; display: flex; align-items: center; gap: 5px; }
|
||||||
<button id="add-btn">Add Option</button>
|
.dot { width: 6px; height: 6px; background: var(--success); border-radius: 50%; }
|
||||||
</div>
|
|
||||||
<div id="duplicate-msg" hidden></div>
|
/* Main Layout */
|
||||||
<div id="timer-section">
|
.main-container { display: flex; flex: 1; overflow: hidden; }
|
||||||
<div id="timer"></div>
|
@media (max-width: 768px) { .main-container { flex-direction: column; } }
|
||||||
<button id="start-voting-btn">Start 2-min Vote</button>
|
.column { flex: 1; display: flex; flex-direction: column; overflow: hidden; background: #fff; border-right: 1px solid #d1d1d6; }
|
||||||
</div>
|
.scroll-area { flex: 1; overflow-y: auto; padding: 16px; -webkit-overflow-scrolling: touch; }
|
||||||
<div id="options-list"></div>
|
|
||||||
<button id="copy-btn">Copy URL</button>
|
/* Components */
|
||||||
|
.card { background: #f9f9f9; border-radius: 10px; padding: 12px; margin-bottom: 15px; border: 1px solid #e5e5ea; }
|
||||||
|
input { border: 1px solid #c6c6c8; padding: 10px; border-radius: 8px; font-size: 16px; width: 100%; outline: none; }
|
||||||
|
button { background: var(--primary); color: white; border: none; padding: 10px; border-radius: 8px; font-weight: 600; cursor: pointer; }
|
||||||
|
|
||||||
|
/* Poll UI */
|
||||||
|
.poll-question { font-size: 20px; font-weight: 700; margin-bottom: 15px; color: var(--primary); }
|
||||||
|
.option-item { display: flex; justify-content: space-between; align-items: center; background: white; padding: 12px; border-radius: 10px; margin-bottom: 8px; border: 1px solid #e5e5ea; }
|
||||||
|
|
||||||
|
/* Chat UI */
|
||||||
|
#chat-messages { display: flex; flex-direction: column; gap: 8px; }
|
||||||
|
.msg { padding: 8px 12px; border-radius: 14px; max-width: 85%; font-size: 14px; }
|
||||||
|
.msg.sent { background: var(--primary); color: white; align-self: flex-end; }
|
||||||
|
.msg.received { background: #e5e5ea; color: black; align-self: flex-start; }
|
||||||
|
.msg-sender { font-size: 10px; font-weight: bold; opacity: 0.7; display: block; }
|
||||||
|
.chat-input-bar { padding: 12px; background: #fff; border-top: 1px solid #d1d1d6; display: flex; gap: 8px; padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div class="header">
|
||||||
|
<div class="setup-row">
|
||||||
|
<span style="font-size: 13px;">Me: <strong id="my-id-display">...</strong></span>
|
||||||
|
<div id="peer-list" class="peer-list-container">
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<script type="module" src="./app.js"></script>
|
<div class="setup-row">
|
||||||
</body>
|
<input type="text" id="nickname-input" placeholder="My Name" style="flex: 0.5;">
|
||||||
</html>
|
<button onclick="joinAs(document.getElementById('nickname-input').value)" style="background: var(--secondary); padding: 8px 12px;">Set</button>
|
||||||
|
<input type="text" id="peer-id-input" placeholder="Friend's Name" style="flex: 1;">
|
||||||
|
<button onclick="connectToPeer()" style="padding: 8px 12px;">Connect</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="main-container">
|
||||||
|
<div class="column">
|
||||||
|
<div class="scroll-area">
|
||||||
|
<div class="card">
|
||||||
|
<input type="text" id="poll-title-input" placeholder="Question/Topic?">
|
||||||
|
<button onclick="updateTitle()" style="width:100%; margin-top:8px;">Set Topic</button>
|
||||||
|
</div>
|
||||||
|
<div id="active-question" class="poll-question">Waiting for Topic...</div>
|
||||||
|
<div style="display:flex; gap:8px; margin-bottom:15px;">
|
||||||
|
<input type="text" id="new-option-text" placeholder="Add option...">
|
||||||
|
<button onclick="addOption()">Add</button>
|
||||||
|
</div>
|
||||||
|
<div id="poll-container"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="column">
|
||||||
|
<div class="scroll-area" id="chat-scroll">
|
||||||
|
<div id="chat-messages"></div>
|
||||||
|
</div>
|
||||||
|
<div class="chat-input-bar">
|
||||||
|
<input type="text" id="chat-input" placeholder="Discuss...">
|
||||||
|
<button onclick="sendMessage()">Send</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
let pollData = { question: "Open Discussion", options: [] };
|
||||||
|
let myName = "";
|
||||||
|
let connections = [];
|
||||||
|
let peer = null;
|
||||||
|
|
||||||
|
function joinAs(id) {
|
||||||
|
if (peer) peer.destroy();
|
||||||
|
peer = new Peer(id || undefined);
|
||||||
|
peer.on('open', newId => {
|
||||||
|
myName = newId;
|
||||||
|
document.getElementById('my-id-display').innerText = newId;
|
||||||
|
});
|
||||||
|
peer.on('connection', conn => setupConnection(conn));
|
||||||
|
}
|
||||||
|
|
||||||
|
function connectToPeer() {
|
||||||
|
const target = document.getElementById('peer-id-input').value;
|
||||||
|
if (target && target !== myName) {
|
||||||
|
setupConnection(peer.connect(target));
|
||||||
|
}
|
||||||
|
document.getElementById('peer-id-input').value = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
function setupConnection(conn) {
|
||||||
|
conn.on('open', () => {
|
||||||
|
// Prevent duplicate connections
|
||||||
|
if (!connections.find(c => c.peer === conn.peer)) {
|
||||||
|
connections.push(conn);
|
||||||
|
updatePeerUI();
|
||||||
|
broadcast({ type: 'SYNC', payload: pollData });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
conn.on('data', data => {
|
||||||
|
if (data.type === 'SYNC') {
|
||||||
|
pollData = data.payload;
|
||||||
|
renderPoll();
|
||||||
|
} else if (data.type === 'CHAT') {
|
||||||
|
appendMessage(data.sender, data.payload, 'received');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
conn.on('close', () => {
|
||||||
|
connections = connections.filter(c => c.peer !== conn.peer);
|
||||||
|
updatePeerUI();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function broadcast(data) {
|
||||||
|
connections.forEach(c => { if (c.open) c.send(data); });
|
||||||
|
}
|
||||||
|
|
||||||
|
function updatePeerUI() {
|
||||||
|
const list = document.getElementById('peer-list');
|
||||||
|
if (connections.length === 0) {
|
||||||
|
list.innerHTML = '<span style="font-size:11px; color:#999;">No peers connected</span>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
list.innerHTML = connections.map(c => `
|
||||||
|
<div class="peer-chip"><div class="dot"></div>${c.peer}</div>
|
||||||
|
`).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- APP LOGIC ---
|
||||||
|
function updateTitle() {
|
||||||
|
const val = document.getElementById('poll-title-input').value;
|
||||||
|
if (!val) return;
|
||||||
|
pollData.question = val;
|
||||||
|
renderPoll();
|
||||||
|
broadcast({ type: 'SYNC', payload: pollData });
|
||||||
|
}
|
||||||
|
|
||||||
|
function addOption() {
|
||||||
|
const txt = document.getElementById('new-option-text').value;
|
||||||
|
if (!txt) return;
|
||||||
|
pollData.options.push({ text: txt, votes: 0 });
|
||||||
|
document.getElementById('new-option-text').value = "";
|
||||||
|
renderPoll();
|
||||||
|
broadcast({ type: 'SYNC', payload: pollData });
|
||||||
|
}
|
||||||
|
|
||||||
|
function vote(i) {
|
||||||
|
pollData.options[i].votes++;
|
||||||
|
renderPoll();
|
||||||
|
broadcast({ type: 'SYNC', payload: pollData });
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderPoll() {
|
||||||
|
document.getElementById('active-question').innerText = pollData.question;
|
||||||
|
const container = document.getElementById('poll-container');
|
||||||
|
container.innerHTML = pollData.options.map((opt, i) => `
|
||||||
|
<div class="option-item">
|
||||||
|
<span>${opt.text}</span>
|
||||||
|
<div style="display:flex; align-items:center;">
|
||||||
|
<span style="font-weight:bold; color:var(--primary); margin-right:12px; font-size:18px;">${opt.votes}</span>
|
||||||
|
<button onclick="vote(${i})" style="background:var(--success); padding:5px 12px;">Vote</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendMessage() {
|
||||||
|
const input = document.getElementById('chat-input');
|
||||||
|
if (!input.value) return;
|
||||||
|
broadcast({ type: 'CHAT', sender: myName, payload: input.value });
|
||||||
|
appendMessage('You', input.value, 'sent');
|
||||||
|
input.value = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
function appendMessage(sender, text, side) {
|
||||||
|
const div = document.createElement('div');
|
||||||
|
div.className = `msg ${side}`;
|
||||||
|
div.innerHTML = `<span class="msg-sender">${sender}</span>${text}`;
|
||||||
|
document.getElementById('chat-messages').appendChild(div);
|
||||||
|
const scroll = document.getElementById('chat-scroll');
|
||||||
|
scroll.scrollTop = scroll.scrollHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
document.getElementById('chat-input').addEventListener('keypress', (e) => { if (e.key === 'Enter') sendMessage(); });
|
||||||
|
|
||||||
|
joinAs(null);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
23
package.json
23
package.json
@@ -1,23 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "p2p-poll-app",
|
|
||||||
"type": "module",
|
|
||||||
"engines": {
|
|
||||||
"node": ">=24"
|
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"dev": "vite",
|
|
||||||
"build": "vite build",
|
|
||||||
"preview": "vite preview"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"@automerge/automerge-repo": "^2.0.0",
|
|
||||||
"@automerge/automerge-repo-network-websocket": "^2.0.0",
|
|
||||||
"@automerge/automerge-repo-storage-indexeddb": "^2.0.0",
|
|
||||||
"@automerge/vanillajs": "^2.5.3"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"vite": "^6.0.0",
|
|
||||||
"vite-plugin-top-level-await": "^1.4.0",
|
|
||||||
"vite-plugin-wasm": "^3.3.0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
1083
pnpm-lock.yaml
generated
1083
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
93
styles.css
93
styles.css
@@ -1,93 +0,0 @@
|
|||||||
body {
|
|
||||||
font-family: sans-serif;
|
|
||||||
max-width: 480px;
|
|
||||||
margin: 40px auto;
|
|
||||||
padding: 0 16px;
|
|
||||||
}
|
|
||||||
|
|
||||||
#add-option {
|
|
||||||
display: flex;
|
|
||||||
gap: 8px;
|
|
||||||
margin-bottom: 8px;
|
|
||||||
}
|
|
||||||
|
|
||||||
#option-input {
|
|
||||||
flex: 1;
|
|
||||||
padding: 8px;
|
|
||||||
font-size: 1rem;
|
|
||||||
border: 1px solid #ccc;
|
|
||||||
border-radius: 4px;
|
|
||||||
}
|
|
||||||
|
|
||||||
#duplicate-msg {
|
|
||||||
color: #c00;
|
|
||||||
font-size: 0.9rem;
|
|
||||||
margin-bottom: 12px;
|
|
||||||
}
|
|
||||||
|
|
||||||
button {
|
|
||||||
padding: 8px 16px;
|
|
||||||
font-size: 1rem;
|
|
||||||
cursor: pointer;
|
|
||||||
border: 1px solid #ccc;
|
|
||||||
border-radius: 4px;
|
|
||||||
background: #fff;
|
|
||||||
}
|
|
||||||
|
|
||||||
button:hover:not(:disabled) {
|
|
||||||
background: #f0f0f0;
|
|
||||||
}
|
|
||||||
|
|
||||||
button:disabled {
|
|
||||||
opacity: 0.5;
|
|
||||||
cursor: default;
|
|
||||||
}
|
|
||||||
|
|
||||||
#timer-section {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 12px;
|
|
||||||
margin-bottom: 16px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.timer.active {
|
|
||||||
font-weight: bold;
|
|
||||||
color: #1a6e1a;
|
|
||||||
}
|
|
||||||
|
|
||||||
.timer.closed {
|
|
||||||
font-weight: bold;
|
|
||||||
color: #c00;
|
|
||||||
}
|
|
||||||
|
|
||||||
.option-item {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: space-between;
|
|
||||||
padding: 10px 12px;
|
|
||||||
border: 1px solid #ddd;
|
|
||||||
border-radius: 4px;
|
|
||||||
margin-bottom: 8px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.option-label {
|
|
||||||
flex: 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
.vote-btn {
|
|
||||||
padding: 4px 12px;
|
|
||||||
font-size: 0.9rem;
|
|
||||||
white-space: nowrap;
|
|
||||||
}
|
|
||||||
|
|
||||||
.vote-btn.voted {
|
|
||||||
background: #e6f4e6;
|
|
||||||
border-color: #4a9e4a;
|
|
||||||
color: #2a6e2a;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#copy-btn {
|
|
||||||
margin-top: 16px;
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
import { defineConfig } from 'vite'
|
|
||||||
import wasm from 'vite-plugin-wasm'
|
|
||||||
import topLevelAwait from 'vite-plugin-top-level-await'
|
|
||||||
|
|
||||||
export default defineConfig({
|
|
||||||
plugins: [wasm(), topLevelAwait()]
|
|
||||||
})
|
|
||||||
Reference in New Issue
Block a user