import { resolveMediaUrl } from "@/lib/media-url"; import type { ApiComment, ApiPost, ApiUser } from "@/types/api"; function asUser(value: ApiUser | string | undefined | null) { if (!value || typeof value === "string") { return null; } return value; } export function getPostAuthor(post: ApiPost) { return asUser(post.author ?? post.authorId); } export function getCommentAuthor(comment: ApiComment) { return asUser(comment.author ?? comment.authorId); } export function getUserLabel(user: ApiUser | null) { if (!user) { return "-"; } return user.name ?? user.stageName ?? user.username ?? user.email ?? "-"; } export function getPostPreviewMedia(post: ApiPost) { const imageCount = post.imageUrls?.length ?? 0; const imageUrl = resolveMediaUrl(post.imageUrls?.[0]); const thumbnailUrl = resolveMediaUrl(post.thumbnailUrl); const videoUrl = resolveMediaUrl(post.videoUrl); const audioUrl = resolveMediaUrl(post.audioUrl); if (imageCount > 0 && imageUrl) { return { kind: "image" as const, url: imageUrl, sourceUrl: "", count: imageCount, }; } if (thumbnailUrl) { return { kind: post.postType === "audio" ? ("audio" as const) : ("video" as const), url: thumbnailUrl, sourceUrl: post.postType === "video" ? videoUrl : audioUrl, count: 1, }; } if (post.postType === "video" && videoUrl) { return { kind: "video" as const, url: "", sourceUrl: videoUrl, count: 1, }; } return { kind: post.postType === "audio" ? ("audio" as const) : post.postType === "video" ? ("video" as const) : ("text" as const), url: "", sourceUrl: post.postType === "video" ? videoUrl : post.postType === "audio" ? audioUrl : "", count: 0, }; }