💄 Optimize attachment list
This commit is contained in:
197
app/components/AttachmentList.vue
Normal file
197
app/components/AttachmentList.vue
Normal file
@@ -0,0 +1,197 @@
|
||||
<template>
|
||||
<div v-if="attachments.length > 0" @click.stop>
|
||||
<!-- Single attachment: direct render -->
|
||||
<attachment-item
|
||||
v-if="attachments.length === 1 && attachments[0]"
|
||||
:item="attachments[0]"
|
||||
/>
|
||||
|
||||
<!-- Multiple attachments -->
|
||||
<template v-else>
|
||||
<!-- All images: use carousel -->
|
||||
<div
|
||||
v-if="isAllImages"
|
||||
class="carousel-container rounded-lg overflow-hidden"
|
||||
:style="carouselStyle"
|
||||
>
|
||||
<v-card width="100%" border>
|
||||
<v-carousel
|
||||
height="100%"
|
||||
hide-delimiter-background
|
||||
show-arrows="hover"
|
||||
hide-delimiters
|
||||
progress="primary"
|
||||
>
|
||||
<v-carousel-item
|
||||
v-for="attachment in attachments"
|
||||
:key="attachment.id"
|
||||
:src="getAttachmentUrl(attachment)"
|
||||
cover
|
||||
/>
|
||||
</v-carousel>
|
||||
</v-card>
|
||||
</div>
|
||||
|
||||
<!-- Mixed content: vertical scrollable -->
|
||||
<div v-else class="space-y-4 max-h-96 overflow-y-auto">
|
||||
<attachment-item
|
||||
v-for="attachment in attachments"
|
||||
:key="attachment.id"
|
||||
:item="attachment"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { computed } from "vue"
|
||||
import type { SnAttachment } from "~/types/api"
|
||||
import AttachmentItem from "./AttachmentItem.vue"
|
||||
|
||||
const props = defineProps<{
|
||||
attachments: SnAttachment[]
|
||||
maxHeight?: number
|
||||
}>()
|
||||
|
||||
const apiBase = useSolarNetworkUrl()
|
||||
|
||||
const isAllImages = computed(
|
||||
() =>
|
||||
props.attachments.length > 0 &&
|
||||
props.attachments.every((att) => att.mimeType?.startsWith("image/"))
|
||||
)
|
||||
|
||||
const carouselHeight = computed(() => {
|
||||
if (!isAllImages.value) return Math.min(400, props.maxHeight || 400)
|
||||
|
||||
const aspectRatio = calculateAspectRatio()
|
||||
// Use a base width of 600px for calculation, adjust height accordingly
|
||||
const baseWidth = 600
|
||||
const calculatedHeight = Math.round(baseWidth / aspectRatio)
|
||||
|
||||
// Respect maxHeight constraint if provided
|
||||
const constrainedHeight = props.maxHeight
|
||||
? Math.min(calculatedHeight, props.maxHeight)
|
||||
: calculatedHeight
|
||||
|
||||
return constrainedHeight
|
||||
})
|
||||
|
||||
const carouselStyle = computed(() => {
|
||||
if (!isAllImages.value) return {}
|
||||
|
||||
const aspectRatio = calculateAspectRatio()
|
||||
const height = carouselHeight.value
|
||||
const width = Math.round(height * aspectRatio)
|
||||
|
||||
return {
|
||||
width: `${width}px`,
|
||||
height: `${height}px`,
|
||||
maxWidth: "100%" // Ensure it doesn't overflow container
|
||||
}
|
||||
})
|
||||
|
||||
function calculateAspectRatio(): number {
|
||||
const ratios: number[] = []
|
||||
|
||||
// Collect all valid ratios
|
||||
for (const attachment of props.attachments) {
|
||||
const meta = attachment.fileMeta
|
||||
if (meta && typeof meta === "object" && "ratio" in meta) {
|
||||
const ratioValue = (meta as Record<string, unknown>).ratio
|
||||
if (typeof ratioValue === "number" && ratioValue > 0) {
|
||||
ratios.push(ratioValue)
|
||||
} else if (typeof ratioValue === "string") {
|
||||
try {
|
||||
const parsed = parseFloat(ratioValue)
|
||||
if (parsed > 0) ratios.push(parsed)
|
||||
} catch {
|
||||
// Skip invalid string ratios
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ratios.length === 0) {
|
||||
// Default to 4:3 aspect ratio when no valid ratios found
|
||||
return 4 / 3
|
||||
}
|
||||
|
||||
if (ratios.length === 1 && ratios[0]) {
|
||||
return ratios[0]
|
||||
}
|
||||
|
||||
// Group similar ratios and find the most common one
|
||||
const commonRatios: Record<number, number> = {}
|
||||
|
||||
// Common aspect ratios to round to (with tolerance)
|
||||
const tolerance = 0.05
|
||||
const standardRatios = [
|
||||
1.0,
|
||||
4 / 3,
|
||||
3 / 2,
|
||||
16 / 9,
|
||||
5 / 3,
|
||||
5 / 4,
|
||||
7 / 5,
|
||||
9 / 16,
|
||||
2 / 3,
|
||||
3 / 4,
|
||||
4 / 5
|
||||
]
|
||||
|
||||
for (const ratio of ratios) {
|
||||
// Find the closest standard ratio within tolerance
|
||||
let closestRatio = ratio
|
||||
let minDiff = Infinity
|
||||
|
||||
for (const standard of standardRatios) {
|
||||
const diff = Math.abs(ratio - standard)
|
||||
if (diff < minDiff && diff <= tolerance) {
|
||||
minDiff = diff
|
||||
closestRatio = standard
|
||||
}
|
||||
}
|
||||
|
||||
// If no standard ratio is close enough, keep original
|
||||
if (minDiff === Infinity || minDiff > tolerance) {
|
||||
closestRatio = ratio
|
||||
}
|
||||
|
||||
commonRatios[closestRatio] = (commonRatios[closestRatio] || 0) + 1
|
||||
}
|
||||
|
||||
// Find the most frequent ratio(s)
|
||||
let maxCount = 0
|
||||
const mostFrequent: number[] = []
|
||||
|
||||
for (const ratio of Object.keys(commonRatios)) {
|
||||
const ratioNum = parseFloat(ratio)
|
||||
const count = commonRatios[ratioNum] || 0
|
||||
if (count > maxCount) {
|
||||
maxCount = count
|
||||
mostFrequent.length = 0
|
||||
mostFrequent.push(ratioNum)
|
||||
} else if (count === maxCount) {
|
||||
mostFrequent.push(ratioNum)
|
||||
}
|
||||
}
|
||||
|
||||
// If only one most frequent ratio, return it
|
||||
if (mostFrequent.length === 1 && mostFrequent[0]) {
|
||||
return mostFrequent[0]
|
||||
}
|
||||
|
||||
// If multiple ratios have the same highest frequency, use median of them
|
||||
mostFrequent.sort((a, b) => a - b)
|
||||
const mid = Math.floor(mostFrequent.length / 2)
|
||||
return mostFrequent.length % 2 === 0
|
||||
? (mostFrequent[mid - 1]! + mostFrequent[mid]!) / 2
|
||||
: mostFrequent[mid]!
|
||||
}
|
||||
|
||||
function getAttachmentUrl(attachment: SnAttachment): string {
|
||||
return `${apiBase}/drive/files/${attachment.id}`
|
||||
}
|
||||
</script>
|
||||
@@ -20,16 +20,11 @@
|
||||
<div v-html="htmlContent" />
|
||||
</article>
|
||||
|
||||
<div
|
||||
v-if="props.item.attachments.length > 0"
|
||||
class="d-flex gap-2 flex-wrap"
|
||||
@click.stop
|
||||
>
|
||||
<attachment-item
|
||||
v-for="attachment in props.item.attachments"
|
||||
:key="attachment.id"
|
||||
:item="attachment"
|
||||
/>
|
||||
<attachment-list :attachments="props.item.attachments" :max-height="640" />
|
||||
|
||||
<div v-if="props.item.isTruncated" class="flex gap-2 text-xs opacity-80">
|
||||
<v-icon icon="mdi-dots-horizontal" size="small" />
|
||||
<p>Post truncated, tap to see details...</p>
|
||||
</div>
|
||||
|
||||
<!-- Post Reactions -->
|
||||
@@ -53,7 +48,7 @@ import { useMarkdownProcessor } from "~/composables/useMarkdownProcessor"
|
||||
import type { SnPost } from "~/types/api"
|
||||
|
||||
import PostHeader from "./PostHeader.vue"
|
||||
import AttachmentItem from "./AttachmentItem.vue"
|
||||
import AttachmentList from "./AttachmentList.vue"
|
||||
import PostReactionList from "./PostReactionList.vue"
|
||||
|
||||
const props = defineProps<{ item: SnPost }>()
|
||||
@@ -66,14 +61,13 @@ const { render } = useMarkdownProcessor()
|
||||
const htmlContent = ref<string>("")
|
||||
|
||||
function handleReaction(symbol: string, attitude: number, delta: number) {
|
||||
emit('react', symbol, attitude, delta)
|
||||
emit("react", symbol, attitude, delta)
|
||||
}
|
||||
|
||||
watch(
|
||||
props.item,
|
||||
(value) => {
|
||||
if (value.content)
|
||||
htmlContent.value = render(value.content)
|
||||
if (value.content) htmlContent.value = render(value.content)
|
||||
},
|
||||
{ immediate: true, deep: true }
|
||||
)
|
||||
|
||||
@@ -9,14 +9,14 @@
|
||||
@click="showReactionDialog"
|
||||
>
|
||||
<v-icon start size="16">mdi-plus</v-icon>
|
||||
<span class="text-caption">React</span>
|
||||
<span class="text-xs">React</span>
|
||||
</v-chip>
|
||||
|
||||
<!-- Existing Reactions -->
|
||||
<v-chip
|
||||
v-for="(count, symbol) in reactions"
|
||||
rounded
|
||||
:key="symbol"
|
||||
rounded
|
||||
:color="getReactionColor(symbol)"
|
||||
:disabled="submitting"
|
||||
@click="reactToPost(symbol)"
|
||||
@@ -38,7 +38,7 @@
|
||||
<div class="reaction-section">
|
||||
<div class="section-header d-flex align-center px-6 py-3">
|
||||
<v-icon class="me-2">mdi-emoticon-happy</v-icon>
|
||||
<span class="text-subtitle-1 font-weight-bold">Positive</span>
|
||||
<span class="font-bold">Positive</span>
|
||||
</div>
|
||||
<div class="reaction-grid">
|
||||
<v-card
|
||||
@@ -51,12 +51,12 @@
|
||||
>
|
||||
<div class="d-flex flex-column align-center justify-center pa-3">
|
||||
<span class="text-h4 mb-1">{{ reaction.emoji }}</span>
|
||||
<span class="text-caption text-center mb-1">{{
|
||||
<span class="text-xs text-center mb-1">{{
|
||||
reaction.symbol
|
||||
}}</span>
|
||||
<span
|
||||
v-if="getReactionCount(reaction.symbol) > 0"
|
||||
class="text-caption font-weight-bold"
|
||||
class="text-xs"
|
||||
>
|
||||
x{{ getReactionCount(reaction.symbol) }}
|
||||
</span>
|
||||
@@ -70,7 +70,7 @@
|
||||
<div class="reaction-section">
|
||||
<div class="section-header d-flex align-center px-6 py-3">
|
||||
<v-icon class="me-2">mdi-emoticon-neutral</v-icon>
|
||||
<span class="text-subtitle-1 font-weight-bold">Neutral</span>
|
||||
<span class="font-bold">Neutral</span>
|
||||
</div>
|
||||
<div class="reaction-grid">
|
||||
<v-card
|
||||
@@ -83,12 +83,12 @@
|
||||
>
|
||||
<div class="d-flex flex-column align-center justify-center pa-3">
|
||||
<span class="text-h4 mb-1">{{ reaction.emoji }}</span>
|
||||
<span class="text-caption text-center mb-1">{{
|
||||
<span class="text-xs text-center mb-1">{{
|
||||
reaction.symbol
|
||||
}}</span>
|
||||
<span
|
||||
v-if="getReactionCount(reaction.symbol) > 0"
|
||||
class="text-caption font-weight-bold"
|
||||
class="text-xs"
|
||||
>
|
||||
x{{ getReactionCount(reaction.symbol) }}
|
||||
</span>
|
||||
@@ -102,7 +102,7 @@
|
||||
<div class="reaction-section">
|
||||
<div class="section-header d-flex align-center px-6 py-3">
|
||||
<v-icon class="me-2">mdi-emoticon-sad</v-icon>
|
||||
<span class="text-subtitle-1 font-weight-bold">Negative</span>
|
||||
<span class="font-bold">Negative</span>
|
||||
</div>
|
||||
<div class="reaction-grid">
|
||||
<v-card
|
||||
@@ -115,12 +115,12 @@
|
||||
>
|
||||
<div class="d-flex flex-column align-center justify-center pa-3">
|
||||
<span class="text-h4 mb-1">{{ reaction.emoji }}</span>
|
||||
<span class="text-caption text-center mb-1">{{
|
||||
<span class="text-xs text-center mb-1">{{
|
||||
reaction.symbol
|
||||
}}</span>
|
||||
<span
|
||||
v-if="getReactionCount(reaction.symbol) > 0"
|
||||
class="text-caption font-weight-bold"
|
||||
class="text-xs"
|
||||
>
|
||||
x{{ getReactionCount(reaction.symbol) }}
|
||||
</span>
|
||||
@@ -135,7 +135,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, computed } from "vue"
|
||||
import { ref } from "vue"
|
||||
|
||||
interface Props {
|
||||
parentId: string
|
||||
@@ -209,20 +209,21 @@ async function reactToPost(symbol: string) {
|
||||
try {
|
||||
submitting.value = true
|
||||
const api = useSolarNetwork()
|
||||
const response = await api(`/sphere/posts/${props.parentId}/reactions`, {
|
||||
let statusCode = 200 // default status
|
||||
|
||||
await api(`/sphere/posts/${props.parentId}/reactions`, {
|
||||
method: "POST",
|
||||
body: {
|
||||
symbol: symbol,
|
||||
attitude: reaction.attitude
|
||||
},
|
||||
onResponse: (res) => {
|
||||
statusCode = res.response.status
|
||||
}
|
||||
})
|
||||
|
||||
// Check if we're removing the reaction (204 status) or adding (200)
|
||||
// In Nuxt, we can check the response status through the fetch response
|
||||
const isRemoving =
|
||||
response && typeof response === "object" && "status" in response
|
||||
? (response as any).status === 204
|
||||
: false
|
||||
const isRemoving = statusCode === 204
|
||||
const delta = isRemoving ? -1 : 1
|
||||
|
||||
emit("react", symbol, reaction.attitude, delta)
|
||||
@@ -243,14 +244,6 @@ function selectReaction(symbol: string) {
|
||||
reactToPost(symbol)
|
||||
}
|
||||
|
||||
// Computed properties and helper functions
|
||||
const totalReactionsCount = computed(() => {
|
||||
return Object.values(props.reactions || {}).reduce(
|
||||
(sum, count) => sum + count,
|
||||
0
|
||||
)
|
||||
})
|
||||
|
||||
function getReactionsByAttitude(attitude: number): ReactionTemplate[] {
|
||||
return availableReactions.filter((reaction) => reaction.attitude === attitude)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user