feat [1/3]: add poll UI

This commit is contained in:
Xylight committed 2025-11-13 22:09:48 -08:00
1 parent 1c75a5248e
commit edb47b586f
9 files changed
+974 -97

No files matched your search

+1 -1
View File
@@ -1,4 +1,3 @@
import { Post } from '$lib/feature/post'
import { Search } from 'mono-svelte'
import type {
CommentReplyView,
@@ -19,6 +18,7 @@ import type {
Person,
PersonMentionView,
PersonView,
Post,
PostView,
PrivateMessageView,
SortType,
+801 -78
View File
File diff suppressed because it is too large. Load diff
+51 -14
View File
@@ -1950,6 +1950,8 @@ export interface Post {
featured_local: boolean
url_content_type?: string
alt_text?: string
event?: PostEvent
poll?: PostPoll
}
export interface PostAggregates {
@@ -2301,6 +2303,25 @@ export interface VoteView {
score: number
}
export interface SearchResponse {
type_: SearchType
comments: Array<CommentView>
posts: Array<PostView>
communities: Array<CommunityView>
users: Array<PersonView>
}
export interface SiteResponse {
site_view: SiteView
taglines: Array<Tagline>
}
export interface SuccessResponse {
success: boolean
}
export interface UpdateTotpResponse {
enabled: boolean
}
export interface UploadImage {
image: File | Buffer
}
@@ -2395,21 +2416,37 @@ export interface AssignFlair {
flair_id_list: number[]
}
export interface SearchResponse {
type_: SearchType
comments: Array<CommentView>
posts: Array<PostView>
communities: Array<CommunityView>
users: Array<PersonView>
export interface PostPoll {
end_poll?: string
mode: string
local_only: boolean
latest_vote?: string
choices: PollChoice[]
}
export interface SiteResponse {
site_view: SiteView
taglines: Array<Tagline>
export interface PollChoice {
id: number
choice_text: string
sort_order: number
num_votes: number
}
export interface SuccessResponse {
success: boolean
}
export interface UpdateTotpResponse {
enabled: boolean
export interface PostEvent {
start: string
end?: string
timezone?: string
max_attendees: number
participant_count: number
full: boolean
online_link?: string
join_mode?: string
external_participation_url?: string
anonymous_participation: boolean
online: boolean
buy_tickets_link?: string
event_fee_currency?: string
event_fee_amount: number
location?: {
[key: string]: unknown
}
}
+3
View File
@@ -579,6 +579,9 @@
"meta": {
"lastEdited": "Last edited {{default}}",
"created": "Created {{default}}"
},
"poll": {
"votes": "{{votes:number}} votes"
}
},
"comment": {
+1 -1
View File
@@ -66,7 +66,7 @@
],
}
})
let type = $derived(mediaType(post.post.url))
let type = $derived(mediaType(post.post))
let rule = $derived(getTagRule(tags.tags))
let hideTitle = $derived(
settings.posts.deduplicateEmbed &&
+20 -2
View File
@@ -70,11 +70,29 @@ export function postLink(post: Post) {
return `/post/${encodeURIComponent(profile.current.instance)}/${post.id}`
}
export type MediaType = 'video' | 'image' | 'iframe' | 'embed' | 'none'
export type MediaType =
| 'video'
| 'image'
| 'iframe'
| 'embed'
| 'poll'
| 'event'
| 'none'
export type IframeType = 'youtube' | 'video' | 'none'
export function mediaType(url?: string): MediaType {
export function mediaType(post?: Post | string): MediaType {
if (!post) return 'none'
const isPost = typeof post != 'string'
const url = isPost ? post.url : post
if (isPost) {
if (post.poll) return 'poll'
if (post.event) return 'event'
}
if (!url) return 'none'
try {
new URL(url)
} catch {
@@ -3,6 +3,7 @@
import { settings, type View } from '$lib/app/settings.svelte'
import { PostIframe, PostImage, PostLink } from '..'
import { iframeType, type MediaType } from '../helpers'
import PostPoll from './PostPoll.svelte'
interface Props {
view?: View
@@ -49,4 +50,6 @@
{view}
{...rest}
/>
{:else if type == 'poll' && post.poll}
<PostPoll post={{ ...post, poll: post.poll! }} />
{/if}
@@ -0,0 +1,93 @@
<script lang="ts">
import type { Post, PostPoll } from '$lib/api/types'
import { t } from '$lib/app/i18n'
import { CommonList } from '$lib/ui/layout'
import EndPlaceholder from '$lib/ui/layout/EndPlaceholder.svelte'
import { publishedToDate } from '$lib/ui/util/date'
import RelativeDate from '$lib/ui/util/RelativeDate.svelte'
import { CheckBadge, Icon } from 'svelte-hero-icons/dist'
import { expoOut } from 'svelte/easing'
import { slide } from 'svelte/transition'
interface Props {
post: Post & { poll: PostPoll }
}
let { post }: Props = $props()
let chosen = $state<number>()
let totalVotes = $derived(
post.poll.choices.reduce((a, b) => a + b.num_votes, 0),
)
let selected = $state<number>()
// $effect used instead of $derived.by to emphasize
// that there is a side effect used (newChoice/prevChoice vote manipulation)
$effect(() => {
if (selected) {
castVote(selected)
}
})
async function castVote(choice: number) {
if (chosen == choice) return
const prevChoice = post.poll.choices.find((i) => i.id == chosen)
const newChoice = post.poll.choices.find((i) => i.id == choice)!
if (prevChoice) {
prevChoice.num_votes--
}
chosen = choice
newChoice.num_votes++
}
</script>
<form class="space-y-2">
<CommonList class="">
{#each post.poll.choices as choice}
<li class="relative z-10 overflow-hidden">
{#if chosen}
<div
in:slide={{ axis: 'x', duration: 500, easing: expoOut }}
class="bg-primary-900/5 dark:bg-primary-100/10 absolute inset-0 -z-10 p-0!"
style="width: {(choice.num_votes / totalVotes) *
100}%; transition: all 0.5s cubic-bezier(0.075, 0.82, 0.165, 1);"
></div>
{/if}
<label
class="px-4 py-2 w-full text-left font-medium flex flex-row gap-2 items-center"
>
<input
class="appearance-none absolute inset-0 cursor-pointer"
name="poll={post.id}"
value={choice.id}
bind:group={selected}
type="radio"
/>
<div class={[chosen == choice.id ? 'opacity-100' : 'opacity-10']}>
<Icon src={CheckBadge} size="24" mini />
</div>
<div>
{choice.choice_text}
</div>
{#if chosen}
<div class="ml-auto">
{Math.floor((choice.num_votes / totalVotes) * 100)}%
</div>
{/if}
</label>
</li>
{/each}
</CommonList>
<EndPlaceholder size="md" margin="bottom-sm">
{$t('post.poll.votes', { votes: totalVotes })}
{#snippet action()}
{#if post.poll.latest_vote}
<RelativeDate date={publishedToDate(post.poll.latest_vote)} />
{/if}
{/snippet}
</EndPlaceholder>
</form>
@@ -159,7 +159,7 @@
</h1>
</header>
<PostMedia
type={mediaType(data.data.value.post.post.url)}
type={mediaType(data.data.value.post.post)}
post={data.data.value.post.post}
opened
view="cozy"