generated from stacks-network/.github
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
25 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 19 additions & 14 deletions
33
src/app/address/[principal]/TokenBalanceCard/useImageUrl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
'use client'; | ||
|
||
import { useEffect, useState } from 'react'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
export function useImageUrl(metadataImageUrl?: string) { | ||
const [contentType, setContentType] = useState<string | null>('image'); | ||
useEffect(() => { | ||
if (!metadataImageUrl) return; | ||
void fetch(metadataImageUrl) | ||
.then(response => { | ||
setContentType(response.headers.get('content-type')); | ||
}) | ||
.catch(() => { | ||
// corrupted image | ||
setContentType(null); | ||
}); | ||
}, [metadataImageUrl]); | ||
return { url: metadataImageUrl, contentType }; | ||
const { data: contentType } = useQuery({ | ||
queryKey: ['image-content-type', metadataImageUrl], | ||
queryFn: async () => { | ||
try { | ||
if (!metadataImageUrl) return 'image'; | ||
const response = await fetch(metadataImageUrl); | ||
return response.headers.get('content-type'); | ||
} catch (error) { | ||
return null; | ||
} | ||
}, | ||
enabled: !!metadataImageUrl, | ||
retry: false, | ||
staleTime: Infinity, | ||
refetchOnWindowFocus: false, | ||
}); | ||
|
||
return { url: metadataImageUrl, contentType: contentType ?? 'image' }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters