|
| 1 | +/** |
| 2 | + * Copyright (c) 2023 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the GNU Affero General Public License (AGPL). |
| 4 | + * See License-AGPL.txt in the project root for license information. |
| 5 | + */ |
| 6 | + |
| 7 | +import { useCallback, useEffect, useState } from "react"; |
| 8 | +import Alert, { AlertType } from "./components/Alert"; |
| 9 | +import dayjs from "dayjs"; |
| 10 | +import { useUserLoader } from "./hooks/use-user-loader"; |
| 11 | +import { getGitpodService } from "./service/service"; |
| 12 | +import deepMerge from "deepmerge"; |
| 13 | + |
| 14 | +const KEY_APP_DISMISSED_NOTIFICATIONS = "gitpod-app-notifications-dismissed"; |
| 15 | +const PRIVACY_POLICY_LAST_UPDATED = "2023-10-17"; |
| 16 | + |
| 17 | +interface Notification { |
| 18 | + id: string; |
| 19 | + type: AlertType; |
| 20 | + message: JSX.Element; |
| 21 | + preventDismiss?: boolean; |
| 22 | + onClose?: () => void; |
| 23 | +} |
| 24 | + |
| 25 | +const UPDATED_PRIVACY_POLICY: Notification = { |
| 26 | + id: "privacy-policy-update", |
| 27 | + type: "info", |
| 28 | + preventDismiss: true, |
| 29 | + onClose: async () => { |
| 30 | + const userUpdates = { additionalData: { profile: { acceptedPrivacyPolicyDate: dayjs().toISOString() } } }; |
| 31 | + const previousUser = await getGitpodService().server.getLoggedInUser(); |
| 32 | + await getGitpodService().server.updateLoggedInUser(deepMerge(previousUser, userUpdates)); |
| 33 | + }, |
| 34 | + message: ( |
| 35 | + <span className="text-md"> |
| 36 | + We've updated our Privacy Policy. You can review it{" "} |
| 37 | + <a className="gp-link" href="https://www.gitpod.io/privacy" target="_blank" rel="noreferrer"> |
| 38 | + here |
| 39 | + </a> |
| 40 | + . |
| 41 | + </span> |
| 42 | + ), |
| 43 | +}; |
| 44 | + |
| 45 | +export function AppNotifications() { |
| 46 | + const [topNotification, setTopNotification] = useState<Notification | undefined>(undefined); |
| 47 | + const { user, loading } = useUserLoader(); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + const notifications = []; |
| 51 | + if (!loading && user?.additionalData?.profile) { |
| 52 | + if ( |
| 53 | + !user.additionalData.profile.acceptedPrivacyPolicyDate || |
| 54 | + new Date(PRIVACY_POLICY_LAST_UPDATED) > new Date(user.additionalData.profile?.acceptedPrivacyPolicyDate) |
| 55 | + ) { |
| 56 | + notifications.push(UPDATED_PRIVACY_POLICY); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + const dismissedNotifications = getDismissedNotifications(); |
| 61 | + const topNotification = notifications.find((n) => !dismissedNotifications.includes(n.id)); |
| 62 | + setTopNotification(topNotification); |
| 63 | + }, [loading, setTopNotification, user]); |
| 64 | + |
| 65 | + const dismissNotification = useCallback(() => { |
| 66 | + if (!topNotification) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + const dismissedNotifications = getDismissedNotifications(); |
| 71 | + dismissedNotifications.push(topNotification.id); |
| 72 | + setDismissedNotifications(dismissedNotifications); |
| 73 | + setTopNotification(undefined); |
| 74 | + }, [topNotification, setTopNotification]); |
| 75 | + |
| 76 | + if (!topNotification) { |
| 77 | + return <></>; |
| 78 | + } |
| 79 | + |
| 80 | + return ( |
| 81 | + <div className="app-container pt-2"> |
| 82 | + <Alert |
| 83 | + type={topNotification.type} |
| 84 | + closable={true} |
| 85 | + onClose={() => { |
| 86 | + if (!topNotification.preventDismiss) { |
| 87 | + dismissNotification(); |
| 88 | + } else { |
| 89 | + if (topNotification.onClose) { |
| 90 | + topNotification.onClose(); |
| 91 | + } |
| 92 | + } |
| 93 | + }} |
| 94 | + showIcon={true} |
| 95 | + className="flex rounded mb-2 w-full" |
| 96 | + > |
| 97 | + <span>{topNotification.message}</span> |
| 98 | + </Alert> |
| 99 | + </div> |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +function getDismissedNotifications(): string[] { |
| 104 | + try { |
| 105 | + const str = window.localStorage.getItem(KEY_APP_DISMISSED_NOTIFICATIONS); |
| 106 | + const parsed = JSON.parse(str || "[]"); |
| 107 | + if (!Array.isArray(parsed)) { |
| 108 | + window.localStorage.removeItem(KEY_APP_DISMISSED_NOTIFICATIONS); |
| 109 | + return []; |
| 110 | + } |
| 111 | + return parsed; |
| 112 | + } catch (err) { |
| 113 | + console.debug("Failed to parse dismissed notifications", err); |
| 114 | + window.localStorage.removeItem(KEY_APP_DISMISSED_NOTIFICATIONS); |
| 115 | + return []; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +function setDismissedNotifications(ids: string[]) { |
| 120 | + try { |
| 121 | + window.localStorage.setItem(KEY_APP_DISMISSED_NOTIFICATIONS, JSON.stringify(ids)); |
| 122 | + } catch (err) { |
| 123 | + console.debug("Failed to set dismissed notifications", err); |
| 124 | + window.localStorage.removeItem(KEY_APP_DISMISSED_NOTIFICATIONS); |
| 125 | + } |
| 126 | +} |
0 commit comments