import { __, sprintf } from '../lib/i18n'; import { useState } from 'preact/hooks'; import { AttachmentImage } from './AttachmentImage'; import { ImageAssetActions } from './ImageAssetActions'; import { Icon } from './Icon'; import { attachmentUrl } from '../lib/api'; import type { ReviewAsset, ReviewComment } from '../types'; function isPromotableImage(asset: ReviewAsset): boolean { return asset.kind === 'attachment' && asset.disposition === 'transient' && /^image\//i.test(asset.mimeType); } function assetDisplayRank(asset: ReviewAsset): number { if (asset.kind === 'screenshot') return 2; return /^image\//i.test(asset.mimeType) ? 0 : 1; } interface Props { comment: ReviewComment; isOwner: boolean; reviewId?: number; onPromoteAsset: (asset: ReviewAsset, comment: ReviewComment) => Promise; onPreviewAsset: (asset: ReviewAsset, comment: ReviewComment) => void; } export function CommentAssets({ comment, isOwner, reviewId, onPromoteAsset, onPreviewAsset }: Props) { const [promotingAssetId, setPromotingAssetId] = useState(); const number = comment.commentNumber ?? comment.id; const assets = [...(comment.assets ?? [])].sort((left, right) => assetDisplayRank(left) - assetDisplayRank(right)); const promote = async (asset: ReviewAsset) => { setPromotingAssetId(asset.id); try { await onPromoteAsset(asset, comment); } finally { setPromotingAssetId(undefined); } }; return <> {assets.length > 0 ? (
{assets.map((asset) => { const canPromote = Boolean(isOwner && reviewId && isPromotableImage(asset)); const promoting = promotingAssetId === asset.id; const isScreenshot = asset.kind === 'screenshot'; const isImage = /^image\//i.test(asset.mimeType); if (isScreenshot || isImage) { const previewLabel = isScreenshot ? (comment.screenshotState === 'partial' ? __('Preview partial context screenshot', 'commentmate') : __('Preview context screenshot', 'commentmate')) : sprintf(__("Preview %s", 'commentmate'), String(asset.originalName)); return (
{!isScreenshot ? ( { void promote(asset); }} /> ) : null}
); } return (
{asset.originalName} {canPromote ? ( ) : null}
); })}
) : null} {comment.screenshotState === 'unavailable' ?

{__("Screenshot unavailable", 'commentmate')}

: null} ; }