Skip to content

Commit 76d0bb3

Browse files
Create useDetectOS hook
1 parent ccae7a3 commit 76d0bb3

File tree

3 files changed

+58
-42
lines changed

3 files changed

+58
-42
lines changed
Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
1-
import React, { useState, useEffect } from "react";
2-
import { museHubReleases, audacityReleases } from "../../assets/js/releaseData";
1+
import { museHubReleases } from "../../assets/js/releaseData";
32
import "../../styles/icons.css";
4-
import DownloadMuseHubButton from "../button/DownloadMuseHubButton";
5-
import platform from "platform";
6-
declare interface Props {
7-
url: string;
8-
}
3+
import useBrowserOS from "../../hooks/useDetectOS";
94

10-
function PromoBanner({ url }: Props) {
11-
//no promo running
12-
//return null;
13-
const [showBanner, setShowBanner] = useState(true);
5+
function PromoBanner() {
6+
const browserOS = useBrowserOS();
147

15-
function getHref() {
16-
switch(platform.os.family){
17-
case "OS X":
18-
return museHubReleases.mac[0].browser_download_url;
19-
case "Windows":
20-
return museHubReleases.win[0].browser_download_url;
21-
default:
22-
setShowBanner(false); return "#";
8+
const getHref = () => {
9+
if (browserOS === "OS X") {
10+
return museHubReleases.mac[0].browser_download_url;
11+
} else if (browserOS === "Windows") {
12+
return museHubReleases.win[0].browser_download_url;
2313
}
24-
}
14+
return "#"; // Default if OS is not supported
15+
};
16+
17+
// Only show the banner for supported OSes
18+
const showBanner = browserOS === "OS X" || browserOS === "Windows";
2519

2620
function handleButtonClick() {
2721
if (typeof _paq !== "undefined") {
@@ -34,29 +28,32 @@ function PromoBanner({ url }: Props) {
3428
}
3529
}
3630

37-
return ( <> { showBanner &&
38-
<div
39-
id="promo-banner"
40-
className="flex items-center justify-center min-h-24 bg-yellow-300 gap-4 flex-wrap"
41-
>
42-
<div className="flex gap-2 flex-wrap my-4 mx-2">
43-
<p className="text-lg text-gray-900">🔥Limited-Time Offer: 80% OFF</p>
44-
<p className="text-lg text-gray-900 font-bold">
45-
Polyspectral MBC Multiband Compressor! Now $9.99🔥
46-
</p>
47-
</div>
48-
49-
<a
50-
href={getHref()}
51-
id="promo-button"
52-
onClick={handleButtonClick}
53-
className="flex text-lg font-bold h-12 my-4 justify-center items-center px-4 border-2 border-gray-900 rounded-md hover:bg-gray-900 hover:text-white"
31+
return (
32+
<>
33+
{showBanner && (
34+
<div
35+
id="promo-banner"
36+
className="flex flex-col lg:flex-row justify-center items-center min-h-24 bg-yellow-300 py-4 gap-4 lg:gap-8"
5437
>
55-
Get it on the Muse Hub
56-
</a>
57-
</div>
58-
} </>
38+
<div className="lg:flex text-center">
39+
<p className="text-lg text-gray-900">🔥Limited-Time Offer: 80% OFF</p>
40+
<p className="text-lg text-gray-900 font-bold">
41+
Polyspectral MBC Multiband Compressor! Now $9.99🔥
42+
</p>
43+
</div>
44+
45+
<a
46+
href={getHref()}
47+
id="promo-button"
48+
onClick={handleButtonClick}
49+
className="flex text-lg font-bold h-12 justify-center items-center px-4 border-2 border-gray-900 rounded-md hover:bg-gray-900 hover:text-white"
50+
>
51+
Get it on the Muse Hub
52+
</a>
53+
</div>
54+
)}
55+
</>
5956
);
6057
}
6158

62-
export default PromoBanner;
59+
export default PromoBanner;

src/hooks/useDetectOS.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { useState, useEffect } from 'react';
2+
import platform from 'platform';
3+
4+
const useBrowserOS = () => {
5+
// Define the state with an explicit string type or null
6+
const [browserOS, setBrowserOS] = useState<string | null>(null);
7+
8+
useEffect(() => {
9+
// Set the OS using the platform library, ensuring it's a string
10+
const detectedOS = platform.os?.family ?? 'Unknown OS';
11+
setBrowserOS(detectedOS);
12+
}, []);
13+
14+
return browserOS;
15+
};
16+
17+
export default useBrowserOS;

src/layouts/BaseLayout.astro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import "../styles/input.css";
66
import BetaBanner from "../components/banner/BetaBanner";
77
import SurveyBanner from "../components/banner/SurveyBanner";
88
import PromoBanner from "../components/banner/PromoBanner";
9+
import useBrowserOS from "../hooks/useDetectOS";
910
1011
export interface Props {
1112
title: string;
@@ -15,6 +16,7 @@ const {
1516
title = "Audacity | Free Audio editor, recorder, music making and more!",
1617
description = "Audacity is the world's most popular audio editing and recording app. Edit, mix, and enhance your audio tracks with the power of Audacity. Download now!",
1718
} = Astro.props;
19+
1820
---
1921

2022
<!doctype html>

0 commit comments

Comments
 (0)