impr: optimistically update votes

This commit is contained in:
James Xylight committed 2025-08-07 16:10:54 -07:00
1 parent 92878e195c
commit 2e95ca4544
3 files changed
+35 -14

No files matched your search

+29 -11
View File
@@ -1,9 +1,9 @@
<script lang="ts" module>
export const voteColor = (vote: number) =>
vote == 1
? `bg-linear-to-br from-blue-500 to-indigo-500 dark:from-blue-400 dark:to-indigo-500 text-slate-50 dark:text-zinc-900`
? `bg-primary-900 dark:bg-primary-100 text-slate-50 dark:text-zinc-900`
: vote == -1
? `bg-linear-to-br from-red-500 to-pink-500 dark:from-red-400 dark:to-pink-600 text-slate-50 `
? `dark:bg-red-400 dark:text-zinc-900`
: ''
export const shouldShowVoteColor = (
@@ -33,8 +33,8 @@
post: Post
vote?: number
score: number
upvotes?: number
downvotes?: number
upvotes: number
downvotes: number
showCounts?: boolean
children?: import('svelte').Snippet<[{ vote?: number; score?: number }]>
}
@@ -56,14 +56,32 @@
toast({ content: $t('toast.loginVoteGate'), type: 'warning' })
return
}
loading = true
switch (vote ?? 0) {
case 0:
// nothing was removed
if (newVote == 1) upvotes++
else downvotes++
break
case 1:
// removed an upvote
upvotes--
if (newVote == -1) downvotes++
break
case -1:
// removed a downvote
downvotes--
if (newVote == 1) upvotes++
break
}
vote = newVote
const res = await voteItem(post, newVote).catch(e => {
toast({ content: errorMessage(e), type: 'error' })
return { upvotes: 0, downvotes: 0, score: 0 }
})
;({ upvotes, downvotes, score } = res)
loading = false
voteItem(post, newVote)
.then(res => ({ upvotes, downvotes, score } = res))
.catch(e => {
toast({ content: errorMessage(e), type: 'error' })
})
}
</script>
@@ -76,7 +76,10 @@
{/if}
<footer
class="flex flex-row gap-2 items-center shrink-0 {buttonHeight}"
class={[
'flex flex-row gap-2 items-center shrink-0 text-slate-600 dark:text-zinc-400',
buttonHeight,
]}
class:flex-row-reverse={settings.posts.reverseActions}
{style}
>
+2 -2
View File
@@ -14,8 +14,8 @@
dark:bg-primary-100 dark:text-black hover:brightness-125 dark:hover:brightness-90 active:brightness-90
dark:active:brightness-75`,
secondary: `border border-slate-200 border-b-slate-300 dark:border-zinc-800 dark:border-t-zinc-700/50 bg-white dark:bg-zinc-900
hover:bg-slate-100 dark:hover:bg-zinc-800 dark:hover:border-zinc-700 dark:hover:border-zinc-700 dark:active:bg-zinc-900 active:bg-slate-200`,
secondary: `border border-slate-200 border-b-slate-300 dark:border-zinc-800 bg-white dark:bg-zinc-900
hover:bg-slate-100 dark:hover:bg-zinc-800 dark:hover:border-zinc-700 dark:active:bg-zinc-900 active:bg-slate-200`,
tertiary:
'border border-transparent bg-transparent hover:bg-slate-100 dark:hover:bg-zinc-700/30 dark:text-zinc-200',