Skip to content

Commit

Permalink
fix: Move Download button
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarsen136 committed Jan 21, 2025
1 parent e68e906 commit f2b09a5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 56 deletions.
8 changes: 1 addition & 7 deletions components/gallery/GalleryItemButton/GalleryItemButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

<GalleryItemMoreActionBtn
:nft="nft"
:image-url="nftMetadata?.image"
:image-data="imageData"
:mime-type="nftMimeType"
:abi="abi"
data-testid="gallery-item-more-button"
/>
Expand All @@ -20,9 +17,8 @@ import GalleryItemMoreActionBtn from './GalleryItemMoreActionBtn.vue'
import { extractTwitterIdFromDescription } from '@/utils/parse'
const { $i18n } = useNuxtApp()
const imageData = ref()
const { getNft: nft, getNftMetadata: nftMetadata, getNftMimeType: nftMimeType, getAbi: abi } = storeToRefs(useNftStore())
const { getNft: nft, getAbi: abi } = storeToRefs(useNftStore())
const customSharingContent = computed(() => {
const twitterId = nft.value?.meta?.description
Expand All @@ -31,8 +27,6 @@ const customSharingContent = computed(() => {
return twitterId ? $i18n.t('sharing.nftWithArtist', [twitterId]) : ''
})
onKodahashRenderCompleted(({ payload }) => imageData.value = payload.image)
</script>

<style scoped lang="scss">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,12 @@ import { NeoButton, NeoDropdown, NeoDropdownItem } from '@kodadot1/brick'
import { Interaction } from '@kodadot1/minimark/v1'
import { useQuery } from '@tanstack/vue-query'
import GalleryItemEditNftButton from './GalleryItemEditNftButton.vue'
import { downloadImage } from '@/utils/download'
import { sanitizeIpfsUrl, toOriginalContentUrl } from '@/utils/ipfs'
import { isMobileDevice } from '@/utils/extension'
import { hasOperationsDisabled } from '@/utils/prefix'
import { refreshOdaTokenMetadata } from '@/services/oda'
import type { NFT } from '@/types'
import type { Abi } from '@/composables/transaction/types'
const { $i18n, $consola } = useNuxtApp()
const { $i18n } = useNuxtApp()
const { toast } = useToast()
const { isCurrentAccount } = useAuth()
const { transaction, isLoading, status } = useTransaction()
Expand All @@ -87,9 +84,6 @@ const route = useRoute()
const props = defineProps<{
nft?: NFT
mimeType?: string
imageUrl?: string
imageData?: string
abi?: Abi | null
}>()
Expand Down Expand Up @@ -122,47 +116,6 @@ const signingModalTitle = computed(() => {
)
})
const isDownloadEnabled = computed(() => {
const mimeType = props.mimeType
return ((
(mimeType?.includes('image') || mimeType?.includes('text/html'))
&& props.imageUrl) || props.imageData
)
})
const downloadMedia = async () => {
let imageUrl = sanitizeIpfsUrl(props.imageUrl)
if (!imageUrl) {
return
}
if (props.imageData) {
const blob = await $fetch<Blob>(props.imageData)
imageUrl = URL.createObjectURL(blob)
}
else if (props.mimeType?.includes('image')) {
imageUrl = toOriginalContentUrl(imageUrl)
}
if (isMobileDevice) {
toast($i18n.t('toast.downloadOnMobile'))
setTimeout(() => {
window.open(imageUrl, '_blank')
}, 2000)
return
}
try {
toast($i18n.t('toast.downloadImage'))
downloadImage(imageUrl, `${props.nft?.collection?.name}_${props.nft?.name}`)
}
catch (error) {
$consola.warn('[ERR] unable to fetch image')
toast($i18n.t('toast.downloadError'))
}
}
const burn = () => {
openUserCartModal('burn')
}
Expand Down
68 changes: 67 additions & 1 deletion components/gallery/GalleryItemToolBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@
class="text-k-grey"
/>
</NeoTooltip>
<NeoTooltip
v-if="isDownloadEnabled"
:label="$t('moreActions.download')"
position="top"
>
<a
no-shadow
@click="downloadMedia"
>
<NeoIcon
icon="arrow-down-to-line"
size="medium"
/>
</a>
</NeoTooltip>
</div>
</template>

Expand All @@ -65,6 +80,9 @@ import {
resolveMedia,
MediaType,
} from '@/utils/gallery/media'
import { downloadImage } from '@/utils/download'
import { sanitizeIpfsUrl, toOriginalContentUrl } from '@/utils/ipfs'
import { isMobileDevice } from '@/utils/extension'
type ReloadElement =
| HTMLIFrameElement
Expand All @@ -78,9 +96,12 @@ const props = defineProps<{
containerId: string
}>()
const { getNft: nft, getNftImage: nftImage, getNftMimeType: nftMimeType, getNftAnimation: nftAnimation, getNftAnimationMimeType: nftAnimationMimeType } = storeToRefs(useNftStore())
const { getNft: nft, getNftImage: nftImage, getNftMetadata: nftMetadata, getNftMimeType: nftMimeType, getNftAnimation: nftAnimation, getNftAnimationMimeType: nftAnimationMimeType } = storeToRefs(useNftStore())
const isLoading = ref(false)
const { toast } = useToast()
const { $i18n, $consola } = useNuxtApp()
const imageData = ref()
const image = computed(() => {
if (!nftImage.value) {
Expand All @@ -90,6 +111,49 @@ const image = computed(() => {
return nftImage.value
})
const nftImageUrl = computed(() => nftMetadata.value?.image)
const isDownloadEnabled = computed(() => {
const mimeType = nftMimeType.value
return ((
(mimeType?.includes('image') || mimeType?.includes('text/html'))
&& nftImageUrl.value) || imageData.value
)
})
const downloadMedia = async () => {
let imageUrl = sanitizeIpfsUrl(nftImageUrl.value)
if (!imageUrl) {
return
}
if (imageData.value) {
const blob = await $fetch<Blob>(imageData.value)
imageUrl = URL.createObjectURL(blob)
}
else if (nftMimeType.value?.includes('image')) {
imageUrl = toOriginalContentUrl(imageUrl)
}
if (isMobileDevice) {
toast($i18n.t('toast.downloadOnMobile'))
setTimeout(() => {
window.open(imageUrl, '_blank')
}, 2000)
return
}
try {
toast($i18n.t('toast.downloadImage'))
downloadImage(imageUrl, `${nft.value?.collection?.name}_${nft.value?.name}`)
}
catch (error) {
$consola.warn('[ERR] unable to fetch image')
toast($i18n.t('toast.downloadError'))
}
}
const mediaAndImageType = computed(() => {
const animationMediaType = resolveMedia(nftAnimationMimeType.value)
const imageMediaType = resolveMedia(nftMimeType.value)
Expand Down Expand Up @@ -160,4 +224,6 @@ const disableNewTab = computed(() => {
return nftImage.value && nftMimeType.value
})
onKodahashRenderCompleted(({ payload }) => imageData.value = payload.image)
</script>

0 comments on commit f2b09a5

Please sign in to comment.