Skip to content
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

Sprint 40 #2735

Merged
merged 19 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
cd759f1
CW-firebase-messaging-for-web Added push notification config for web
MeyerPV Aug 14, 2024
6b7537e
CW-firebase-messaging-for-web Update settings
MeyerPV Aug 15, 2024
92e2dfc
CW-firebase-messaging-for-web Updated config
MeyerPV Aug 15, 2024
a71b7ca
CW-firebase-messaging-for-web Fixed env issue for service worker
MeyerPV Aug 16, 2024
69c4a85
CW-firebase-messaging-for-web Fix service-worker naming
MeyerPV Aug 16, 2024
ba461e8
Adjust sidenav width to 15rem for better space utilization
matanfield Aug 30, 2024
8f85746
Standardize sidebar width to 15rem across all layouts
matanfield Aug 30, 2024
ec6d41d
changed width to 18.75rem
matanfield Sep 1, 2024
7f25bb8
Merge pull request #2730 from daostack/adjust-sidenav-width
MeyerPV Sep 3, 2024
8f5238c
Merge pull request #2723 from daostack/CW-firebase-messaging-for-web
MeyerPV Sep 3, 2024
c69907c
CW-hotfix-firebase-settings
MeyerPV Sep 4, 2024
bef5f56
CW-hotfix-firebase-firestore-settings
MeyerPV Sep 4, 2024
fb785d2
CW-hotfix-firebase-firestore-settings
MeyerPV Sep 5, 2024
7eab570
Merge pull request #2732 from daostack/CW-hotfix-firebase-firestore-s…
MeyerPV Sep 5, 2024
1a36c9a
CW-service-worker-performance
MeyerPV Sep 5, 2024
1177161
CW-service-worker-performance Added firefox permissions
MeyerPV Sep 10, 2024
956b543
CW-service-worker-performance Fixed settings for firestore
MeyerPV Sep 10, 2024
dc5b99a
CW-service-worker-performance Remove console.log
MeyerPV Sep 10, 2024
c85af4d
Merge pull request #2734 from daostack/CW-service-worker-performance
MeyerPV Sep 10, 2024
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
82 changes: 82 additions & 0 deletions public/firebase-messaging-sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* eslint-disable */
// firebase-messaging-sw.js
importScripts(
"https://www.gstatic.com/firebasejs/10.7.1/firebase-app-compat.js",
);
importScripts(
"https://www.gstatic.com/firebasejs/10.7.1/firebase-messaging-compat.js",
);

const ENV = {
LOCAL: "http://localhost:3000",
DEV: "https://web-dev.common.io",
STAGE: "https://web-staging.common.io",
PRODUCTION: "https://common.io",
};

const FIREBASE_CONFIG_ENV = {
DEV: {
apiKey: "AIzaSyDbTFuksgOkIVWDiFe_HG7-BE8X6Dwsg-0",
authDomain: "common-dev-34b09.firebaseapp.com",
databaseURL: "https://common-dev-34b09.firebaseio.com",
projectId: "common-dev-34b09",
storageBucket: "common-dev-34b09.appspot.com",
messagingSenderId: "870639147922",
appId: "1:870639147922:web:9ee954bb1dd52e25cb7f4b",
},
STAGE: {
apiKey: "AIzaSyBASCWJMV64mZJObeFEitLmdUC1HqmtjJk",
authDomain: "common-staging-1d426.firebaseapp.com",
databaseURL: "https://common-staging-1d426.firebaseio.com",
projectId: "common-staging-1d426",
storageBucket: "common-staging-1d426.appspot.com",
messagingSenderId: "701579202562",
appId: "1:701579202562:web:5729d8a875f98f6709571b",
},
PRODUCTION: {
apiKey: "AIzaSyAlYrKLd6KNKVkhmNEMKfb0cWHSWicCBOY",
authDomain: "common-production-67641.firebaseapp.com",
databaseURL: "https://common-production-67641.firebaseio.com",
projectId: "common-production-67641",
storageBucket: "common-production-67641.appspot.com",
messagingSenderId: "461029494046",
appId: "1:461029494046:web:4e2e4afbbeb7b487b48d0f",
},
};

let firebaseConfig = {};

switch (location.origin) {
case ENV.LOCAL:
case ENV.DEV: {
firebaseConfig = FIREBASE_CONFIG_ENV.DEV;
break;
}
case ENV.STAGE: {
firebaseConfig = FIREBASE_CONFIG_ENV.STAGE;
break;
}
case ENV.PRODUCTION: {
firebaseConfig = FIREBASE_CONFIG_ENV.PRODUCTION;
break;
}
default: {
firebaseConfig = FIREBASE_CONFIG_ENV.DEV;
break;
}
}

firebase.initializeApp(firebaseConfig);

const messaging = firebase.messaging();

