Skip to content

feat(site): add webpush notification serviceworker #17123

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

Merged
merged 8 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
site changes from cj/push-notifications-2-rebase
  • Loading branch information
johnstcn committed Mar 27, 2025
commit 58cc953bb2e1b37c35289f22e43e09f96d639279
22 changes: 22 additions & 0 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2371,6 +2371,28 @@ class ApiMethods {
await this.axios.post<void>("/api/v2/notifications/test");
};

createNotificationPushSubscription = async (
userId: string,
req: TypesGen.WebpushSubscription,
) => {
await this.axios.post<void>(
`/api/v2/users/${userId}/webpush/subscription`,
req,
);
};

deleteNotificationPushSubscription = async (
userId: string,
req: TypesGen.DeleteWebpushSubscription,
) => {
await this.axios.delete<void>(
`/api/v2/users/${userId}/webpush/subscription`,
{
data: req,
},
);
};

requestOneTimePassword = async (
req: TypesGen.RequestOneTimePasscodeRequest,
) => {
Expand Down
5 changes: 5 additions & 0 deletions site/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ if (element === null) {
throw new Error("root element is null");
}

// The service worker handles push notifications.
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("/serviceWorker.js");
}

const root = createRoot(element);
root.render(<App />);
22 changes: 21 additions & 1 deletion site/src/modules/dashboard/Navbar/NavbarView.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { API } from "api/api";
import { experiments } from "api/queries/experiments";
import type * as TypesGen from "api/typesGenerated";
import { Button } from "components/Button/Button";
import { ExternalImage } from "components/ExternalImage/ExternalImage";
import { CoderIcon } from "components/Icons/CoderIcon";
import type { ProxyContextValue } from "contexts/ProxyContext";
import { useWebpushNotifications } from "contexts/useWebpushNotifications";
import { useEmbeddedMetadata } from "hooks/useEmbeddedMetadata";
import { NotificationsInbox } from "modules/notifications/NotificationsInbox/NotificationsInbox";
import type { FC } from "react";
import { useQuery } from "react-query";
import { NavLink, useLocation } from "react-router-dom";
import { cn } from "utils/cn";
import { DeploymentDropdown } from "./DeploymentDropdown";
Expand Down Expand Up @@ -43,6 +48,9 @@ export const NavbarView: FC<NavbarViewProps> = ({
canViewAuditLog,
proxyContextValue,
}) => {
const { subscribed, enabled, loading, subscribe, unsubscribe } =
useWebpushNotifications();

return (
<div className="border-0 border-b border-solid h-[72px] flex items-center leading-none px-6">
<NavLink to="/workspaces">
Expand All @@ -55,7 +63,7 @@ export const NavbarView: FC<NavbarViewProps> = ({

<NavItems className="ml-4" />

<div className="flex items-center gap-3 ml-auto">
<div className=" hidden md:flex items-center gap-3 ml-auto">
{proxyContextValue && (
<div className="hidden md:block">
<ProxyMenu proxyContextValue={proxyContextValue} />
Expand All @@ -71,6 +79,18 @@ export const NavbarView: FC<NavbarViewProps> = ({
/>
</div>

{enabled ? (
subscribed ? (
<Button variant="outline" disabled={loading} onClick={unsubscribe}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it just for experimental purposes? Dropping a button on NavBar may look inconsistent. If this is permanent, perhaps ask Bruno for his guidance.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, just for experimental. We'll need to better integrate this later.

Disable WebPush
</Button>
) : (
<Button variant="outline" disabled={loading} onClick={subscribe}>
Enable WebPush
</Button>
)
) : null}

<NotificationsInbox
fetchNotifications={API.getInboxNotifications}
markAllAsRead={API.markAllInboxNotificationsAsRead}
Expand Down
1 change: 1 addition & 0 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export const MockBuildInfo: TypesGen.BuildInfoResponse = {
workspace_proxy: false,
upgrade_message: "My custom upgrade message",
deployment_id: "510d407f-e521-4180-b559-eab4a6d802b8",
webpush_public_key: "fake-public-key",
telemetry: true,
};

Expand Down
18 changes: 18 additions & 0 deletions site/vite.config.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from "node:path";
import react from "@vitejs/plugin-react";
import { buildSync } from "esbuild";
import { visualizer } from "rollup-plugin-visualizer";
import { type PluginOption, defineConfig } from "vite";
import checker from "vite-plugin-checker";
Expand Down Expand Up @@ -28,6 +29,19 @@ export default defineConfig({
emptyOutDir: false,
// 'hidden' works like true except that the corresponding sourcemap comments in the bundled files are suppressed
sourcemap: "hidden",
rollupOptions: {
input: {
index: path.resolve(__dirname, "./index.html"),
serviceWorker: path.resolve(__dirname, "./src/serviceWorker.ts"),
},
output: {
entryFileNames: (chunkInfo) => {
return chunkInfo.name === "serviceWorker"
? "[name].js"
: "assets/[name]-[hash].js";
},
},
},
},
define: {
"process.env": {
Expand Down Expand Up @@ -89,6 +103,10 @@ export default defineConfig({
target: process.env.CODER_HOST || "http://localhost:3000",
secure: process.env.NODE_ENV === "production",
},
"/serviceWorker.js": {
target: process.env.CODER_HOST || "http://localhost:3000",
secure: process.env.NODE_ENV === "production",
},
},
allowedHosts: true,
},
Expand Down
Loading