MCPcopy Index your code
hub / github.com/coder/coder / subscribe

Function subscribe

site/src/contexts/useWebpushNotifications.ts:50–91  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

48 }, []);
49
50 const subscribe = async (): Promise<void> => {
51 try {
52 setLoading(true);
53
54 // Explicitly request notification permission before subscribing
55 // to the push manager. Without this, pushManager.subscribe()
56 // throws a generic "Registration failed - permission denied"
57 // DOMException when the permission state is "denied", which
58 // gives no opportunity to show a clear message to the user.
59 const permission = await Notification.requestPermission();
60 if (permission !== "granted") {
61 throw new Error(
62 "Notifications are blocked by your browser. Please allow notifications for this site in your browser settings.",
63 );
64 }
65
66 const registration = await navigator.serviceWorker.ready;
67 const vapidPublicKey = buildInfoQuery.data?.webpush_public_key;
68
69 const subscription = await registration.pushManager.subscribe({
70 userVisibleOnly: true,
71 applicationServerKey: vapidPublicKey,
72 });
73 const json = subscription.toJSON();
74 if (!json.keys || !json.endpoint) {
75 throw new Error("No keys or endpoint found");
76 }
77
78 await API.createWebPushSubscription("me", {
79 endpoint: json.endpoint,
80 auth_key: json.keys.auth,
81 p256dh_key: json.keys.p256dh,
82 });
83
84 setSubscribed(true);
85 } catch (error) {
86 console.error("Subscription failed:", error);
87 throw error;
88 } finally {
89 setLoading(false);
90 }
91 };
92
93 const unsubscribe = async (): Promise<void> => {
94 try {

Callers

nothing calls this directly

Calls 1

subscribeMethod · 0.65

Tested by

no test coverage detected