Skip to content

Commit 81b8c6e

Browse files
committed
site changes from cj/push-notifications-2-rebase
1 parent 10ac9fb commit 81b8c6e

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

site/src/api/api.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,6 +2371,28 @@ class ApiMethods {
23712371
await this.axios.post<void>("/api/v2/notifications/test");
23722372
};
23732373

2374+
createNotificationPushSubscription = async (
2375+
userId: string,
2376+
req: TypesGen.WebpushSubscription,
2377+
) => {
2378+
await this.axios.post<void>(
2379+
`/api/v2/users/${userId}/webpush/subscription`,
2380+
req,
2381+
);
2382+
};
2383+
2384+
deleteNotificationPushSubscription = async (
2385+
userId: string,
2386+
req: TypesGen.DeleteWebpushSubscription,
2387+
) => {
2388+
await this.axios.delete<void>(
2389+
`/api/v2/users/${userId}/webpush/subscription`,
2390+
{
2391+
data: req,
2392+
},
2393+
);
2394+
};
2395+
23742396
requestOneTimePassword = async (
23752397
req: TypesGen.RequestOneTimePasscodeRequest,
23762398
) => {

site/src/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ if (element === null) {
1414
throw new Error("root element is null");
1515
}
1616

17+
// The service worker handles push notifications.
18+
if ("serviceWorker" in navigator) {
19+
navigator.serviceWorker.register("/serviceWorker.js");
20+
}
21+
1722
const root = createRoot(element);
1823
root.render(<App />);

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { API } from "api/api";
2+
import { experiments } from "api/queries/experiments";
23
import type * as TypesGen from "api/typesGenerated";
4+
import { Button } from "components/Button/Button";
35
import { ExternalImage } from "components/ExternalImage/ExternalImage";
46
import { CoderIcon } from "components/Icons/CoderIcon";
57
import type { ProxyContextValue } from "contexts/ProxyContext";
8+
import { useWebpushNotifications } from "contexts/useWebpushNotifications";
9+
import { useEmbeddedMetadata } from "hooks/useEmbeddedMetadata";
610
import { NotificationsInbox } from "modules/notifications/NotificationsInbox/NotificationsInbox";
711
import type { FC } from "react";
12+
import { useQuery } from "react-query";
813
import { NavLink, useLocation } from "react-router-dom";
914
import { cn } from "utils/cn";
1015
import { DeploymentDropdown } from "./DeploymentDropdown";
@@ -43,6 +48,9 @@ export const NavbarView: FC<NavbarViewProps> = ({
4348
canViewAuditLog,
4449
proxyContextValue,
4550
}) => {
51+
const { subscribed, enabled, loading, subscribe, unsubscribe } =
52+
useWebpushNotifications();
53+
4654
return (
4755
<div className="border-0 border-b border-solid h-[72px] flex items-center leading-none px-6">
4856
<NavLink to="/workspaces">
@@ -55,7 +63,7 @@ export const NavbarView: FC<NavbarViewProps> = ({
5563

5664
<NavItems className="ml-4" />
5765

58-
<div className="flex items-center gap-3 ml-auto">
66+
<div className=" hidden md:flex items-center gap-3 ml-auto">
5967
{proxyContextValue && (
6068
<div className="hidden md:block">
6169
<ProxyMenu proxyContextValue={proxyContextValue} />
@@ -71,6 +79,18 @@ export const NavbarView: FC<NavbarViewProps> = ({
7179
/>
7280
</div>
7381

82+
{enabled ? (
83+
subscribed ? (
84+
<Button variant="outline" disabled={loading} onClick={unsubscribe}>
85+
Disable WebPush
86+
</Button>
87+
) : (
88+
<Button variant="outline" disabled={loading} onClick={subscribe}>
89+
Enable WebPush
90+
</Button>
91+
)
92+
) : null}
93+
7494
<NotificationsInbox
7595
fetchNotifications={API.getInboxNotifications}
7696
markAllAsRead={API.markAllInboxNotificationsAsRead}

site/src/testHelpers/entities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ export const MockBuildInfo: TypesGen.BuildInfoResponse = {
227227
workspace_proxy: false,
228228
upgrade_message: "My custom upgrade message",
229229
deployment_id: "510d407f-e521-4180-b559-eab4a6d802b8",
230+
webpush_public_key: "fake-public-key",
230231
telemetry: true,
231232
};
232233

site/vite.config.mts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as path from "node:path";
22
import react from "@vitejs/plugin-react";
3+
import { buildSync } from "esbuild";
34
import { visualizer } from "rollup-plugin-visualizer";
45
import { type PluginOption, defineConfig } from "vite";
56
import checker from "vite-plugin-checker";
@@ -28,6 +29,19 @@ export default defineConfig({
2829
emptyOutDir: false,
2930
// 'hidden' works like true except that the corresponding sourcemap comments in the bundled files are suppressed
3031
sourcemap: "hidden",
32+
rollupOptions: {
33+
input: {
34+
index: path.resolve(__dirname, "./index.html"),
35+
serviceWorker: path.resolve(__dirname, "./src/serviceWorker.ts"),
36+
},
37+
output: {
38+
entryFileNames: (chunkInfo) => {
39+
return chunkInfo.name === "serviceWorker"
40+
? "[name].js"
41+
: "assets/[name]-[hash].js";
42+
},
43+
},
44+
},
3145
},
3246
define: {
3347
"process.env": {
@@ -89,6 +103,10 @@ export default defineConfig({
89103
target: process.env.CODER_HOST || "http://localhost:3000",
90104
secure: process.env.NODE_ENV === "production",
91105
},
106+
"/serviceWorker.js": {
107+
target: process.env.CODER_HOST || "http://localhost:3000",
108+
secure: process.env.NODE_ENV === "production",
109+
},
92110
},
93111
allowedHosts: true,
94112
},

0 commit comments

Comments
 (0)