Skip to content

Commit 46f7519

Browse files
committed
conditionally hide push notification button
1 parent eef2038 commit 46f7519

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

site/src/contexts/usePushNotifications.ts

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { useQuery } from "react-query";
77
interface PushNotifications {
88
readonly subscribed: boolean;
99
readonly loading: boolean;
10+
readonly enabled: boolean;
1011

1112
subscribe(): Promise<void>;
1213
unsubscribe(): Promise<void>;
@@ -19,6 +20,17 @@ export const usePushNotifications = (): PushNotifications => {
1920
const [subscribed, setSubscribed] = useState<boolean>(false);
2021
const [loading, setLoading] = useState<boolean>(true);
2122

23+
// Disable push notifications if the server is not configured to send them.
24+
const enabled = !!buildInfoQuery.data?.push_notifications_public_key;
25+
if (!enabled) {
26+
return {
27+
enabled: false,
28+
subscribed: false,
29+
loading: false,
30+
subscribe: async () => {},
31+
unsubscribe: async () => {},
32+
};
33+
}
2234
useEffect(() => {
2335
// Check if browser supports push notifications
2436
if (!("Notification" in window) || !("serviceWorker" in navigator)) {
@@ -95,6 +107,7 @@ export const usePushNotifications = (): PushNotifications => {
95107
};
96108

97109
return {
110+
enabled,
98111
subscribed,
99112
loading: loading || buildInfoQuery.isLoading,
100113
subscribe,

site/src/modules/dashboard/Navbar/NavbarView.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const NavbarView: FC<NavbarViewProps> = ({
4444
canViewAuditLog,
4545
proxyContextValue,
4646
}) => {
47-
const { subscribed, loading, subscribe, unsubscribe } =
47+
const { enabled, subscribed, loading, subscribe, unsubscribe } =
4848
usePushNotifications();
4949

5050
return (
@@ -59,13 +59,13 @@ export const NavbarView: FC<NavbarViewProps> = ({
5959

6060
<NavItems className="ml-4" />
6161

62-
{/* // TODO: styling required here.
63-
{subscribed ? (
64-
<button onClick={unsubscribe}>Unsubscribe</button>
65-
) : (
66-
<button onClick={subscribe}>Subscribe</button>
62+
{enabled && (
63+
subscribed ? (
64+
<button onClick={unsubscribe}>Unsubscribe</button>
65+
) : (
66+
<button onClick={subscribe}>Subscribe</button>
67+
)
6768
)}
68-
*/}
6969

7070
<div className=" hidden md:flex items-center gap-3 ml-auto">
7171
{proxyContextValue && (

0 commit comments

Comments
 (0)