improve: update commentvote

This commit is contained in:
Xylight committed 2024-02-26 17:03:36 -08:00
1 parent ee9dbf1071
commit d3168c1d1d
6 files changed
+82 -120

No files matched your search

@@ -101,7 +101,7 @@
<span class:font-bold={op} class="flex flex-row gap-1 items-center">
<UserLink
inComment
avatarSize={20}
avatarSize={22}
avatar
user={node.comment_view.creator}
/>
@@ -109,20 +109,10 @@
<span class="text-sky-500">OP</span>
{/if}
</span>
<span class="text-slate-600 dark:text-zinc-400 flex flex-row gap-1">
<RelativeDate
date={publishedToDate(node.comment_view.comment.published)}
/>
<span>•</span>
<span>
{Math.floor(
(node.comment_view.counts.upvotes /
(node.comment_view.counts.upvotes +
node.comment_view.counts.downvotes)) *
100
) || 0}%
</span>
</span>
<RelativeDate
class="text-zinc-400"
date={publishedToDate(node.comment_view.comment.published)}
/>
<span class="text-slate-600 dark:text-zinc-400 flex flex-row gap-2 ml-1">
{#if node.comment_view.comment.updated}
<Icon src={Pencil} solid size="12" title="Edited" />
@@ -34,7 +34,8 @@
<div class="flex flex-row items-center gap-0.5 h-7 relative">
<CommentVote
bind:score={comment.counts.score}
bind:upvotes={comment.counts.upvotes}
bind:downvotes={comment.counts.downvotes}
bind:vote={comment.my_vote}
commentId={comment.comment.id}
/>
@@ -93,7 +94,7 @@
<Icon src={comment.saved ? BookmarkSlash : Bookmark} mini size="16" />
<span>{comment.saved ? 'Unsave' : 'Save'}</span>
</MenuButton>
{#if $profile?.user && $profile.jwt && isCommentMutable(comment, $profile.user.local_user_view)}
{#if $profile?.user && $profile.jwt && $profile.user.local_user_view.person.id == comment.creator.id}
<MenuButton
color="danger-subtle"
on:click={async () => {
@@ -12,97 +12,69 @@
import { userSettings } from '$lib/settings'
import { profile } from '$lib/auth.js'
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
import { Button } from 'mono-svelte'
import { Button, buttonColor, toast } from 'mono-svelte'
import { vote as voteItem } from '$lib/lemmy/contentview'
import { shouldShowVoteColor } from '../post/PostVote.svelte'
import { fly } from 'svelte/transition'
export let vote: number = 0
export let score: number
export let upvotes: number
export let downvotes: number
export let commentId: number
async function upvote() {
if (!$profile?.jwt) return
const upvoted = vote == 1
if (vote == -1) {
score += 2
} else if (vote == 1) {
score -= 1
} else if (vote == 0) {
score += 1
const castVote = async (newVote: number) => {
if (!$profile?.jwt) {
toast({ content: 'You must be logged in to vote.', type: 'warning' })
return
}
vote = Number(!upvoted)
await getClient()
.likeComment({
score: upvoted ? 0 : 1,
comment_id: commentId,
})
.catch((_) => undefined)
vote = newVote
const res = await getClient().likeComment({
comment_id: commentId,
score: vote,
})
;({ upvotes, downvotes } = res.comment_view.counts)
}
async function downvote() {
if (!$profile?.jwt) return
const upvoted = vote == -1
if (vote == -1) {
score += 1
} else if (vote == 1) {
score -= 2
} else if (vote == 0) {
score -= 1
}
vote = -Number(!upvoted)
await getClient()
.likeComment({
score: upvoted ? 0 : -1,
comment_id: commentId,
})
.catch((_) => undefined)
}
const voteColor = (vote: number) => `
${vote == 1 ? '!text-blue-500' : vote == -1 ? '!text-red-500' : ''}
`
</script>
<div
class="flex flex-row items-center rounded-md transition-all
cursor-pointer h-full border-slate-200
dark:border-zinc-800 gap-0.5"
>
<Button
on:click={upvote}
class=" transition-colors hover:dark:text-blue-500 hover:text-blue-600 {vote ==
1
? 'dark:!text-blue-500 !text-blue-600'
: ''}"
aria-label="Upvote"
size="custom"
color="tertiary"
<div class="rounded-lg h-full flex items-center [&>*]:p-2 overflow-hidden">
<button
on:click={() => castVote(vote == 1 ? 0 : 1)}
class="flex items-center gap-0.5 {buttonColor.tertiary} transition-colors border-0 !pl-2
{vote == 1 ? shouldShowVoteColor(vote, 'upvotes') : ''}"
>
<Icon src={ChevronUp} size="20" mini />
</Button>
<span
class="text-sm font-medium transition-colors {voteColor(vote)}"
class:hidden={$profile?.user?.local_user_view.local_user.show_scores ==
false}
<Icon src={ChevronUp} size="18" mini />
<span class="grid text-sm">
{#key upvotes}
<span
style="grid-column: 1; grid-row: 1;"
in:fly={{ duration: 200, y: -6 }}
out:fly={{ duration: 200, y: 6 }}
>
<FormattedNumber number={upvotes} />
</span>
{/key}
</span>
</button>
<div
class="border-l h-6 w-0 !p-0 border-slate-200 dark:border-zinc-800"
></div>
<button
on:click={() => castVote(vote == -1 ? 0 : -1)}
class="flex items-center gap-0.5 {buttonColor.tertiary} transition-colors border-0 !pr-2
{vote == -1 ? shouldShowVoteColor(vote, 'downvotes') : ''}"
>
<FormattedNumber number={score} />
</span>
<Button
on:click={downvote}
class=" hover:dark:text-red-500 hover:text-red-600 {vote == -1
? '!text-red-500 !dark:text-red-600'
: ''} transition-colors disabled:pointer-events-none disabled:opacity-50"
aria-label="Downvote"
disabled={$site?.site_view.local_site.enable_downvotes == false}
size="custom"
color="tertiary"
>
<Icon src={ChevronDown} size="20" mini />
</Button>
<Icon src={ChevronDown} size="18" mini />
<span class="grid text-sm">
{#key downvotes}
<span
style="grid-column: 1; grid-row: 1;"
in:fly={{ duration: 200, y: -6 }}
out:fly={{ duration: 200, y: 6 }}
>
<FormattedNumber number={downvotes} />
</span>
{/key}
</span>
</button>
</div>
+14 -14
View File
@@ -1,3 +1,17 @@
<script lang="ts" context="module">
import { userSettings } from '$lib/settings.js'
export const voteColor = (vote: number, border: boolean = false) =>
vote == 1 ? `!text-blue-500` : vote == -1 ? `!text-red-500` : ''
export const shouldShowVoteColor = (
vote: number,
type: 'upvotes' | 'downvotes'
): string =>
(vote == -1 && type == 'downvotes') || (vote == 1 && type == 'upvotes')
? voteColor(vote)
: ''
</script>
<script lang="ts">
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
import type { Post } from 'lemmy-js-client'
@@ -14,7 +28,6 @@
import { Button, Popover, buttonColor, toast } from 'mono-svelte'
import { site } from '$lib/lemmy.js'
import { fly } from 'svelte/transition'
import { userSettings } from '$lib/settings.js'
export let post: Post
export let vote: number = 0
@@ -24,19 +37,6 @@
let oldScore = score
const voteColor = (vote: number, border: boolean = false) =>
vote == 1 ? `!text-blue-500` : vote == -1 ? `!text-red-500` : ''
const shouldShowVoteColor = (
vote: number,
type: 'upvotes' | 'downvotes'
): string =>
(vote == -1 && type == 'downvotes') ||
(vote == 1 && type == 'upvotes') ||
!$userSettings.separateVotes
? voteColor(vote)
: ''
const castVote = async (newVote: number) => {
if (!$profile?.jwt) {
toast({ content: 'You must be logged in to vote.', type: 'warning' })
+1 -1
View File
@@ -53,6 +53,6 @@
}
</script>
<time datetime={dateTime} title={dateTime}>
<time datetime={dateTime} title={dateTime} class={$$props.class ?? ''}>
{formatRelativeDate(date)}
</time>