//import * as ATPAPI from "@atproto/api" import { useAtom } from "jotai"; import * as React from "react"; import { usePollData, usePollMutationQueue, } from "~/providers/PollMutationQueueProvider"; //import { useAuth } from "~/providers/UnifiedAuthProvider"; import { renderSnack } from "~/routes/__root"; import { imgCDNAtom } from "~/utils/atoms"; import { useQueryArbitrary, useQueryConstellation, useQueryProfile } from "~/utils/useQuery"; import { type embedtryfall } from "./PostEmbeds"; import { ExternalLinkEmbed } from "./PostEmbeds"; export function PollEmbed({ did, rkey, redactedLoading, embedtryfall }: { did: string; rkey: string; redactedLoading?: boolean; embedtryfall?: embedtryfall; }) { //const { agent } = useAuth(); const { refreshPollData } = usePollMutationQueue(); const pollUri = `at://${did}/app.reddwarf.embed.poll/${rkey}`; const { data: pollRecord, isLoading, error } = useQueryArbitrary(pollUri); const dontLoadPolls = embedtryfall && (isLoading || pollRecord === undefined || error !== null) || false const { data: voteCountsA } = useQueryConstellation({ method: "/links/count/distinct-dids", target: pollUri, collection: "app.reddwarf.poll.vote.a", path: ".subject.uri", customkey: "constellation-polls", enabled: !dontLoadPolls }); const { data: voteCountsB } = useQueryConstellation({ method: "/links/count/distinct-dids", target: pollUri, collection: "app.reddwarf.poll.vote.b", path: ".subject.uri", customkey: "constellation-polls", enabled: !dontLoadPolls }); const { data: voteCountsC } = useQueryConstellation({ method: "/links/count/distinct-dids", target: pollUri, collection: "app.reddwarf.poll.vote.c", path: ".subject.uri", customkey: "constellation-polls", enabled: !dontLoadPolls }); const { data: voteCountsD } = useQueryConstellation({ method: "/links/count/distinct-dids", target: pollUri, collection: "app.reddwarf.poll.vote.d", path: ".subject.uri", customkey: "constellation-polls", enabled: !dontLoadPolls }); // const { data: votersA } = useQueryConstellation({ // method: "/links", // target: pollUri, // collection: "app.reddwarf.poll.vote.a", // path: ".subject.uri", // customkey: "constellation-polls", // enabled: !isLoading // }); // const { data: votersB } = useQueryConstellation({ // method: "/links", // target: pollUri, // collection: "app.reddwarf.poll.vote.b", // path: ".subject.uri", // customkey: "constellation-polls", // enabled: !isLoading // }); // const { data: votersC } = useQueryConstellation({ // method: "/links", // target: pollUri, // collection: "app.reddwarf.poll.vote.c", // path: ".subject.uri", // customkey: "constellation-polls", // enabled: !isLoading // }); // const { data: votersD } = useQueryConstellation({ // method: "/links", // target: pollUri, // collection: "app.reddwarf.poll.vote.d", // path: ".subject.uri", // customkey: "constellation-polls", // enabled: !isLoading // }); const poll = { ...(pollRecord?.value ?? {}), multiple: true, } as { a: string; b: string; c?: string; d?: string; expiry?: string; multiple?: boolean; createdAt: string; }; const options = [poll.a, poll.b, poll.c, poll.d].filter(Boolean); const serverCounts = { a: parseInt((voteCountsA as any)?.total || "0"), b: parseInt((voteCountsB as any)?.total || "0"), c: parseInt((voteCountsC as any)?.total || "0"), d: parseInt((voteCountsD as any)?.total || "0"), }; const { results, totalVotes, handleVote, votersA, votersB, votersC, votersD } = usePollData( pollUri, pollRecord?.cid, !!poll.multiple, serverCounts, !dontLoadPolls ); if (dontLoadPolls && embedtryfall) { const link = embedtryfall.embed.external; const onOpen = embedtryfall.onOpen return ( <> {/* pass thru confirm
embedtryfall = {JSON.stringify(embedtryfall, null, 2)}
isLoading = {JSON.stringify(isLoading, null, 2)}
pollRecord = {JSON.stringify(pollRecord, null, 2)}
error = {JSON.stringify(error, null, 2)}
*/} ) } if (isLoading && !embedtryfall) { return (
); } if (error || !pollRecord?.value) { return
Failed to load poll
; } const isExpired = false; return ( <>
Public Poll
{poll.multiple ? ( ) : ( )} {poll.multiple ? "Select one or more options" : "Select one option"}
{options.map((optionText, index) => { const optionKey = ["a", "b", "c", "d"][index] as | "a" | "b" | "c" | "d"; const { topVoterDids } = results[optionKey]; const optionState = results[optionKey]; const hasVotedForOption = optionState.hasVoted; const votePercentage = totalVotes > 0 ? (optionState.count / totalVotes) * 100 : 0; const votersData = (() => { if (optionKey === "a") return votersA?.linking_records || []; if (optionKey === "b") return votersB?.linking_records || []; if (optionKey === "c") return votersC?.linking_records || []; if (optionKey === "d") return votersD?.linking_records || []; return []; })(); const topVoters = votersData .filter((v: any) => !!v.did) .slice(0, 5); return (
{ e.stopPropagation(); if (!isExpired) { handleVote(optionKey); } }} >
{optionText} {hasVotedForOption && ( {poll.multiple ? "✓" : "✓ (click to remove)"} )}
{topVoterDids.length > 0 && (
{topVoterDids.map((did, idx) => (
))}
)} {votePercentage.toFixed(0)}%
); })}
Never expires
); } export function PollOptionAvatar({ did }: { did: string }) { const [imgcdn] = useAtom(imgCDNAtom); const { data: profileRecord } = useQueryProfile( `at://${did}/app.bsky.actor.profile/self`, ); const avatarUrl = getAvatarUrl(profileRecord, did, imgcdn); if (!avatarUrl) { return
; } return ( voter { const target = e.target as HTMLImageElement; target.style.display = "none"; target.parentElement!.style.backgroundColor = "#6b7280"; }} /> ); } function getAvatarUrl(opProfile: any, did: string, cdn: string) { const link = opProfile?.value?.avatar?.ref?.["$link"]; if (!link) return null; return `https://${cdn}/img/avatar/plain/${did}/${link}@jpeg`; }