Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 | 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 2x 2x 2x 2x 2x 9x 1x 1x 1x 1x 1x 9x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 27x 27x 27x 27x 27x 9x 9x 9x 9x 17x 17x 17x 17x 17x 17x 17x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 9x 9x 9x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x | 'use client'
import Link from 'next/link'
import {useRouter} from 'next/navigation'
import {useEffect, useState} from 'react'
import type {Board, BoardProposal} from '@/lib/proposals'
import {shortDate} from '@/lib/dates'
import Dropdown from '@/components/Dropdown'
import Avatar from '@/components/Avatar'
export default function Proposals({
board,
isAdmin,
tributeTo,
}: {
board: Board
isAdmin: boolean
/** The act a tribute band covers: a proposal with no artist is theirs */
tributeTo: string | null
}) {
const router = useRouter()
const [proposing, setProposing] = useState(false)
const [busy, setBusy] = useState<string | null>(null)
const [showArchived, setShowArchived] = useState(false)
// Someone else voting or proposing updates this page live
useEffect(() => {
const es = new EventSource('/api/stream')
es.onmessage = () => router.refresh()
return () => es.close()
}, [router])
async function act(id: string, run: () => Promise<Response>) {
setBusy(id)
await run()
setBusy(null)
router.refresh()
}
const vote = (p: BoardProposal) =>
act(p.id, () =>
fetch(`/api/proposals/${p.id}/vote`, {
method: p.mine ? 'DELETE' : 'POST',
}),
)
const setStatus = (id: string, status: 'APPROVED' | 'ARCHIVED' | 'PENDING') =>
act(id, () =>
fetch(`/api/proposals/${id}`, {
method: 'PATCH',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({status}),
}),
)
const remove = (p: {id: string; title: string}) =>
window.confirm(`Delete the proposal “${p.title}” and its votes?`) &&
act(p.id, () => fetch(`/api/proposals/${p.id}`, {method: 'DELETE'}))
return (
<main className="mx-auto max-w-3xl px-4 pb-12 pt-5">
<div className="flex items-end justify-between gap-3">
<div>
<h1 className="text-3xl font-extrabold">Proposals</h1>
<p className="mt-1 text-muted">
Songs someone wants to add. At {board.threshold} vote
{board.threshold === 1 ? '' : 's'} a song joins the book to learn.
</p>
</div>
<button
type="button"
onClick={() => setProposing(!proposing)}
aria-expanded={proposing}
className="min-h-11 shrink-0 rounded-lg bg-accent px-4 font-bold text-on-accent"
>
Propose a song
</button>
</div>
{proposing && (
<ProposeForm
tributeTo={tributeTo}
onDone={() => {
setProposing(false)
router.refresh()
}}
/>
)}
<section aria-labelledby="open-h" className="mt-6">
<h2
id="open-h"
className="text-xs font-bold uppercase tracking-widest text-muted"
>
Open · {board.pending.length}
</h2>
{!board.pending.length && (
<p className="py-8 text-center text-muted">
Nothing open. Got an idea?
</p>
)}
<ul>
{board.pending.map((p) => (
<li
key={p.id}
className="border-t border-line py-4 first:border-t-0"
>
<ProposalRow
p={p}
threshold={board.threshold}
busy={busy === p.id}
isAdmin={isAdmin}
onVote={() => vote(p)}
onApprove={() => setStatus(p.id, 'APPROVED')}
onArchive={() => setStatus(p.id, 'ARCHIVED')}
onDelete={() => remove(p)}
onSaved={() => router.refresh()}
/>
</li>
))}
</ul>
</section>
{board.approved.length > 0 && (
<section aria-labelledby="in-h" className="mt-8">
<h2
id="in-h"
className="text-xs font-bold uppercase tracking-widest text-muted"
>
Recently voted in
</h2>
<ul className="mt-2">
{board.approved.map((a) => (
<li
key={a.id}
className="flex min-h-12 items-center gap-3 border-t border-line"
>
<span
aria-hidden="true"
className="h-2.5 w-2.5 rounded-full border-2 border-muted"
/>
<span className="flex-1">
<strong>{a.title}</strong>{' '}
<span className="text-muted">· {a.artist}</span>
</span>
<span className="text-sm text-faint">
{shortDate(a.approvedAt)}
</span>
{a.songId && (
<Link
href={`/songs/${a.songId}`}
className="text-sm text-sky no-underline"
>
Chart ›
</Link>
)}
</li>
))}
</ul>
</section>
)}
{board.archived.length > 0 && (
<section className="mt-8">
<button
type="button"
onClick={() => setShowArchived(!showArchived)}
aria-expanded={showArchived}
className="min-h-11 text-xs font-bold uppercase tracking-widest text-muted"
>
Archived · {board.archived.length} {showArchived ? '▾' : '▸'}
</button>
{showArchived && (
<ul>
{board.archived.map((a) => (
<li
key={a.id}
className="flex min-h-12 items-center gap-3 border-t border-line text-muted"
>
<span className="flex-1">
{a.title} · {a.artist}{' '}
<span className="text-faint">— {a.proposer}</span>
</span>
{isAdmin && (
<button
type="button"
onClick={() => setStatus(a.id, 'PENDING')}
className="min-h-11 px-2 text-sm underline"
>
Reopen
</button>
)}
</li>
))}
</ul>
)}
</section>
)}
</main>
)
}
function ProposalRow({
p,
threshold,
busy,
isAdmin,
onVote,
onApprove,
onArchive,
onDelete,
onSaved,
}: {
p: BoardProposal
threshold: number
busy: boolean
isAdmin: boolean
onVote: () => void
onApprove: () => void
onArchive: () => void
onDelete: () => void
onSaved: () => void
}) {
const [editing, setEditing] = useState(false)
const needed = Math.max(threshold - p.voters.length, 0)
if (editing)
return <EditForm p={p} onDone={() => (setEditing(false), onSaved())} />
return (
// The buttons always stay at the right; a long title wraps instead, so
// the row keeps its shape when "Vote" becomes the wider "Voted ✓"
<div className="flex items-start gap-3">
<div className="min-w-0 flex-1">
<p className="text-lg font-bold leading-snug">
{p.title} <span className="font-normal text-muted">· {p.artist}</span>
</p>
<p className="mt-0.5 flex flex-wrap items-center gap-x-1 text-[13px] text-faint">
<Avatar name={p.proposer} src={p.proposerAvatar} size={18} />
<span className="ml-0.5">{p.proposer}</span> ·{' '}
{shortDate(p.proposedAt)}
{p.youtubeUrl && (
<>
{' · '}
<a
href={p.youtubeUrl}
target="_blank"
rel="noreferrer"
className="text-sky no-underline"
>
Recording ↗
</a>
</>
)}
{p.lyricsUrl && (
<>
{' · '}
<a
href={p.lyricsUrl}
target="_blank"
rel="noreferrer"
className="text-sky no-underline"
>
Lyrics ↗
</a>
</>
)}
</p>
<div
className="mt-2 flex items-center gap-2"
aria-label={`${p.voters.length} of ${threshold} votes`}
>
{Array.from(
{length: Math.max(threshold, p.voters.length)},
(_, i) => (
<span
key={i}
aria-hidden="true"
className={`h-2 w-7 rounded-full ${i < p.voters.length ? 'bg-accent' : 'bg-line-2'}`}
/>
),
)}
{p.voters.length > 0 && (
<span className="ml-1 flex -space-x-1.5">
{p.voters.map((v, i) => (
<Avatar
key={i}
name={v.name}
src={v.avatar}
size={22}
className="ring-2 ring-ink"
/>
))}
</span>
)}
<span className="ml-1 text-[13px] text-muted">
{p.voters.length
? p.voters.map((v) => v.name).join(', ')
: 'No votes yet'}
{needed > 0 && ` · ${needed} more to add it`}
</span>
</div>
</div>
<div className="flex shrink-0 items-center gap-1">
<button
type="button"
onClick={onVote}
disabled={busy}
aria-pressed={p.mine}
className={`min-h-11 whitespace-nowrap rounded-lg px-4 font-bold disabled:opacity-50 ${p.mine ? 'bg-accent text-on-accent' : 'border border-line-2'}`}
>
{p.mine ? 'Voted ✓' : 'Vote'}
</button>
{isAdmin && (
<Dropdown
trigger="•••"
label={`More for ${p.title}`}
triggerClassName="flex min-h-11 items-center rounded-lg px-3 text-muted"
panelClassName="w-52"
>
<MenuButton onClick={onApprove}>Add to the book now</MenuButton>
<MenuButton onClick={() => setEditing(true)}>Edit</MenuButton>
<MenuButton onClick={onArchive}>Archive</MenuButton>
<MenuButton onClick={onDelete} danger>
Delete
</MenuButton>
</Dropdown>
)}
</div>
</div>
)
}
function MenuButton({
onClick,
children,
danger,
}: {
onClick: () => void
children: React.ReactNode
danger?: boolean
}) {
return (
<button
type="button"
onClick={onClick}
className={`block min-h-11 w-full rounded-lg px-3 text-left hover:bg-line ${danger ? 'text-bad' : ''}`}
>
{children}
</button>
)
}
function Field({
label,
value,
onChange,
type = 'text',
required = false,
}: {
label: string
value: string
onChange: (v: string) => void
type?: string
required?: boolean
}) {
return (
<label className="flex flex-col gap-1 text-sm text-muted">
{label}
<input
type={type}
required={required}
value={value}
onChange={(e) => onChange(e.target.value)}
className="min-h-11 rounded-lg border border-line-2 bg-ink px-3 text-base text-text"
/>
</label>
)
}
function ProposeForm({
onDone,
tributeTo,
}: {
onDone: () => void
tributeTo: string | null
}) {
const [title, setTitle] = useState('')
const [artist, setArtist] = useState('')
const [youtubeUrl, setYoutubeUrl] = useState('')
const [lyricsUrl, setLyricsUrl] = useState('')
const [error, setError] = useState<string | null>(null)
async function submit(e: React.FormEvent) {
e.preventDefault()
setError(null)
const r = await fetch('/api/proposals', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
title,
artist: artist || tributeTo || '',
youtubeUrl,
lyricsUrl,
}),
})
if (r.ok) return onDone()
setError(
r.status === 429
? 'That’s a lot of proposals — try again in an hour.'
: 'Check the links are full web addresses (https://…).',
)
}
return (
<form
onSubmit={submit}
className="mt-4 grid gap-3 rounded-xl border border-line-2 bg-panel p-4 sm:grid-cols-2"
>
<Field label="Song" value={title} onChange={setTitle} required />
<Field
label={
tributeTo ? `Originally by (blank = ${tributeTo})` : 'Originally by'
}
value={artist}
onChange={setArtist}
required={!tributeTo}
/>
<Field
label="Recording link (optional)"
value={youtubeUrl}
onChange={setYoutubeUrl}
type="url"
/>
<Field
label="Lyrics link (optional)"
value={lyricsUrl}
onChange={setLyricsUrl}
type="url"
/>
<button
type="submit"
className="min-h-11 rounded-lg bg-accent font-bold text-on-accent sm:col-span-2"
>
Propose it
</button>
{error && (
<p role="alert" className="text-sm text-bad sm:col-span-2">
{error}
</p>
)}
</form>
)
}
function EditForm({p, onDone}: {p: BoardProposal; onDone: () => void}) {
const [title, setTitle] = useState(p.title)
const [artist, setArtist] = useState(p.artist)
const [youtubeUrl, setYoutubeUrl] = useState(p.youtubeUrl ?? '')
const [error, setError] = useState<string | null>(null)
async function submit(e: React.FormEvent) {
e.preventDefault()
const r = await fetch(`/api/proposals/${p.id}`, {
method: 'PATCH',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({title, artist, youtubeUrl: youtubeUrl || null}),
})
if (r.ok) onDone()
else setError('Couldn’t save — check the link.')
}
return (
<form
onSubmit={submit}
className="grid gap-3 rounded-xl border border-line-2 bg-panel p-4 sm:grid-cols-3"
>
<Field label="Song" value={title} onChange={setTitle} required />
<Field
label="Originally by"
value={artist}
onChange={setArtist}
required
/>
<Field
label="Recording link"
value={youtubeUrl}
onChange={setYoutubeUrl}
type="url"
/>
<div className="flex gap-2 sm:col-span-3">
<button
type="submit"
className="min-h-11 rounded-lg bg-accent px-4 font-bold text-on-accent"
>
Save
</button>
<button
type="button"
onClick={onDone}
className="min-h-11 px-3 text-muted"
>
Cancel
</button>
</div>
{error && (
<p role="alert" className="text-sm text-bad sm:col-span-3">
{error}
</p>
)}
</form>
)
}
|