Skip to content
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
14 changes: 12 additions & 2 deletions src/components/landing/landingButtons.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import Button from '../button';

const downloadLink = 'https://github.com/unitystation/stationhub/releases';
import getDownloadLink from '../../lib/platform';
const githubUrl = 'https://github.com/unitystation/unitystation';
const discordInviteUrl = 'https://discord.com/invite/tFcTpBp';
const githubReleases = 'https://github.com/unitystation/stationhub/releases/latest';

import {useEffect, useState} from 'react';



const LandingButtons = () => {
const [downloadLink, setDownloadLink] = useState(githubReleases);

useEffect(() => {
getDownloadLink().then((link) => setDownloadLink(link));
}, []);

return (<div className={'flex flex-wrap justify-center mt-8 gap-4'}>
<Button filled={true} text={'Download'} linkTo={downloadLink}/>
<Button filled={false} text={'Github'} linkTo={githubUrl}/>
Expand Down
71 changes: 71 additions & 0 deletions src/lib/platform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import UAParser from 'ua-parser-js';
import GithubReleaseResponse from '../types/githubReleaseResponse';

function getUserPlatform(): string | undefined {
const parser = new UAParser();
const result = parser.getResult();
const { os } = result;
console.log(result);
let minifiedPlatform: string | undefined;

switch (os.name?.toLowerCase()) {
case 'mac os':
minifiedPlatform = 'mac';
break;
case 'windows':
minifiedPlatform = 'win';
break;
case 'linux':
case 'fedora' :
case 'debian' :
case 'gentoo' :
case 'ubuntu' :
case 'mint':
case 'pcLinuxos':
case 'redhat' :
case 'suse':
case 'slackware':
case 'manjaro':
case 'arch':
minifiedPlatform = 'lin';
break;
default:
minifiedPlatform = undefined;
break;
}

return minifiedPlatform;
}

async function getDownloadLinkForPlatform(platform: string | undefined): Promise<string> {
let defaultDownloadLink = 'https://github.com/unitystation/stationhub/releases/latest';

if (platform === undefined) {
return defaultDownloadLink;
}

return await fetchGithubReleases().then(
(response: GithubReleaseResponse) => {
const assets = response.assets;
const asset = assets.find(asset => asset.name.includes(platform));
if (asset !== undefined) {
defaultDownloadLink = asset.browser_download_url;
}

return defaultDownloadLink;
});
}

function fetchGithubReleases(): Promise<GithubReleaseResponse> {
const githubReleasesApi = 'https://api.github.com/repos/unitystation/stationhub/releases/latest';
return fetch(githubReleasesApi).then( response => {
return response.json()
});
}

function getDownloadLink(): Promise<string> {
const platform = getUserPlatform();
return getDownloadLinkForPlatform(platform);
}

export default getDownloadLink;
Loading