-
Notifications
You must be signed in to change notification settings - Fork 2
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
8 changed files
with
249 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Button , Text, Link, Spinner} from '@chakra-ui/react'; | ||
import { Icon } from '@chakra-ui/icons'; | ||
import {useRecoilValue} from 'recoil'; | ||
import { platformLatestDataSelector } from '../states'; | ||
import { getOperationSystem } from '../libs/helper'; | ||
import { FaWindows , FaApple , FaLinux, FaDownload } from "react-icons/fa"; | ||
import { OperationSystem } from '../types'; | ||
|
||
|
||
|
||
export function getPlaformIconUrl(platform?: OperationSystem) { | ||
const operationSystem = platform ?? getOperationSystem(); | ||
switch (operationSystem) { | ||
case "windows": | ||
return FaWindows ; | ||
case "mac": | ||
return FaApple ; | ||
case "linux": | ||
return FaLinux ; | ||
default: | ||
return FaDownload ; | ||
} | ||
} | ||
|
||
export default function DownloadButton() { | ||
const platformLatestDataInfo = useRecoilValue(platformLatestDataSelector); | ||
return ( | ||
<Link href={platformLatestDataInfo?.url} download> | ||
<Button rightIcon={platformLatestDataInfo ? <Icon as={getPlaformIconUrl()} /> : <Spinner />} colorScheme='teal' variant='solid' size='lg' > | ||
{ | ||
!platformLatestDataInfo ? 'Loading...' : ( | ||
<> | ||
<Text> | ||
Download {platformLatestDataInfo?.version} | ||
</Text> | ||
</> | ||
) | ||
} | ||
</Button> | ||
</Link> | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import type { OperationSystem, PlatformData, githubLatestReleaseData } from "../types"; | ||
|
||
|
||
const API_URL = "https://api.github.com/repos/codad5/google-task-tauri/releases/latest"; | ||
|
||
|
||
const OsToPlatform : Record<OperationSystem, string[]> = { | ||
"windows": ["exe"], | ||
"mac": ["dmg"], | ||
"linux": ["AppImage", "deb"], | ||
"other": [], | ||
"darwin": ["dmg"], | ||
}; | ||
|
||
|
||
|
||
|
||
export function getOperationSystem() : OperationSystem { | ||
// get operation system from user agent | ||
const userAgent = navigator.userAgent.toLowerCase(); | ||
if (userAgent.indexOf("win32") >= 0 || userAgent.indexOf("wow32") >= 0) { | ||
return "windows"; | ||
} | ||
if (userAgent.indexOf("win64") >= 0 || userAgent.indexOf("wow64") >= 0) { | ||
return "windows"; | ||
} | ||
if (userAgent.indexOf("mac") >= 0) { | ||
return "mac"; | ||
} | ||
if (userAgent.indexOf("linux") >= 0) { | ||
return "linux"; | ||
} | ||
return "other"; | ||
} | ||
|
||
export async function getLatestJson(): Promise<githubLatestReleaseData|null> { | ||
const latestJson = localStorage.getItem("latestJson"); | ||
if (latestJson) return JSON.parse(latestJson); | ||
return fetch(API_URL).then((response) => { | ||
localStorage.setItem("latestJson", JSON.stringify(response.json())); | ||
// check response status | ||
if (response.status !== 200) return null; | ||
return response.json() | ||
}); | ||
} | ||
|
||
|
||
export async function getLatestVersionData(operationSystem: OperationSystem) : Promise<PlatformData|null> { | ||
const latestJson = await getLatestJson(); | ||
if (!latestJson || !latestJson.assets) return null; | ||
console.log(latestJson); | ||
const version = latestJson.tag; | ||
const date = latestJson.published_at; | ||
const osPlatform = OsToPlatform[operationSystem]; | ||
const data = latestJson.assets.find((asset) => { | ||
return osPlatform.includes(asset.name.split(".").pop() ?? ""); | ||
}) | ||
return data ? { | ||
platform: operationSystem, | ||
url: data.browser_download_url, | ||
version, | ||
date, | ||
} : null; | ||
} | ||
|
||
export async function getLatestVersionDataFOrThisPlatform() : Promise<PlatformData|null> { | ||
return await getLatestVersionData(getOperationSystem()); | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { atom, selector } from 'recoil'; | ||
import { PlatformData } from './types'; | ||
|
||
export const platformLatestData = atom<PlatformData | null>({ | ||
key: 'platformLatestData', | ||
default: null, | ||
}); | ||
|
||
|
||
export const platformLatestDataSelector = selector<PlatformData | null>({ | ||
key: 'platformLatestDataSelector', | ||
get: ({ get }) => { | ||
const platformData = get(platformLatestData); | ||
return platformData; | ||
}, | ||
}); | ||
|
||
export const lastestVersion = selector<string | null>({ | ||
key: 'lastestVersionSelector', | ||
get: ({ get }) => { | ||
const platformData = get(platformLatestData); | ||
return platformData?.version ?? null; | ||
}, | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export type OperationSystem = "windows" | "mac" | "linux" | "other" | "darwin" | ||
|
||
export type PlatformData = { | ||
platform: string; | ||
url: string; | ||
version: string; | ||
date: string; | ||
|
||
}; | ||
|
||
export type githubPlaformData = { | ||
url: string; | ||
name: string; | ||
browser_download_url: string; | ||
download_count: number; | ||
size: number; | ||
}; | ||
|
||
export type githubPlaformsData = githubPlaformData[] | ||
|
||
export type githubLatestReleaseData = { | ||
name: string; | ||
published_at: string; | ||
body: string; | ||
assets: githubPlaformsData; | ||
tag: string; | ||
draft: boolean; | ||
}; |