|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { AppBskyRichtextFacet } from '@atproto/api' |
| 3 | +import { segmentize } from '@atcute/bluesky-richtext-segmenter' |
| 4 | +import type { Comment } from '#shared/types/blog-post' |
| 5 | +
|
| 6 | +type RichtextFeature = |
| 7 | + | AppBskyRichtextFacet.Link |
| 8 | + | AppBskyRichtextFacet.Mention |
| 9 | + | AppBskyRichtextFacet.Tag |
| 10 | +
|
| 11 | +function getCommentUrl(comment: Comment): string { |
| 12 | + return atUriToWebUrl(comment.uri) ?? '#' |
| 13 | +} |
| 14 | +const props = defineProps<{ |
| 15 | + comment: Comment |
| 16 | + depth: number |
| 17 | +}>() |
| 18 | +
|
| 19 | +const MaxDepth = 4 |
| 20 | +
|
| 21 | +function getFeatureUrl(feature: RichtextFeature): string | undefined { |
| 22 | + if (feature.$type === 'app.bsky.richtext.facet#link') return feature.uri |
| 23 | + if (feature.$type === 'app.bsky.richtext.facet#mention') |
| 24 | + return `https://bsky.app/profile/${feature.did}` |
| 25 | + if (feature.$type === 'app.bsky.richtext.facet#tag') |
| 26 | + return `https://bsky.app/hashtag/${feature.tag}` |
| 27 | +} |
| 28 | +
|
| 29 | +const processedSegments = segmentize(props.comment.text, props.comment.facets).map(segment => ({ |
| 30 | + text: segment.text, |
| 31 | + url: segment.features?.[0] ? getFeatureUrl(segment.features[0] as RichtextFeature) : undefined, |
| 32 | +})) |
| 33 | +
|
| 34 | +function getHostname(uri: string): string { |
| 35 | + try { |
| 36 | + return new URL(uri).hostname |
| 37 | + } catch { |
| 38 | + return uri |
| 39 | + } |
| 40 | +} |
| 41 | +</script> |
| 42 | + |
| 43 | +<template> |
| 44 | + <div :class="depth === 0 ? 'flex gap-3' : 'flex gap-3 mt-3'"> |
| 45 | + <!-- Avatar --> |
| 46 | + <a |
| 47 | + :href="`https://bsky.app/profile/${comment.author.handle}`" |
| 48 | + target="_blank" |
| 49 | + rel="noopener noreferrer" |
| 50 | + class="shrink-0" |
| 51 | + > |
| 52 | + <img |
| 53 | + v-if="comment.author.avatar" |
| 54 | + :src="comment.author.avatar" |
| 55 | + :alt="comment.author.displayName || comment.author.handle" |
| 56 | + :class="['rounded-full', depth === 0 ? 'w-10 h-10' : 'w-8 h-8']" |
| 57 | + width="40" |
| 58 | + height="40" |
| 59 | + loading="lazy" |
| 60 | + /> |
| 61 | + <div |
| 62 | + v-else |
| 63 | + :class="[ |
| 64 | + 'rounded-full bg-border flex items-center justify-center text-fg-muted', |
| 65 | + depth === 0 ? 'w-10 h-10' : 'w-8 h-8 text-sm', |
| 66 | + ]" |
| 67 | + > |
| 68 | + {{ (comment.author.displayName || comment.author.handle).charAt(0).toUpperCase() }} |
| 69 | + </div> |
| 70 | + </a> |
| 71 | + |
| 72 | + <div class="flex-1 min-w-0"> |
| 73 | + <!-- Author info + timestamp --> |
| 74 | + <div class="flex flex-wrap items-baseline gap-x-2 gap-y-0"> |
| 75 | + <a |
| 76 | + :href="`https://bsky.app/profile/${comment.author.handle}`" |
| 77 | + target="_blank" |
| 78 | + rel="noopener noreferrer" |
| 79 | + class="font-medium text-fg hover:underline" |
| 80 | + > |
| 81 | + {{ comment.author.displayName || comment.author.handle }} |
| 82 | + </a> |
| 83 | + <span class="text-fg-subtle text-sm">@{{ comment.author.handle }}</span> |
| 84 | + <span class="text-fg-subtle text-sm">·</span> |
| 85 | + <a |
| 86 | + :href="getCommentUrl(props.comment)" |
| 87 | + target="_blank" |
| 88 | + rel="noopener noreferrer" |
| 89 | + class="text-fg-subtle text-sm hover:underline" |
| 90 | + > |
| 91 | + <NuxtTime relative :datetime="comment.createdAt" /> |
| 92 | + </a> |
| 93 | + </div> |
| 94 | + |
| 95 | + <!-- Comment text with rich segments --> |
| 96 | + <p class="text-fg-muted whitespace-pre-wrap"> |
| 97 | + <template v-for="(segment, i) in processedSegments" :key="i"> |
| 98 | + <a |
| 99 | + v-if="segment.url" |
| 100 | + :href="segment.url" |
| 101 | + target="_blank" |
| 102 | + rel="noopener noreferrer" |
| 103 | + class="link" |
| 104 | + >{{ segment.text }}</a |
| 105 | + > |
| 106 | + <template v-else>{{ segment.text }}</template> |
| 107 | + </template> |
| 108 | + </p> |
| 109 | + |
| 110 | + <!-- Embedded images --> |
| 111 | + <div |
| 112 | + v-if="comment.embed?.type === 'images' && comment.embed.images" |
| 113 | + class="flex flex-wrap gap-2" |
| 114 | + > |
| 115 | + <a |
| 116 | + v-for="(img, i) in comment.embed.images" |
| 117 | + :key="i" |
| 118 | + :href="img.fullsize" |
| 119 | + target="_blank" |
| 120 | + rel="noopener noreferrer" |
| 121 | + class="block" |
| 122 | + > |
| 123 | + <img |
| 124 | + :src="img.thumb" |
| 125 | + :alt="img.alt || 'Embedded image'" |
| 126 | + class="rounded-lg max-w-48 max-h-36 object-cover" |
| 127 | + loading="lazy" |
| 128 | + /> |
| 129 | + </a> |
| 130 | + </div> |
| 131 | + |
| 132 | + <!-- Embedded external link card --> |
| 133 | + <a |
| 134 | + v-if="comment.embed?.type === 'external' && comment.embed.external" |
| 135 | + :href="comment.embed.external.uri" |
| 136 | + target="_blank" |
| 137 | + rel="noopener noreferrer" |
| 138 | + class="flex gap-3 p-3 border border-border rounded-lg bg-bg-subtle hover:bg-bg-subtle/80 transition-colors no-underline" |
| 139 | + > |
| 140 | + <img |
| 141 | + v-if="comment.embed.external.thumb" |
| 142 | + :src="comment.embed.external.thumb" |
| 143 | + :alt="comment.embed.external.title" |
| 144 | + width="20" |
| 145 | + height="20" |
| 146 | + class="w-20 h-20 rounded object-cover shrink-0" |
| 147 | + loading="lazy" |
| 148 | + /> |
| 149 | + <div class="min-w-0"> |
| 150 | + <div class="font-medium text-fg truncate"> |
| 151 | + {{ comment.embed.external.title }} |
| 152 | + </div> |
| 153 | + <div class="text-sm text-fg-muted line-clamp-2"> |
| 154 | + {{ comment.embed.external.description }} |
| 155 | + </div> |
| 156 | + <div class="text-xs text-fg-subtle mt-1 truncate"> |
| 157 | + {{ getHostname(comment.embed.external.uri) }} |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + </a> |
| 161 | + |
| 162 | + <!-- Like/repost counts --> |
| 163 | + <div |
| 164 | + v-if="comment.likeCount > 0 || comment.repostCount > 0" |
| 165 | + class="mt-2 flex gap-4 text-sm text-fg-subtle" |
| 166 | + > |
| 167 | + <span v-if="comment.likeCount > 0"> |
| 168 | + {{ $t('blog.atproto.like_count', { count: comment.likeCount }, comment.likeCount) }} |
| 169 | + </span> |
| 170 | + <span v-if="comment.repostCount > 0"> |
| 171 | + {{ $t('blog.atproto.repost_count', { count: comment.repostCount }, comment.repostCount) }} |
| 172 | + </span> |
| 173 | + </div> |
| 174 | + |
| 175 | + <!-- Nested replies --> |
| 176 | + <template v-if="comment.replies.length > 0"> |
| 177 | + <div v-if="depth < MaxDepth" class="mt-2 ps-2 border-is-2 border-border flex flex-col"> |
| 178 | + <BlueskyComment |
| 179 | + v-for="reply in comment.replies" |
| 180 | + :key="reply.uri" |
| 181 | + :comment="reply" |
| 182 | + :depth="depth + 1" |
| 183 | + /> |
| 184 | + </div> |
| 185 | + <a |
| 186 | + v-else |
| 187 | + :href="getCommentUrl(comment.replies[0]!)" |
| 188 | + target="_blank" |
| 189 | + rel="noopener noreferrer" |
| 190 | + class="mt-2 block text-sm link" |
| 191 | + > |
| 192 | + {{ |
| 193 | + $t( |
| 194 | + 'blog.atproto.more_replies', |
| 195 | + { count: comment.replies.length }, |
| 196 | + comment.replies.length, |
| 197 | + ) |
| 198 | + }} |
| 199 | + </a> |
| 200 | + </template> |
| 201 | + </div> |
| 202 | + </div> |
| 203 | +</template> |
0 commit comments