Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement ImgurFallbackImage component #167

Merged
merged 2 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ jobs:
run: |
npm ci
echo "VUE_APP_BASE_URL=https://staging.disfactory.tw/api" > .env
echo "VUE_APP_IMGUR_FALLBACK_URL=https://staging.disfactory.tw/imgur" >> .env
npm run build
- name: Deploy to GitHub Pages
uses: JamesIves/github-pages-deploy-action@3.7.1
Expand Down Expand Up @@ -95,6 +96,7 @@ jobs:
run: |
npm ci
echo "VUE_APP_BASE_URL=https://api.disfactory.tw/api" > .env.production
echo "VUE_APP_IMGUR_FALLBACK_URL=https://api.disfactory.tw/imgur" >> .env.production
npm run build
echo "disfactory.tw" > dist/CNAME
- name: Deploy to GitHub Pages
Expand Down
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>