From d6d1a9fe1fec32b6ab024b7593c680c006a1b6e1 Mon Sep 17 00:00:00 2001 From: James Xylight Date: Thu, 31 Jul 2025 23:05:59 -0700 Subject: [PATCH] feat: add vote ratio bar --- .../lemmy/comment/CommentTree.svelte | 2 +- .../lemmy/comment/CommentVote.svelte | 26 ++++++++++++++++++- src/lib/components/lemmy/post/PostVote.svelte | 25 +++++++++++++++++- src/lib/i18n/en.json | 4 +++ src/lib/settings.svelte.ts | 2 ++ src/routes/settings/+page.svelte | 7 +++++ 6 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/lib/components/lemmy/comment/CommentTree.svelte b/src/lib/components/lemmy/comment/CommentTree.svelte index 16f62a14..edf74ce2 100644 --- a/src/lib/components/lemmy/comment/CommentTree.svelte +++ b/src/lib/components/lemmy/comment/CommentTree.svelte @@ -96,7 +96,7 @@ contentClass={[ (node.children.length > 0 || node.comment_view.counts.child_count > 0) && - 'border-l-2', + 'border-l', 'ml-2.5 pl-3 sm:pl-4 lg:pl-5', node.depth % 2 == 0 ? 'border-slate-200 dark:border-zinc-800' diff --git a/src/lib/components/lemmy/comment/CommentVote.svelte b/src/lib/components/lemmy/comment/CommentVote.svelte index 6e0041a3..194fa3b6 100644 --- a/src/lib/components/lemmy/comment/CommentVote.svelte +++ b/src/lib/components/lemmy/comment/CommentVote.svelte @@ -8,6 +8,7 @@ import { backOut } from 'svelte/easing' import { fly } from 'svelte/transition' import { shouldShowVoteColor } from '../post/PostVote.svelte' + import { settings } from '$lib/settings.svelte' interface Props { vote?: number @@ -36,13 +37,19 @@ }) ;({ upvotes, downvotes } = res.comment_view.counts) } + + let voteRatio = $derived( + Math.floor(((upvotes ?? 0) / ((upvotes ?? 0) + (downvotes ?? 0))) * 100), + )
+ + diff --git a/src/lib/components/lemmy/post/PostVote.svelte b/src/lib/components/lemmy/post/PostVote.svelte index 632b34e8..e26bb507 100644 --- a/src/lib/components/lemmy/post/PostVote.svelte +++ b/src/lib/components/lemmy/post/PostVote.svelte @@ -22,6 +22,7 @@ import { site } from '$lib/lemmy.svelte.js' import { vote as voteItem } from '$lib/lemmy/contentview.js' import { errorMessage } from '$lib/lemmy/error' + import { settings } from '$lib/settings.svelte' import type { Post } from 'lemmy-js-client' import { buttonColor, toast } from 'mono-svelte' import { ChevronDown, ChevronUp, Icon } from 'svelte-hero-icons' @@ -67,16 +68,21 @@ {#if children}{@render children({ vote, score })}{:else} + {@const voteRatio = Math.floor( + ((upvotes ?? 0) / ((upvotes ?? 0) + (downvotes ?? 0))) * 100, + )}
{/if} + + diff --git a/src/lib/i18n/en.json b/src/lib/i18n/en.json index c1d3558d..dea0246f 100644 --- a/src/lib/i18n/en.json +++ b/src/lib/i18n/en.json @@ -850,6 +850,10 @@ "titleOpensUrl": { "title": "Title opens URL", "description": "Makes clicking the title open the post's attached URL, rather than the comments." + }, + "voteRatioBar": { + "title": "Vote ratio bar", + "description": "When the controversiality of a post reaches greater than 15%, a bar will be displayed in the vote buttons to help visualize sentiment." } }, "embeds": { diff --git a/src/lib/settings.svelte.ts b/src/lib/settings.svelte.ts index 1a109e77..98df5f40 100644 --- a/src/lib/settings.svelte.ts +++ b/src/lib/settings.svelte.ts @@ -104,6 +104,7 @@ interface Settings { messages: { fullMarkdown: boolean } + voteRatioBar: boolean } export const defaultSettings: Settings = { @@ -184,6 +185,7 @@ export const defaultSettings: Settings = { messages: { fullMarkdown: toBool(env.PUBLIC_FULL_MARKDOWN) ?? false, }, + voteRatioBar: true, } function createSettingsState(initial: Settings): Settings { diff --git a/src/routes/settings/+page.svelte b/src/routes/settings/+page.svelte index 7579c224..39201000 100644 --- a/src/routes/settings/+page.svelte +++ b/src/routes/settings/+page.svelte @@ -29,6 +29,7 @@ ArrowRight, ArrowsPointingOut, ArrowsRightLeft, + ArrowsUpDown, ArrowTopRightOnSquare, ArrowTrendingDown, ArrowUpTray, @@ -402,6 +403,12 @@ title={$t('settings.app.absoluteDates.title')} description={$t('settings.app.absoluteDates.description')} /> +