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

Fall back to showing both downloads if architecture detection fails #710

Merged
merged 2 commits into from
Jun 17, 2024
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
78 changes: 64 additions & 14 deletions components/NodeApp/Download/Download.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,76 @@ export function DownloadForCurrentPlatform({ downloadUrlsByPlatform }: Props) {

const linkProps = useMemo(() => {
if (!linkData)
return {
opacity: 0,
};
return [
{
opacity: 0,
},
];

const { url, label } = linkData;
return linkData.map(({ url, label, platform }) => {
if (url && label && platform) {
if (linkData.length === 1) {
return { href: url, children: "Download Now" };
}

return url && label
? { href: url, children: "Download Now" }
: { href: REPO_URL, children: "View on GitHub", target: "_blank" };
const downloadButtonLabel: string = (() => {
switch (platform) {
case "mac-arm": {
return "Apple Silicon";
}
case "mac-intel": {
return "Intel";
}
case "windows": {
return "Windows";
}
}
})();

return {
href: url,
children:
linkData.length === 1
? "Download Now"
: `Download for ${downloadButtonLabel}`,
};
}
if (!url || !label || !platform)
return { href: REPO_URL, children: "View on GitHub", target: "_blank" };

return url && label && platform
? {
href: url,
children:
linkData.length === 1
? "Download Now"
: `Download for ${platform}`,
}
: { href: REPO_URL, children: "View on GitHub", target: "_blank" };
});
}, [linkData]);

return (
<Flex direction="column" alignItems="center" maxW="100%">
<DownloadButton as="a" size="lg" mb={4} {...linkProps} />
{linkData?.label && (
<>
<Text>{linkData.label}</Text>
<Box my={3} w="120px" borderBottom="1px dashed black" />
</>
)}
<Flex
direction="row"
gap={4}
flexWrap="wrap"
justifyContent="center"
mb={linkProps.length > 1 ? 8 : 0}
>
{linkProps.map((lp, i) => (
<Flex direction="column" alignItems="center" key={i}>
<DownloadButton as="a" size="lg" {...lp} />
{linkProps.length === 1 && (
<>
<Text mt={4}>{linkData && linkData[i].label}</Text>
<Box my={3} w="120px" borderBottom="1px dashed black" />
</>
)}
</Flex>
))}
</Flex>
{downloadUrlsByPlatform && (
<Text
as="a"
Expand Down
60 changes: 43 additions & 17 deletions utils/nodeAppUrl/useDownloadLinkForPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import { UAParser } from "ua-parser-js";
export function useDownloadLinkForPlatform(
downloadUrlsByPlatform?: DownloadUrlsByPlatform
) {
const [platformData, setPlatformData] = useState<{
url: string | null;
label: string | null;
platform: Platform | null;
} | null>(null);
const [platformData, setPlatformData] = useState<
| {
url: string | null;
label: string | null;
platform: Platform | null;
}[]
| null
>(null);

useEffect(() => {
const run = async () => {
Expand All @@ -40,7 +43,7 @@ async function getDataForPlatform(
};

if (!downloadUrlsByPlatform) {
return notFoundOption;
return [notFoundOption];
}

if (ua.os.name === "Mac OS") {
Expand All @@ -60,26 +63,49 @@ async function getDataForPlatform(
}
} catch (err) {}

return architecture === "arm"
? {
if (architecture === "arm") {
return [
{
platform: PLATFORMS.MAC_ARM,
label: "macOS (Apple Silicon)",
url: downloadUrlsByPlatform[PLATFORMS.MAC_ARM],
}
: {
},
];
}

if (architecture === "x86") {
return [
{
platform: PLATFORMS.MAC_INTEL,
label: "macOS (Intel)",
url: downloadUrlsByPlatform[PLATFORMS.MAC_INTEL],
};
},
];
}

return [
{
platform: PLATFORMS.MAC_ARM,
label: "macOS (Apple Silicon)",
url: downloadUrlsByPlatform[PLATFORMS.MAC_ARM],
},
{
platform: PLATFORMS.MAC_INTEL,
label: "macOS (Intel)",
url: downloadUrlsByPlatform[PLATFORMS.MAC_INTEL],
},
];
}

if (ua.os.name === "Windows") {
return {
platform: PLATFORMS.WINDOWS,
label: "Windows",
url: downloadUrlsByPlatform[PLATFORMS.WINDOWS],
};
return [
{
platform: PLATFORMS.WINDOWS,
label: "Windows",
url: downloadUrlsByPlatform[PLATFORMS.WINDOWS],
},
];
}

return notFoundOption;
return [notFoundOption];
}
Loading