|
6 | 6 | import { resolveRoute } from '$app/paths'; |
7 | 7 | import * as Button from "$lib/components/ui/button"; |
8 | 8 | import * as Progress from "$lib/components/ui/progress"; |
9 | | - import { computeBrightness } from "$lib/utils/color.js"; |
| 9 | + import { getContrastColor } from "$lib/utils/color.js"; |
10 | 10 |
|
11 | 11 | let surveyIdFromQuery = $derived(page.url.searchParams.get('id')); |
12 | | - let currentSurvey = $derived(surveyIdFromQuery ? $surveys.find(s => s.id === surveyIdFromQuery) : undefined); |
13 | | - let totalVotes = $derived(currentSurvey ? Object.values(currentSurvey.results).reduce((sum, count) => sum + count, 0) : 0); |
| 12 | + let survey = $derived(surveyIdFromQuery ? $surveys.find(s => s.id === surveyIdFromQuery) : undefined); |
| 13 | + let totalVotes = $derived(survey ? Object.values(survey.results).reduce((sum, count) => sum + count, 0) : 0); |
14 | 14 |
|
15 | 15 | function getAnswerDisplay(answerId: string): { text: string } { |
16 | | - if (!currentSurvey) return { text: "Unknown Option" }; |
17 | | - const answer = currentSurvey.answers.find(a => a.id === answerId); |
| 16 | + if (!survey) return { text: "Unknown Option" }; |
| 17 | + const answer = survey.answers.find(a => a.id === answerId); |
18 | 18 | return answer |
19 | 19 | ? { text: answer.text || "Unknown" } |
20 | 20 | : { text: "Unknown Option" }; |
|
24 | 24 | <title>{m.results_page_title()} | {m.app_title()}</title> |
25 | 25 | </svelte:head> |
26 | 26 |
|
27 | | -{#if currentSurvey} |
| 27 | +{#if survey} |
28 | 28 | <div class="w-full max-w-xl text-center"> |
29 | | - <h2 class="text-2xl sm:text-3xl font-bold mb-6"> |
30 | | - {currentSurvey.question} |
| 29 | + <h2 class="text-2xl sm:text-3xl font-bold mb-6" style="color: {getContrastColor(survey?.appearance.backgroundColor)};"> |
| 30 | + {survey.question} |
31 | 31 | </h2> |
32 | 32 |
|
33 | 33 | {#if totalVotes > 0} |
34 | 34 | <div class="space-y-4 p-6"> |
35 | | - {#each currentSurvey.answers as answer (answer.id)} |
36 | | - {@const votes = currentSurvey.results[answer.id] || 0} |
| 35 | + {#each survey.answers as answer (answer.id)} |
| 36 | + {@const votes = survey.results[answer.id] || 0} |
37 | 37 | {@const percentage = totalVotes > 0 ? (votes / totalVotes) * 100 : 0} |
38 | 38 | {@const display = getAnswerDisplay(answer.id)} |
39 | 39 | <div class="text-left"> |
40 | 40 | <div class="flex justify-between items-center mb-1"> |
41 | | - <span class="text-md sm:text-lg"> |
| 41 | + <span class="text-md sm:text-lg" style="color: {getContrastColor(survey?.appearance.backgroundColor)};"> |
42 | 42 | {display.text}: {votes} {votes === 1 ? m.votes_suffix_singular() : m.votes_suffix_plural()} |
43 | 43 | </span> |
44 | | - {#if percentage > 0}<span class="text-md sm:text-lg">{percentage.toFixed(1)}%</span>{/if} |
| 44 | + {#if percentage > 0} |
| 45 | + <span class="text-md sm:text-lg" style="color: {getContrastColor(survey?.appearance.backgroundColor)};"> |
| 46 | + {percentage.toFixed(1)}% |
| 47 | + </span> |
| 48 | + {/if} |
45 | 49 | </div> |
46 | | - <Progress.Root value={percentage} class="h-6 sm:h-8" style="background-color: {currentSurvey?.appearance.buttonColor || ''};" /> |
| 50 | + <Progress.Root value={percentage} class="h-6 sm:h-8" style="background-color: {survey?.appearance.buttonColor || ''};" /> |
47 | 51 | </div> |
48 | 52 | {/each} |
49 | 53 | </div> |
50 | | - <p class="">{m.total_votes_label({ count: totalVotes })}</p> |
| 54 | + <p class="" style="color: {getContrastColor(survey?.appearance.backgroundColor)};"> |
| 55 | + {m.total_votes_label({ count: totalVotes })} |
| 56 | + </p> |
51 | 57 | {:else} |
52 | 58 | <p class="text-xl text-muted-foreground my-8">{m.no_responses_message()}</p> |
53 | 59 | {/if} |
|
56 | 62 | onclick={() => goto(resolveRoute(`/survey?id=${surveyIdFromQuery}`, {}))} |
57 | 63 | class="mt-8" |
58 | 64 | style=" |
59 | | - background-color: {currentSurvey?.appearance.buttonColor || ''}; |
60 | | - color: { |
61 | | - (() => { |
62 | | - return computeBrightness(currentSurvey?.appearance.buttonColor) > 128 ? 'black' : 'white'; |
63 | | - })() |
64 | | - }; |
| 65 | + background-color: {survey?.appearance.buttonColor || ''}; |
| 66 | + color: {getContrastColor(survey?.appearance.buttonColor)}; |
65 | 67 | " |
66 | 68 | size="lg" |
67 | 69 | > |
|
0 commit comments