Skip to content

Commit

Permalink
feat: github release cdn
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Jul 30, 2023
1 parent ba8da45 commit 07e3efb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
47 changes: 33 additions & 14 deletions components/HeroSection.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { useTranslation } from 'next-i18next'
import Image from 'next/image'
import Container from './Container'
import Link from 'next/link'
import UAParser from 'ua-parser-js'
import androidSvg from '../public/images/platform/android.svg'
import linuxSvg from '../public/images/platform/linux.svg'
import macosSvg from '../public/images/platform/macos.svg'
import windowsSvg from '../public/images/platform/windows.svg'
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'
import UAParser from 'ua-parser-js'
import Container from './Container'

const HeroSection = ({ release }) => {
const router = useRouter()
const isCn = router.locale == 'zh-CN'
const [githubAccessible, setGithubAccessible] = useState(true)
const [downloadUrl, setDownloadUrl] = useState('')

useEffect(() => {
if (!isCn) {
setGithubAccessible(true)
return
}
fetch('https://api.github.com').catch(() => {
setGithubAccessible(false)
})
}, [isCn])

useEffect(() => {
const parser = new UAParser()
const os = parser.getOS().name
Expand All @@ -30,20 +41,28 @@ const HeroSection = ({ release }) => {
ua.os = 'android'
}

const getDownloadCdnUrl = (rawUrl) => {
if (!githubAccessible) {
return `https://ghproxy.com/${rawUrl.replace('//', '/')}`
} else {
return rawUrl
}
}

const osAssert = release.assert[ua.os]
// first match os and arch
if (osAssert?.[ua.arch]) {
setDownloadUrl(osAssert[ua.arch])
setDownloadUrl(getDownloadCdnUrl(osAssert[ua.arch]))
return
}
// second match os
if (osAssert) {
setDownloadUrl(osAssert[Object.keys(osAssert)[0]])
setDownloadUrl(getDownloadCdnUrl(osAssert[Object.keys(osAssert)[0]]))
return
}
// no match, fallback to github latest release
setDownloadUrl('https://github.com/GopeedLab/gopeed/releases/latest')
}, [release.assert])
}, [githubAccessible, release.assert])

const { t } = useTranslation('common')
return (
Expand All @@ -70,7 +89,7 @@ const HeroSection = ({ release }) => {
</Link>
<Link
href="https://github.com/GopeedLab/gopeed/releases/latest"
className="relative flex h-12 w-full items-center justify-center px-6 before:absolute before:inset-0 before:rounded-lg before:border before:border-transparent before:bg-primary/10 before:bg-gradient-to-b before:transition before:duration-300 hover:before:scale-105 active:duration-75 active:before:scale-95 dark:before:border-gray-800 dark:before:bg-gray-900 sm:w-max"
className="relative flex h-12 w-full items-center justify-center px-6 before:absolute before:inset-0 before:rounded-lg before:bg-primary/10 before:bg-gradient-to-b before:transition before:duration-300 hover:before:scale-105 active:duration-75 active:before:scale-95 sm:w-max"
>
<span className="relative text-base font-semibold text-primary dark:text-white">{t('downloadMore')}</span>
</Link>
Expand All @@ -83,16 +102,16 @@ const HeroSection = ({ release }) => {
</div>
</div>
</div>
<div className="hidden py-8 mt-16 border-y border-gray-100 dark:border-gray-800 sm:flex justify-between">
<div className="text-left">
<div className="py-8 mt-16 border-y border-gray-100 dark:border-gray-800 sm:flex justify-between">
<div className="text-left pb-4 lg:pb-0">
<h6 className="text-lg font-semibold text-gray-700 dark:text-white">{t('home.feat1.title')}</h6>
<p className="mt-2 text-gray-500">{t('home.feat1.desc')}</p>
</div>
<div className="text-left">
<div className="text-left pb-4 lg:pb-0">
<h6 className="text-lg font-semibold text-gray-700 dark:text-white">{t('home.feat2.title')}</h6>
<p className="mt-2 text-gray-500">{t('home.feat2.desc')}</p>
</div>
<div className="text-left">
<div className="text-left pb-4 lg:pb-0">
<h6 className="text-lg font-semibold text-gray-700 dark:text-white">{t('home.feat3.title')}</h6>
<p className="mt-2 text-gray-500">{t('home.feat3.desc')}</p>
</div>
Expand Down
2 changes: 1 addition & 1 deletion components/Translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const Translate = (props) => {
} else {
return (
<a
href="void:javascript(0)"
href="#"
onClick={changeLocale(locale)}
className={clsx(
'flex items-center justify-between px-3 py-1',
Expand Down
4 changes: 2 additions & 2 deletions public/locales/en-US/common.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"selectLanguage": "Select Language",
"donate": "Donate",
"download": "Download App",
"downloadMore": "Download More",
"download": "Download",
"downloadMore": "Get More",
"docs": "Docs",
"home": "Home",
"contact": "Contact",
Expand Down
2 changes: 1 addition & 1 deletion public/locales/zh-CN/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"selectLanguage": "选择语言",
"donate": "捐赠",
"download": "立即下载",
"downloadMore": "更多下载",
"downloadMore": "获取更多",
"docs": "文档",
"home": "主页",
"contact": "联系",
Expand Down

0 comments on commit 07e3efb

Please sign in to comment.