2023-07-27 05:31:52 +00:00
|
|
|
<!--
|
2024-02-13 15:59:27 +00:00
|
|
|
SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 05:31:52 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2020-01-29 19:37:25 +00:00
|
|
|
<template>
|
2023-05-19 05:08:51 +00:00
|
|
|
<div :class="{ [$style.done]: closed || isVoted }">
|
|
|
|
<ul :class="$style.choices">
|
2024-01-30 10:53:53 +00:00
|
|
|
<li v-for="(choice, i) in poll.choices" :key="i" :class="$style.choice" @click="vote(i)">
|
2023-05-19 05:08:51 +00:00
|
|
|
<div :class="$style.bg" :style="{ 'width': `${showResult ? (choice.votes / total * 100) : 0}%` }"></div>
|
|
|
|
<span :class="$style.fg">
|
|
|
|
<template v-if="choice.isVoted"><i class="ti ti-check" style="margin-right: 4px; color: var(--accent);"></i></template>
|
2022-12-29 01:14:44 +00:00
|
|
|
<Mfm :text="choice.text" :plain="true"/>
|
2024-01-19 23:11:59 +00:00
|
|
|
<span v-if="showResult" style="margin-left: 4px; opacity: 0.7;">({{ i18n.tsx._poll.votesCount({ n: choice.votes }) }})</span>
|
2020-01-29 19:37:25 +00:00
|
|
|
</span>
|
|
|
|
</li>
|
|
|
|
</ul>
|
2023-05-19 05:08:51 +00:00
|
|
|
<p v-if="!readOnly" :class="$style.info">
|
2024-01-19 23:11:59 +00:00
|
|
|
<span>{{ i18n.tsx._poll.totalVotes({ n: total }) }}</span>
|
2020-01-29 19:37:25 +00:00
|
|
|
<span> · </span>
|
2023-05-19 05:08:51 +00:00
|
|
|
<a v-if="!closed && !isVoted" style="color: inherit;" @click="showResult = !showResult">{{ showResult ? i18n.ts._poll.vote : i18n.ts._poll.showResult }}</a>
|
2022-07-20 13:24:26 +00:00
|
|
|
<span v-if="isVoted">{{ i18n.ts._poll.voted }}</span>
|
|
|
|
<span v-else-if="closed">{{ i18n.ts._poll.closed }}</span>
|
2020-01-29 19:37:25 +00:00
|
|
|
<span v-if="remaining > 0"> · {{ timer }}</span>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-07-20 13:24:26 +00:00
|
|
|
<script lang="ts" setup>
|
2023-02-16 14:09:41 +00:00
|
|
|
import { computed, ref } from 'vue';
|
2023-09-04 04:33:38 +00:00
|
|
|
import * as Misskey from 'misskey-js';
|
2023-09-19 07:37:43 +00:00
|
|
|
import { sum } from '@/scripts/array.js';
|
|
|
|
import { pleaseLogin } from '@/scripts/please-login.js';
|
|
|
|
import * as os from '@/os.js';
|
2024-01-04 09:32:46 +00:00
|
|
|
import { misskeyApi } from '@/scripts/misskey-api.js';
|
2023-09-19 07:37:43 +00:00
|
|
|
import { i18n } from '@/i18n.js';
|
2024-07-14 06:27:52 +00:00
|
|
|
import { host } from '@/config.js';
|
2023-09-19 07:37:43 +00:00
|
|
|
import { useInterval } from '@/scripts/use-interval.js';
|
2020-01-29 19:37:25 +00:00
|
|
|
|
2022-07-20 13:24:26 +00:00
|
|
|
const props = defineProps<{
|
2024-01-30 10:53:53 +00:00
|
|
|
noteId: string;
|
|
|
|
poll: NonNullable<Misskey.entities.Note['poll']>;
|
2022-07-20 13:24:26 +00:00
|
|
|
readOnly?: boolean;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const remaining = ref(-1);
|
|
|
|
|
2024-01-30 10:53:53 +00:00
|
|
|
const total = computed(() => sum(props.poll.choices.map(x => x.votes)));
|
2022-07-20 13:24:26 +00:00
|
|
|
const closed = computed(() => remaining.value === 0);
|
2024-01-30 10:53:53 +00:00
|
|
|
const isVoted = computed(() => !props.poll.multiple && props.poll.choices.some(c => c.isVoted));
|
2024-01-19 23:11:59 +00:00
|
|
|
const timer = computed(() => i18n.tsx._poll[
|
2024-01-30 10:53:53 +00:00
|
|
|
remaining.value >= 86400 ? 'remainingDays' :
|
|
|
|
remaining.value >= 3600 ? 'remainingHours' :
|
|
|
|
remaining.value >= 60 ? 'remainingMinutes' : 'remainingSeconds'
|
|
|
|
]({
|
|
|
|
s: Math.floor(remaining.value % 60),
|
|
|
|
m: Math.floor(remaining.value / 60) % 60,
|
|
|
|
h: Math.floor(remaining.value / 3600) % 24,
|
|
|
|
d: Math.floor(remaining.value / 86400),
|
|
|
|
}));
|
2022-07-20 13:24:26 +00:00
|
|
|
|
|
|
|
const showResult = ref(props.readOnly || isVoted.value);
|
|
|
|
|
2024-07-14 06:27:52 +00:00
|
|
|
const pleaseLoginContext = {
|
|
|
|
type: 'lookup',
|
|
|
|
path: `https://${host}/notes/${props.note.id}`,
|
|
|
|
} as const;
|
|
|
|
|
2022-07-20 13:24:26 +00:00
|
|
|
// 期限付きアンケート
|
2024-01-30 10:53:53 +00:00
|
|
|
if (props.poll.expiresAt) {
|
2022-07-20 13:24:26 +00:00
|
|
|
const tick = () => {
|
2024-01-30 10:53:53 +00:00
|
|
|
remaining.value = Math.floor(Math.max(new Date(props.poll.expiresAt!).getTime() - Date.now(), 0) / 1000);
|
2022-07-20 13:24:26 +00:00
|
|
|
if (remaining.value === 0) {
|
|
|
|
showResult.value = true;
|
2020-01-29 19:37:25 +00:00
|
|
|
}
|
2022-07-20 13:24:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
useInterval(tick, 3000, {
|
|
|
|
immediate: true,
|
|
|
|
afterMounted: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const vote = async (id) => {
|
2024-07-14 06:27:52 +00:00
|
|
|
pleaseLogin(undefined, pleaseLoginContext);
|
2022-07-20 13:24:26 +00:00
|
|
|
|
|
|
|
if (props.readOnly || closed.value || isVoted.value) return;
|
|
|
|
|
|
|
|
const { canceled } = await os.confirm({
|
|
|
|
type: 'question',
|
2024-01-30 10:53:53 +00:00
|
|
|
text: i18n.tsx.voteConfirm({ choice: props.poll.choices[id].text }),
|
2022-07-20 13:24:26 +00:00
|
|
|
});
|
|
|
|
if (canceled) return;
|
2021-12-03 07:07:50 +00:00
|
|
|
|
2024-01-04 09:32:46 +00:00
|
|
|
await misskeyApi('notes/polls/vote', {
|
2024-01-30 10:53:53 +00:00
|
|
|
noteId: props.noteId,
|
2022-07-20 13:24:26 +00:00
|
|
|
choice: id,
|
|
|
|
});
|
2024-01-30 10:53:53 +00:00
|
|
|
if (!showResult.value) showResult.value = !props.poll.multiple;
|
2022-07-20 13:24:26 +00:00
|
|
|
};
|
2020-01-29 19:37:25 +00:00
|
|
|
</script>
|
|
|
|
|
2023-05-19 05:08:51 +00:00
|
|
|
<style lang="scss" module>
|
|
|
|
.choices {
|
|
|
|
display: block;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
list-style: none;
|
|
|
|
}
|
2020-01-29 19:37:25 +00:00
|
|
|
|
2023-05-19 05:08:51 +00:00
|
|
|
.choice {
|
|
|
|
display: block;
|
|
|
|
position: relative;
|
|
|
|
margin: 4px 0;
|
|
|
|
padding: 4px;
|
|
|
|
//border: solid 0.5px var(--divider);
|
|
|
|
background: var(--accentedBg);
|
|
|
|
border-radius: 4px;
|
|
|
|
overflow: clip;
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
2020-01-29 19:37:25 +00:00
|
|
|
|
2023-05-19 05:08:51 +00:00
|
|
|
.bg {
|
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
height: 100%;
|
|
|
|
background: var(--accent);
|
|
|
|
background: linear-gradient(90deg,var(--buttonGradateA),var(--buttonGradateB));
|
|
|
|
transition: width 1s ease;
|
|
|
|
}
|
2020-01-29 19:37:25 +00:00
|
|
|
|
2023-05-19 05:08:51 +00:00
|
|
|
.fg {
|
|
|
|
position: relative;
|
|
|
|
display: inline-block;
|
|
|
|
padding: 3px 5px;
|
|
|
|
background: var(--panel);
|
|
|
|
border-radius: 3px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.info {
|
|
|
|
color: var(--fg);
|
|
|
|
}
|
|
|
|
|
|
|
|
.done {
|
|
|
|
.choice {
|
|
|
|
cursor: default;
|
2020-01-29 19:37:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|