|
| 1 | +<script lang="ts" setup> |
| 2 | +import type { Option } from '../../types.ts' |
| 3 | +import type { OptionsDatabase, Posts } from '@devprotocol/clubs-plugin-posts' |
| 4 | +import { type ClubsProfile, decode } from '@devprotocol/clubs-core' |
| 5 | +import { onMounted, ref } from 'vue' |
| 6 | +import Profile from '../Votes/Profile.vue' |
| 7 | +import IconLock from '../../assets/images/icon-lock.png' |
| 8 | +import { remark } from 'remark' |
| 9 | +import strip from 'strip-markdown' |
| 10 | +
|
| 11 | +type Props = { |
| 12 | + options: Option[] |
| 13 | + feeds: OptionsDatabase[] |
| 14 | + postsPluginId: string |
| 15 | +} |
| 16 | +const props = defineProps<Props>() |
| 17 | +
|
| 18 | +const polls = ref<any[]>([]) |
| 19 | +
|
| 20 | +const { postsPluginId, feeds } = props |
| 21 | +
|
| 22 | +type Polls = { |
| 23 | + title: string |
| 24 | + values: PostsPlus[] |
| 25 | +} |
| 26 | +
|
| 27 | +type PostsPlus = Posts & { |
| 28 | + image: string |
| 29 | + profile: Promise<{ |
| 30 | + readonly profile: ClubsProfile | undefined |
| 31 | + readonly error: Error | undefined |
| 32 | + }> |
| 33 | + stripedMarkdown: string |
| 34 | +} |
| 35 | +
|
| 36 | +const feedId = ref('') |
| 37 | +
|
| 38 | +const fetchPolls = async (feed: OptionsDatabase): Promise<Polls> => { |
| 39 | + feedId.value = feed.id |
| 40 | + const title = feed.title |
| 41 | +
|
| 42 | + const url = new URL( |
| 43 | + `/api/${postsPluginId}/${feedId.value}/search/has:option/%23poll`, |
| 44 | + window.location.origin, |
| 45 | + ) |
| 46 | +
|
| 47 | + const res = await fetch(url.toString()) |
| 48 | + const json = await res.json() |
| 49 | +
|
| 50 | + return { |
| 51 | + title: title ? title : feedId.value, |
| 52 | + values: decode(json.contents), |
| 53 | + } |
| 54 | +} |
| 55 | +
|
| 56 | +onMounted(async () => { |
| 57 | + await Promise.all( |
| 58 | + feeds.map(async (feed) => { |
| 59 | + const data = await fetchPolls(feed) |
| 60 | +
|
| 61 | + data.values = data.values.map((post) => { |
| 62 | + post.updated_at = new Date(post.updated_at).toLocaleString('ja-JP') |
| 63 | +
|
| 64 | + const images = post.options.find((item) => item.key === '#images') |
| 65 | + if (images && images.value.length > 0) { |
| 66 | + post.image = images.value[0] |
| 67 | + } |
| 68 | +
|
| 69 | + remark() |
| 70 | + .use(strip) |
| 71 | + .process(post.content) |
| 72 | + .then((text) => { |
| 73 | + post.stripedMarkdown = text.toString() |
| 74 | + }) |
| 75 | +
|
| 76 | + return post |
| 77 | + }) |
| 78 | +
|
| 79 | + polls.value = [...polls.value, data] |
| 80 | + }), |
| 81 | + ) |
| 82 | +}) |
| 83 | +</script> |
| 84 | + |
| 85 | +<template> |
| 86 | + <div class="p-4"> |
| 87 | + <div v-for="poll in polls" :key="poll.title"> |
| 88 | + <h2 class="mb-4 text-xl">{{ poll.title }}</h2> |
| 89 | + <a |
| 90 | + v-for="post in poll.values" |
| 91 | + :key="post.id" |
| 92 | + :href="`/posts/${feedId}/${post.id}`" |
| 93 | + class="block mb-4 p-2 bg-gray-100 rounded" |
| 94 | + > |
| 95 | + <div class="flex justify-between gap-2"> |
| 96 | + <div class="w-full"> |
| 97 | + <p class="text-lg font-bold">Post_title: {{ post.title }}</p> |
| 98 | + <p class="mb-1 text-xs text-gray-400">{{ post.updated_at }}</p> |
| 99 | + <div class="flex justify-between gap-2"> |
| 100 | + <Profile :address="post.created_by" /> |
| 101 | + <p v-if="true" class="flex-grow flex-wrap text-lg truncate"> |
| 102 | + {{ post.stripedMarkdown }} |
| 103 | + </p> |
| 104 | + <div |
| 105 | + v-else |
| 106 | + class="flex flex-col justify-center items-center flex-grow p-2 bg-gray-200 rounded" |
| 107 | + > |
| 108 | + <img |
| 109 | + class="mb-1 w-5" |
| 110 | + :src="IconLock.src" |
| 111 | + alt="paper-airplane" |
| 112 | + /> |
| 113 | + <p class="leading-none">Locked</p> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + <figure v-if="!isMasked && post.image"> |
| 118 | + <img |
| 119 | + :src="post.image" |
| 120 | + class="rounded max-w-20 max-h-20 object-cover object-center" |
| 121 | + alt="post image" |
| 122 | + /> |
| 123 | + </figure> |
| 124 | + </div> |
| 125 | + </a> |
| 126 | + <div v-if="poll.length < 1" class="mb-4 p-2 bg-gray-100 rounded"> |
| 127 | + <p class="w-full text-gray-400 text-center">Empty :)</p> |
| 128 | + </div> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | +</template> |
0 commit comments