Skip to content

Commit

Permalink
feat: implement ImgurFallbackImage component
Browse files Browse the repository at this point in the history
  • Loading branch information
Yukaii committed May 16, 2023
1 parent 1e7bdff commit f7b7a6a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
VUE_APP_BASE_URL=https://staging.disfactory.tw/api
VUE_APP_IMGUR_FALLBACK_URL=https://api.disfactory.tw/imgur
6 changes: 5 additions & 1 deletion src/components/FactoryDetailPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</div>
</v-slide-item>
<v-slide-item v-for="(image, index) in images" class="mr-4" :key="image.id" :class="{ 'ml-4': index === 0 }" @click.native="setLightboxIndex(index)">
<img :src="image.url" class="factory-slide-image" />
<ImgurFallbackImage :src="image.url" className="factory-slide-image" />
</v-slide-item>
</v-slide-group>

Expand Down Expand Up @@ -145,9 +145,13 @@ import { getFactoryReportRecords } from '@/api'
import { useAppState } from '../lib/appState'
import { FactoryImage, getDisplayStatusText, ReportRecord } from '../types'
import ImgurFallbackImage from './ImgurFallbackImage.vue'
export default createComponent({
name: 'FactoryDetailPage',
components: {
ImgurFallbackImage
},
setup () {
const [appState, { pageTransition, expandFactoryDetail, collapseFactoryDetail, toggleFactoryDetail }] = useAppState()
Expand Down
58 changes: 58 additions & 0 deletions src/components/ImgurFallbackImage.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<template>
<img :src="imageUrl" :alt="alt" :class="className" @error="onError" />
</template>

<script lang="ts">
import { createComponent, computed, ref, onUpdated, watch } from '@vue/composition-api'
const IMGUR_REGEX = /i\.imgur\.com\/([a-zA-Z0-9]+)\.([a-zA-Z0-9]+)(\?.*)?$/
const imgurFallbackBaseUrl = process.env.NODE_ENV === 'production' ? process.env.VUE_APP_IMGUR_FALLBACK_URL : '/server/imgur'
export default createComponent({
name: 'ImgurFallbackImage',
props: {
src: {
type: String,
required: true
},
alt: {
type: String,
default: ''
},
className: {
type: String,
default: ''
}
},
setup (props, context) {
const matches = IMGUR_REGEX.exec(props.src)
const imgurInfo = computed(() => {
return matches ? {
id: matches[1],
ext: matches[2]
} : null
})
const fallbackUrl = computed(() => {
if (imgurInfo.value) {
return `${imgurFallbackBaseUrl}/${imgurInfo.value.id}.${imgurInfo.value.ext}`
} else {
return props.src
}
})
const imageUrl = ref(fallbackUrl.value)
const onError = () => {
imageUrl.value = props.src
}
return {
imageUrl,
onError
}
}
})
</script>

0 comments on commit f7b7a6a

Please sign in to comment.