messaging.onBackgroundMessage((payload) => {
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
data: payload.data,
icon: "/logo.png",
};

self.registration.showNotification(notificationTitle, notificationOptions);
});
Binary file added public/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions src/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export const local: Configuration = {
deadSeaCommonId: "958dca85-7bc1-4714-95bd-1fc6343f0654",
parentsForClimateCommonId: "958dca85-7bc1-4714-95bd-1fc6343f0654",
saadiaCommonId: "958dca85-7bc1-4714-95bd-1fc6343f0654",
vapidKey:
"BHVFyNetSC6oA2uFejnUFuDcSUYcas2R5lwW80z6gZc6zODp7rRdh2t8bht3LygJWjyI1toV165EYgdZqxCS_Y4",
};

const dev: Configuration = {
Expand All @@ -44,6 +46,8 @@ const dev: Configuration = {
deadSeaCommonId: "958dca85-7bc1-4714-95bd-1fc6343f0654",
parentsForClimateCommonId: "958dca85-7bc1-4714-95bd-1fc6343f0654",
saadiaCommonId: "958dca85-7bc1-4714-95bd-1fc6343f0654",
vapidKey:
"BHVFyNetSC6oA2uFejnUFuDcSUYcas2R5lwW80z6gZc6zODp7rRdh2t8bht3LygJWjyI1toV165EYgdZqxCS_Y4",
};

const stage: Configuration = {
Expand All @@ -62,6 +66,8 @@ const stage: Configuration = {
deadSeaCommonId: "a55a1e9b-104a-4866-9f4f-3e017bbae281",
parentsForClimateCommonId: "a55a1e9b-104a-4866-9f4f-3e017bbae281",
saadiaCommonId: "a55a1e9b-104a-4866-9f4f-3e017bbae281",
vapidKey:
"BBvr8z8QaPSJJfIRxmjBrq5Vs49BY95uZK_6QFyR7gKWgwrs5toDy-hvwWEtk-rbkVHBgOu9l2orK45u1n--9M0",
};

const production: Configuration = {
Expand All @@ -80,6 +86,8 @@ const production: Configuration = {
deadSeaCommonId: "6cfbfae6-2e5c-4b3b-ba70-e8fd871f48e2",
parentsForClimateCommonId: "04ac2ec2-5cb2-4ab9-ae3f-5f223f482768",
saadiaCommonId: "7c8c8996-b678-44df-9a57-e291431eb00f",
vapidKey:
"BKJ324iR-B5SoDG42bMrC_Q_poAv7BO-Z3AuMh5Grrg6TxO1QnN6mgzt2KyFFax0JSuuUhUKP-OrcTUPfboVqns",
};

const config: ConfigurationObject = {
Expand Down
2 changes: 2 additions & 0 deletions src/pages/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ThemeHandler,
UserNotificationsAmountHandler,
WebViewLoginHandler,
NotificationsHandler,
} from "./handlers";
import { Router } from "./router";

Expand All @@ -34,6 +35,7 @@ const App = () => {
<ThemeHandler />
<UserNotificationsAmountHandler />
<WebViewLoginHandler />
<NotificationsHandler />
<LoginContainer />
<Router />
</ReactRouter>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { FC, useEffect, useState } from "react";
import { useSelector } from "react-redux";
import { selectUser } from "@/pages/Auth/store/selectors";
import { NotificationService } from "@/services";

const NotificationsHandler: FC = () => {
const user = useSelector(selectUser());
const userId = user?.uid;
const [isRegistered, setIsRegistered] = useState(false);

function initServiceWorker() {
navigator.serviceWorker
.register("/firebase-messaging-sw.js")
.then((registration) => {
setIsRegistered(true);
return registration;
})
.catch((err) => {
console.log("ServiceWorker registration failed: ", err);
});
}

// Check if the service worker is already registered or register a new one
useEffect(() => {
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.getRegistration("/firebase-messaging-sw.js")
.then((existingRegistration) => {
if (existingRegistration) {
setIsRegistered(true);
} else {
initServiceWorker();
}

return;
})
.catch((err) => {
console.log("Error checking service worker registration: ", err);
});
}
}, []);

// Handle notification permissions and foreground message listener
useEffect(() => {
if (!userId || !isRegistered) {
return;
}

let unsubscribeOnMessage;
(async () => {
const hasPermissions = await NotificationService.requestPermissions();
if (!hasPermissions) {
console.log("Notification permissions denied");
return;
}

await NotificationService.saveFCMToken();
unsubscribeOnMessage = NotificationService.onForegroundMessage();
})();

return () => {
if (unsubscribeOnMessage) {
unsubscribeOnMessage();
}
};
}, [userId, isRegistered]);

return null;
};

export default NotificationsHandler;
1 change: 1 addition & 0 deletions src/pages/App/handlers/NotificationsHandler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as NotificationsHandler } from "./NotificationsHandler";
1 change: 1 addition & 0 deletions src/pages/App/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./TextDirectionHandler";
export * from "./UserNotificationsAmountHandler";
export * from "./WebViewLoginHandler";
export * from "./ThemeHandler";
export * from "./NotificationsHandler";
73 changes: 73 additions & 0 deletions src/services/Notification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import firebase from "@/shared/utils/firebase";
import firebaseConfig from "@/config";
import Api from "./Api";

enum NOTIFICATIONS_PERMISSIONS {
DEFAULT = "default",
DENIED = "denied",
GRANTED = "granted"
}


class NotificationService {
private endpoints: {
setFCMToken: string;
};

constructor() {
this.endpoints = {
setFCMToken: '/users/auth/google/set-fcm-token',
};
}

public requestPermissions = async (): Promise<boolean> => {
try {
if(Notification.permission === NOTIFICATIONS_PERMISSIONS.GRANTED) {
return true;
}
const permission = await Notification.requestPermission();
if (permission === NOTIFICATIONS_PERMISSIONS.GRANTED) {
return true;
} else {
return false;
}
} catch (err) {
return false;
}
}

public saveFCMToken = async (): Promise<void> => {
try {
const token = await firebase.messaging().getToken({ vapidKey: firebaseConfig.vapidKey });
if (token) {

await Api.post(
this.endpoints.setFCMToken,
{
token,
}
);
}
} catch (error) {
console.error("An error occurred while retrieving token. ", error);
}
}

public onForegroundMessage = () => {
const unsubscribe = firebase.messaging().onMessage((payload) => {

const { title, body } = payload.notification;
if (Notification.permission === 'granted') {
new Notification(title, {
body,
data: payload?.data,
icon: "/logo.png",
});
}
});

return unsubscribe;
}
}

export default new NotificationService();
1 change: 1 addition & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export {
} from "./DiscussionMessage";
export { default as NotionService } from "./Notion";
export { default as FeatureFlagService } from "./FeatureFlag";
export { default as NotificationService } from "./Notification";
7 changes: 5 additions & 2 deletions src/shared/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useDispatch, useSelector } from "react-redux";
import { Link, RouteProps, useHistory } from "react-router-dom";
import classNames from "classnames";
import { Routes } from "@/pages/MyAccount/components/Routes";
import { NotificationService } from "@/services";
import { Loader } from "@/shared/components";
import {
useAnyMandatoryRoles,
Expand Down Expand Up @@ -82,7 +83,8 @@ const Header = () => {
setShowAccountLinks(isMyAccountRoute);
}, [showMenu, isMyAccountRoute]);

const handleLogIn = useCallback(() => {
const handleLogIn = useCallback(async () => {
await NotificationService.requestPermissions();
dispatch(setLoginModalState({ isShowing: true }));
setShowMenu(false);
}, [dispatch]);
Expand Down Expand Up @@ -116,7 +118,8 @@ const Header = () => {
dispatch(logOut());
};

const handleLaunchApp = () => {
const handleLaunchApp = async () => {
await NotificationService.requestPermissions();
history.push(ROUTE_PATHS.INBOX);
};

Expand Down
1 change: 1 addition & 0 deletions src/shared/constants/featureFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export enum FeatureFlags {
AiBot = "AiBot",
AiBotPro = "AiBotPro",
UpdateRoles = "UpdateRoles",
HavingAnIssue = "HavingAnIssue"
}

export enum FeatureFlagVisibility {
Expand Down
1 change: 1 addition & 0 deletions src/shared/interfaces/Configuration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Configuration {
deadSeaCommonId: string;
parentsForClimateCommonId: string;
saadiaCommonId: string;
vapidKey: string;
}

export type ConfigurationObject = Record<Environment, Configuration>;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
--main-mw: 100%;
--main-pl: unset;
--sb-max-width: unset;
--sb-width: 21rem;
--sb-width: 18.75rem;
--sb-content-max-width: 100%;
--sb-content-width: 100%;
--sb-content-pb: 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
--main-mw: calc(120rem + var(--sb-h-indent, 0));
--main-pl: calc(var(--sb-h-indent, 0));
--sb-max-width: unset;
--sb-width: 21rem;
--sb-width: 18.75rem;
--sb-content-max-width: 100%;
--sb-content-width: 100%;
--sb-content-pb: 0;
Expand Down
2 changes: 1 addition & 1 deletion src/shared/layouts/SidenavLayout/SidenavLayout.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
.container {
--main-pl: unset;
--sb-max-width: unset;
--sb-width: 18.75rem;
--sb-width: 15rem;
--sb-content-max-width: 100%;
--sb-content-width: 100%;
--sb-content-pb: 0;
Expand Down
Loading
Loading