test
deploy / deploy (push) Successful in 40s

This commit is contained in:
Romain committed 2026-10-08 18:27:49 +02:00
1 parent d700543eba
commit 9612a7bfb2
7 files changed
+174 -57

No files matched your search

+71
View File
@@ -0,0 +1,71 @@
import { useState } from 'react'
import { REACTION_EMOJIS, reactToReview, unreactReview, type ReactionEmoji } from '../../lib/cloud/reviews'
import { cx } from '../../lib/utils'
/** Barre de réactions (emoji) sur un avis. Une seule réaction par personne ; retoucher la sienne l'enlève. */
export default function Reactions({
authorId,
itemId,
reactions: initial,
mine: initialMine,
}: {
authorId: string
itemId: string
reactions: Record<string, number>
mine: string | null
}) {
const [reactions, setReactions] = useState<Record<string, number>>(initial)
const [mine, setMine] = useState<string | null>(initialMine)
const [busy, setBusy] = useState(false)
const toggle = async (emoji: ReactionEmoji) => {
if (busy) return
const prev = { reactions, mine }
const next: Record<string, number> = { ...reactions }
if (mine === emoji) {
next[emoji] = Math.max(0, (next[emoji] ?? 0) - 1)
setReactions(next)
setMine(null)
} else {
if (mine) next[mine] = Math.max(0, (next[mine] ?? 0) - 1)
next[emoji] = (next[emoji] ?? 0) + 1
setReactions(next)
setMine(emoji)
}
setBusy(true)
try {
if (prev.mine === emoji) await unreactReview(authorId, itemId)
else await reactToReview(authorId, itemId, emoji)
} catch {
setReactions(prev.reactions)
setMine(prev.mine)
} finally {
setBusy(false)
}
}
return (
<div className="flex flex-wrap gap-1.5">
{REACTION_EMOJIS.map((emoji) => {
const count = reactions[emoji] ?? 0
const on = mine === emoji
return (
<button
key={emoji}
type="button"
onClick={() => void toggle(emoji)}
disabled={busy}
aria-pressed={on}
className={cx(
'inline-flex items-center gap-1 rounded-full border px-2.5 py-1 text-sm transition-colors active:scale-95 disabled:opacity-60',
on ? 'border-accent bg-accent-fill/10 text-ink' : 'border-line text-ink-2',
)}
>
<span>{emoji}</span>
{count > 0 && <span className="text-xs font-medium">{count}</span>}
</button>
)
})}
</div>
)
}