add retry for preview image (#7845)

Signed-off-by: denis-tingaikin <denis.tingajkin@xored.com>
This commit is contained in:
Denis Tingaikin 2025-01-31 05:57:54 +03:00 committed by GitHub
parent cadaeb7e0e
commit f3ab8a8173
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -13,7 +13,7 @@
// limitations under the License. // limitations under the License.
// --> // -->
<script lang="ts"> <script lang="ts">
import { getJsonOrEmpty, type LinkPreviewDetails, canDisplayLinkPreview } from '@hcengineering/presentation' import { getJsonOrEmpty, type LinkPreviewDetails } from '@hcengineering/presentation'
import { type Attachment } from '@hcengineering/attachment' import { type Attachment } from '@hcengineering/attachment'
import { type WithLookup } from '@hcengineering/core' import { type WithLookup } from '@hcengineering/core'
import { Spinner } from '@hcengineering/ui' import { Spinner } from '@hcengineering/ui'
@ -22,12 +22,27 @@
export let attachment: WithLookup<Attachment> export let attachment: WithLookup<Attachment>
let useDefaultIcon = false let useDefaultIcon = false
let retryCount = 0
let viewModel: LinkPreviewDetails let viewModel: LinkPreviewDetails
let previewImageSrc: string | undefined
function refreshPreviewImage (): void {
if (viewModel?.image === undefined) {
return
}
if (retryCount > 3) {
previewImageSrc = undefined
return
}
retryCount++
previewImageSrc = `${viewModel.image}#${Date.now()}`
}
onMount(() => { onMount(() => {
void getJsonOrEmpty(attachment.file, attachment.name) void getJsonOrEmpty(attachment.file, attachment.name)
.then((res) => { .then((res) => {
viewModel = res as LinkPreviewDetails viewModel = res as LinkPreviewDetails
refreshPreviewImage()
}) })
.catch((err) => { .catch((err) => {
console.error(err) console.error(err)
@ -63,9 +78,16 @@
{#if viewModel.description} {#if viewModel.description}
{viewModel.description} {viewModel.description}
{/if} {/if}
{#if viewModel.image} {#if previewImageSrc}
<a target="_blank" href={viewModel.url}> <a target="_blank" href={viewModel.url}>
<img src={viewModel.image} class="round-image" alt="link-preview" /> <img
src={previewImageSrc}
class="round-image"
alt="link-preview"
on:error={() => {
refreshPreviewImage()
}}
/>
</a> </a>
{/if} {/if}
</div> </div>