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 { Search } from 'mono-svelte'
import type { import type {
CommentReplyView, CommentReplyView,
@@ -19,6 +18,7 @@ import type {
Person, Person,
PersonMentionView, PersonMentionView,
PersonView, PersonView,
Post,
PostView, PostView,
PrivateMessageView, PrivateMessageView,
SortType, 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 featured_local: boolean
url_content_type?: string url_content_type?: string
alt_text?: string alt_text?: string
event?: PostEvent
poll?: PostPoll
} }
export interface PostAggregates { export interface PostAggregates {
@@ -2301,6 +2303,25 @@ export interface VoteView {
score: number 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 { export interface UploadImage {
image: File | Buffer image: File | Buffer
} }
@@ -2395,21 +2416,37 @@ export interface AssignFlair {
flair_id_list: number[] flair_id_list: number[]
} }
export interface SearchResponse { export interface PostPoll {
type_: SearchType end_poll?: string
comments: Array<CommentView> mode: string
posts: Array<PostView> local_only: boolean
communities: Array<CommunityView> latest_vote?: string
users: Array<PersonView> choices: PollChoice[]
} }
export interface SiteResponse { export interface PollChoice {
site_view: SiteView id: number
taglines: Array<Tagline> choice_text: string
sort_order: number
num_votes: number
} }
export interface SuccessResponse {
success: boolean export interface PostEvent {
} start: string
export interface UpdateTotpResponse { end?: string
enabled: boolean 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": { "meta": {
"lastEdited": "Last edited {{default}}", "lastEdited": "Last edited {{default}}",
"created": "Created {{default}}" "created": "Created {{default}}"
},
"poll": {
"votes": "{{votes:number}} votes"
} }
}, },
"comment": { "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 rule = $derived(getTagRule(tags.tags))
let hideTitle = $derived( let hideTitle = $derived(
settings.posts.deduplicateEmbed && settings.posts.deduplicateEmbed &&
+20 -2
View File
@@ -70,11 +70,29 @@ export function postLink(post: Post) {
return `/post/${encodeURIComponent(profile.current.instance)}/${post.id}` 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 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' if (!url) return 'none'
try { try {
new URL(url) new URL(url)
} catch { } catch {
@@ -3,6 +3,7 @@
import { settings, type View } from '$lib/app/settings.svelte' import { settings, type View } from '$lib/app/settings.svelte'
import { PostIframe, PostImage, PostLink } from '..' import { PostIframe, PostImage, PostLink } from '..'
import { iframeType, type MediaType } from '../helpers' import { iframeType, type MediaType } from '../helpers'
import PostPoll from './PostPoll.svelte'
interface Props { interface Props {
view?: View view?: View
@@ -49,4 +50,6 @@
{view} {view}
{...rest} {...rest}
/> />
{:else if type == 'poll' && post.poll}
<PostPoll post={{ ...post, poll: post.poll! }} />
{/if} {/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> </h1>
</header> </header>
<PostMedia <PostMedia
type={mediaType(data.data.value.post.post.url)} type={mediaType(data.data.value.post.post)}
post={data.data.value.post.post} post={data.data.value.post.post}
opened opened
view="cozy" view="cozy"