aboutsummaryrefslogtreecommitdiffstats
path: root/site/src/components/NotificationButton.tsx
blob: fa31cded6e59aab0f6f2d7b848da45b7fb05398c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import { useState, useEffect } from "react";
import { messaging, initializeForegroundNotifications } from "../firebase.ts";
import { getToken, deleteToken } from "firebase/messaging";
import { getGameTitle } from "../utils.ts";

const VAPID_KEY =
  "BK7tpLF5Loy8Ew8bKxhTi-vOEJdxJSnu-jPyagWecLdD_SrEAt_OQS7nu0Xu3hR7AQpn0cOmgcdeeQd5zq5-Gyo";

interface NotificationButtonProps {
  className?: string;
  isMoe?: boolean;
  gameId?: string;
}

export default function NotificationButton({ className = "", isMoe = false, gameId }: NotificationButtonProps) {
  const [permission, setPermission] = useState<NotificationPermission>("default");
  const [isRegistered, setIsRegistered] = useState(false);
  const [isSubscribed, setIsSubscribed] = useState(false);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    // Check initial permission status
    setPermission(Notification.permission);

    // Check if service worker is registered
    const checkRegistration = async () => {
      if ('serviceWorker' in navigator) {
        const registration = await navigator.serviceWorker.getRegistration('/firebase-messaging-sw.js');
        setIsRegistered(!!registration);

        // Initialize foreground notifications if already registered
        if (registration && Notification.permission === "granted") {
          initializeForegroundNotifications();
        }
      }
    };

    // Check if subscribed to topic (for gameId mode)
    const checkTopicSubscription = () => {
      if (gameId) {
        const subscribedTopics = JSON.parse(localStorage.getItem('subscribed_topics') || '[]');
        setIsSubscribed(subscribedTopics.includes(gameId));
      }
    };

    checkRegistration();
    checkTopicSubscription();
  }, [gameId]);

  const handleSubscribeToTopic = async () => {
    setLoading(true);
    setError(null);

    try {
      // Ensure notifications are enabled first
      const permissionResult = await Notification.requestPermission();
      setPermission(permissionResult);

      if (permissionResult === "granted") {
        let registration = await navigator.serviceWorker.getRegistration('/firebase-messaging-sw.js');
        if (!registration) {
          registration = await navigator.serviceWorker.register('/firebase-messaging-sw.js');
          console.log("Service Worker registered:", registration);
        }

        let token = localStorage.getItem('fcm_token');
        if (!token) {
          token = await getToken(messaging, {
            vapidKey: VAPID_KEY,
            serviceWorkerRegistration: registration,
          });
          localStorage.setItem('fcm_token', token);
        }

        // TODO: Subscribe to topic via backend API
        console.log(`Subscribing to topic: ${gameId} with token: ${token}`);
        await fetch(import.meta.env.VITE_MIDDLEWARE_BASE_URL+`/api/notifications/set?topic=${gameId}&token=${token}&action=subscribe`, {
          method: "GET",
          headers: { "Content-Type": "application/json" },
        });

        // Update local storage to track subscribed topics
        const subscribedTopics = JSON.parse(localStorage.getItem('subscribed_topics') || '[]');
        if (!subscribedTopics.includes(gameId)) {
          subscribedTopics.push(gameId);
          localStorage.setItem('subscribed_topics', JSON.stringify(subscribedTopics));
        }

        setIsSubscribed(true);
        setIsRegistered(true);
      } else {
        setError("Notification permission was denied");
      }
    } catch (err) {
      console.error("Error subscribing to topic:", err);
      setError("Failed to subscribe to game notifications. Please try again.");
    } finally {
      setLoading(false);
    }
  };

  const handleUnsubscribeFromTopic = async () => {
    setLoading(true);
    setError(null);

    try {
      const token = localStorage.getItem('fcm_token');

      // TODO: Unsubscribe from topic via backend API
      console.log(`Unsubscribing from topic: ${gameId} with token: ${token}`);
      await fetch(import.meta.env.VITE_MIDDLEWARE_BASE_URL+`/api/notifications/set?topic=${gameId}&token=${token}&action=unsubscribe`, {
        method: "GET",
        headers: { "Content-Type": "application/json" },
      });


      // Update local storage to remove topic
      const subscribedTopics = JSON.parse(localStorage.getItem('subscribed_topics') || '[]');
      const updatedTopics = subscribedTopics.filter((topic: string) => topic !== gameId);
      localStorage.setItem('subscribed_topics', JSON.stringify(updatedTopics));

      setIsSubscribed(false);
    } catch (err) {
      console.error("Error unsubscribing from topic:", err);
      setError("Failed to unsubscribe from game notifications. Please try again.");
    } finally {
      setLoading(false);
    }
  };

  const handleEnableNotifications = async () => {
    setLoading(true);
    setError(null);

    try {
      const permissionResult = await Notification.requestPermission();
      setPermission(permissionResult);

      if (permissionResult === "granted") {
        let registration = await navigator.serviceWorker.getRegistration('/firebase-messaging-sw.js');
        if (!registration) {
          registration = await navigator.serviceWorker.register('/firebase-messaging-sw.js');
          console.log("Service Worker registered:", registration);
        } else {
          console.log("Reusing existing Service Worker:", registration);
        }

        const token = await getToken(messaging, {
          vapidKey: VAPID_KEY,
          serviceWorkerRegistration: registration,
        });
        console.log("FCM Token:", token);

        localStorage.setItem('fcm_token', token);

        // Foreground notification listener
        initializeForegroundNotifications();

        setIsRegistered(true);
      } else {
        setError("Notification permission was denied");
      }
    } catch (err) {
      console.error("Error enabling notifications:", err);
      setError("Failed to enable notifications. Please try again.");
    } finally {
      setLoading(false);
    }
  };

  const handleDisableNotifications = async () => {
    setLoading(true);
    setError(null);

    try {
      await deleteToken(messaging);
      console.log("FCM token deleted");
      localStorage.removeItem('fcm_token');

      // Clear all subscribed topics
      localStorage.removeItem('subscribed_topics');

      if ('serviceWorker' in navigator) {
        const registration = await navigator.serviceWorker.getRegistration('/firebase-messaging-sw.js');
        if (registration) {
          await registration.unregister();
          console.log("Service Worker unregistered");
        }
      }

      setIsRegistered(false);
      setIsSubscribed(false);
    } catch (err) {
      console.error("Error disabling notifications:", err);
      setError("Failed to disable notifications. Please try again.");
    } finally {
      setLoading(false);
    }
  };

  // Determine button content
  const getButtonContent = () => {
    if (loading) {
      return (
        <>
          <svg className="animate-spin h-5 w-5 mr-2" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
            <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
            <path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
          </svg>
          {gameId
            ? (isSubscribed ? "Unsubscribing..." : "Subscribing...")
            : (isRegistered ? "Disabling..." : "Enabling...")
          }
        </>
      );
    }

    if (permission === "denied") {
      return (
        <>
          <svg className="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M18.364 18.364A9 9 0 005.636 5.636m12.728 12.728A9 9 0 015.636 5.636m12.728 12.728L5.636 5.636" />
          </svg>
          Notifications Blocked
        </>
      );
    }

    // For topic subscription mode
    if (gameId) {
      if (isSubscribed && permission === "granted") {
        return (
          <>
            <svg className="w-5 h-5 mr-2" fill="currentColor" viewBox="0 0 24 24">
              <path d="M5 13l4 4L19 7" />
            </svg>
            Unsubscribe from {getGameTitle(gameId)}
          </>
        );
      }
      return (
        <>
          <svg className="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9" />
          </svg>
          Subscribe to {getGameTitle(gameId)} Updates
        </>
      );
    }

    if (isRegistered && permission === "granted") {
      return (
        <>
          <svg className="w-5 h-5 mr-2" fill="currentColor" viewBox="0 0 24 24">
            <path d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9" />
          </svg>
          Disable Notifications
        </>
      );
    }

    return (
      <>
        <svg className="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
          <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9" />
        </svg>
        Enable Notifications
      </>
    );
  };

  const handleClick = () => {
    if (permission === "denied") {
      alert("Notifications are blocked. Please enable them in your browser settings.");
      return;
    }

    if (gameId) {
      if (permission !== "granted") {
        alert("Please enable general notifications first before subscribing to game updates.");
        return;
      }

      if (isSubscribed) {
        handleUnsubscribeFromTopic();
      } else {
        handleSubscribeToTopic();
      }
    } else {
      if (isRegistered && permission === "granted") {
        handleDisableNotifications();
      } else {
        handleEnableNotifications();
      }
    }
  };

  const getButtonStyles = () => {
    if (loading || permission === "denied" || (gameId && permission !== "granted")) {
      return isMoe
        ? `bg-pink-300 cursor-not-allowed opacity-60`
        : `bg-gray-600 cursor-not-allowed opacity-60`;
    }

    const isActive = gameId ? isSubscribed : isRegistered;

    if (isMoe) {
      return isActive
        ? `bg-pink-600 text-white hover:bg-pink-700`
        : `bg-pink-500 text-white hover:bg-pink-600`;
    } else {
      return isActive
        ? `bg-purple-700 text-white hover:bg-purple-800`
        : `bg-purple-600 text-white hover:bg-purple-700`;
    }
  };

  return (
    <div className="flex flex-col items-center gap-2">
      <button
        onClick={handleClick}
        className={`flex items-center justify-center px-4 py-2 rounded-lg font-semibold transition-colors ${getButtonStyles()} ${className}`}
      >
        {getButtonContent()}
      </button>
      {error && (
        <p className={`text-sm ${isMoe ? "text-pink-600" : "text-red-500"}`}>
          {error}
        </p>
      )}
      {permission === "denied" && (
        <p className={`text-xs ${isMoe ? "text-pink-600" : "text-gray-400"}`}>
          To enable notifications, update your browser settings
        </p>
      )}
      {gameId && permission !== "granted" && permission !== "denied" && (
        <p className={`text-xs ${isMoe ? "text-pink-600" : "text-gray-400"}`}>
          Enable general notifications first to subscribe to game updates
        </p>
      )}
    </div>
  );
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage