when a notification is clicked, jump to corresponding event
if event_id is not available, jump to inbox i haven't check app notification, this is for push notification for now
This commit is contained in:
1 parent
bb14a049c2
commit
8b55ffa045
6 files changed
+106
-22
No files matched your search
@@ -241,6 +241,10 @@ const useEventTimelineLoader = (
|
||||
) => {
|
||||
const loadEventTimeline = useCallback(
|
||||
async (eventId: string) => {
|
||||
if (!room.getUnfilteredTimelineSet().getTimelineForEvent(eventId)) {
|
||||
await mx.roomInitialSync(room.roomId, PAGINATION_LIMIT);
|
||||
await mx.getLatestTimeline(room.getUnfilteredTimelineSet());
|
||||
}
|
||||
const [err, replyEvtTimeline] = await to(
|
||||
mx.getEventTimeline(room.getUnfilteredTimelineSet(), eventId)
|
||||
);
|
||||
@@ -782,12 +786,12 @@ export function RoomTimeline({
|
||||
)
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (eventId) {
|
||||
setTimeline(getEmptyTimeline());
|
||||
loadEventTimeline(eventId);
|
||||
}
|
||||
}, [eventId, loadEventTimeline]);
|
||||
useEffect(
|
||||
() => {
|
||||
if (eventId) {
|
||||
handleOpenEvent(eventId);
|
||||
}
|
||||
}, [eventId]);
|
||||
|
||||
// Scroll to bottom on initial timeline load
|
||||
useLayoutEffect(() => {
|
||||
|
||||
@@ -28,8 +28,9 @@ import {
|
||||
_ROOM_PATH,
|
||||
_SEARCH_PATH,
|
||||
_SERVER_PATH,
|
||||
CREATE_PATH,
|
||||
TO_ROOM_EVENT_PATH,
|
||||
} from './paths';
|
||||
import { isAuthenticated } from '../../client/state/auth';
|
||||
import {
|
||||
getAppPathFromHref,
|
||||
getExploreFeaturedPath,
|
||||
@@ -61,6 +62,14 @@ import { AutoRestoreBackupOnVerification } from '../components/BackupRestore';
|
||||
import { RoomSettingsRenderer } from '../features/room-settings';
|
||||
import { ClientRoomsNotificationPreferences } from './client/ClientRoomsNotificationPreferences';
|
||||
import { SpaceSettingsRenderer } from '../features/space-settings';
|
||||
import { UserRoomProfileRenderer } from '../components/UserRoomProfileRenderer';
|
||||
import { CreateRoomModalRenderer } from '../features/create-room';
|
||||
import { HomeCreateRoom } from './client/home/CreateRoom';
|
||||
import { Create } from './client/create';
|
||||
import { CreateSpaceModalRenderer } from '../features/create-space';
|
||||
import { SearchModalRenderer } from '../features/search';
|
||||
import { getFallbackSession } from '../state/sessions';
|
||||
import { ToRoomEvent } from './client/ToRoomEvent';
|
||||
|
||||
export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize) => {
|
||||
const { hashRouter } = clientConfig;
|
||||
@@ -71,7 +80,7 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize)
|
||||
<Route
|
||||
index
|
||||
loader={() => {
|
||||
if (isAuthenticated()) return redirect(getHomePath());
|
||||
if (getFallbackSession()) return redirect(getHomePath());
|
||||
const afterLoginPath = getAppPathFromHref(getOriginBaseUrl(), window.location.href);
|
||||
if (afterLoginPath) setAfterLoginRedirectPath(afterLoginPath);
|
||||
return redirect(getLoginPath());
|
||||
@@ -79,7 +88,7 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize)
|
||||
/>
|
||||
<Route
|
||||
loader={() => {
|
||||
if (isAuthenticated()) {
|
||||
if (getFallbackSession()) {
|
||||
return redirect(getHomePath());
|
||||
}
|
||||
|
||||
@@ -99,7 +108,7 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize)
|
||||
|
||||
<Route
|
||||
loader={() => {
|
||||
if (!isAuthenticated()) {
|
||||
if (!getFallbackSession()) {
|
||||
const afterLoginPath = getAppPathFromHref(
|
||||
getOriginBaseUrl(hashRouter),
|
||||
window.location.href
|
||||
@@ -125,6 +134,10 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize)
|
||||
>
|
||||
<Outlet />
|
||||
</ClientLayout>
|
||||
<SearchModalRenderer />
|
||||
<UserRoomProfileRenderer />
|
||||
<CreateRoomModalRenderer />
|
||||
<CreateSpaceModalRenderer />
|
||||
<RoomSettingsRenderer />
|
||||
<SpaceSettingsRenderer />
|
||||
<ReceiveSelfDeviceVerification />
|
||||
@@ -152,7 +165,7 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize)
|
||||
}
|
||||
>
|
||||
{mobile ? null : <Route index element={<WelcomePage />} />}
|
||||
<Route path={_CREATE_PATH} element={<p>create</p>} />
|
||||
<Route path={_CREATE_PATH} element={<HomeCreateRoom />} />
|
||||
<Route path={_JOIN_PATH} element={<p>join</p>} />
|
||||
<Route path={_SEARCH_PATH} element={<HomeSearch />} />
|
||||
<Route
|
||||
@@ -253,6 +266,7 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize)
|
||||
<Route path={_FEATURED_PATH} element={<FeaturedRooms />} />
|
||||
<Route path={_SERVER_PATH} element={<PublicRooms />} />
|
||||
</Route>
|
||||
<Route path={CREATE_PATH} element={<Create />} />
|
||||
<Route
|
||||
path={INBOX_PATH}
|
||||
element={
|
||||
@@ -277,6 +291,7 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize)
|
||||
<Route path={_NOTIFICATIONS_PATH} element={<Notifications />} />
|
||||
<Route path={_INVITES_PATH} element={<Invites />} />
|
||||
</Route>
|
||||
<Route index path={TO_ROOM_EVENT_PATH} element={<ToRoomEvent />} />
|
||||
</Route>
|
||||
<Route path="/*" element={<p>Page not found</p>} />
|
||||
</Route>
|
||||
|
||||
@@ -26,6 +26,7 @@ import { getMxIdLocalPart, mxcUrlToHttp } from '../../utils/matrix';
|
||||
import { useSelectedRoom } from '../../hooks/router/useSelectedRoom';
|
||||
import { useInboxNotificationsSelected } from '../../hooks/router/useInbox';
|
||||
import { useMediaAuthentication } from '../../hooks/useMediaAuthentication';
|
||||
import { useRoomNavigate } from '../../hooks/useRoomNavigate';
|
||||
|
||||
function SystemEmojiFeature() {
|
||||
const [twitterEmoji] = useSetting(settingsAtom, 'twitterEmoji');
|
||||
@@ -263,6 +264,41 @@ function MessageNotifications() {
|
||||
);
|
||||
}
|
||||
|
||||
function HandleNotificationClick() {
|
||||
const { navigateRoom } = useRoomNavigate();
|
||||
|
||||
const handleNotificationClickEvent = (event: any) => {
|
||||
if (
|
||||
!event.data ||
|
||||
!event.source
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const eventData = event.data;
|
||||
if (
|
||||
!(eventData.type == "notificationToRoomEvent") ||
|
||||
!eventData.room_id ||
|
||||
!eventData.event_id
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("main thread received notification click event:");
|
||||
console.log(event);
|
||||
navigateRoom(eventData.room_id, eventData.event_id);
|
||||
};
|
||||
|
||||
useEffect( () => {
|
||||
navigator.serviceWorker.addEventListener("message", handleNotificationClickEvent);
|
||||
console.log("notification click event is listening on main thread");
|
||||
return () => {
|
||||
navigator.serviceWorker.removeEventListener("message", handleNotificationClickEvent);
|
||||
}
|
||||
}, [navigateRoom]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
type ClientNonUIFeaturesProps = {
|
||||
children: ReactNode;
|
||||
};
|
||||
@@ -275,6 +311,7 @@ export function ClientNonUIFeatures({ children }: ClientNonUIFeaturesProps) {
|
||||
<FaviconUpdater />
|
||||
<InviteNotifications />
|
||||
<MessageNotifications />
|
||||
<HandleNotificationClick />
|
||||
{children}
|
||||
</>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import React, { useEffect } from "react";
|
||||
import { useParams } from "react-router-dom";
|
||||
import { useRoomNavigate } from "../../hooks/useRoomNavigate";
|
||||
|
||||
export function ToRoomEvent() {
|
||||
const { room_id, event_id } = useParams();
|
||||
const { navigateRoom } = useRoomNavigate();
|
||||
|
||||
useEffect(() => {
|
||||
if (!room_id) { return; }
|
||||
|
||||
navigateRoom(room_id, event_id);
|
||||
}, [room_id, event_id, navigateRoom]);
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -83,6 +83,9 @@ export type InboxNotificationsPathSearchParams = {
|
||||
export const INBOX_NOTIFICATIONS_PATH = `/inbox/${_NOTIFICATIONS_PATH}`;
|
||||
export const INBOX_INVITES_PATH = `/inbox/${_INVITES_PATH}`;
|
||||
|
||||
export const _TO_PATH = '/to';
|
||||
export const TO_ROOM_EVENT_PATH = `${_TO_PATH}/:room_id/:event_id?`;
|
||||
|
||||
export const SPACE_SETTINGS_PATH = '/space-settings/';
|
||||
|
||||
export const ROOM_SETTINGS_PATH = '/room-settings/';
|
||||
@@ -135,7 +135,7 @@ const handlePushNotificationEventData = async (eventData: PushMessageData) => {
|
||||
const pushData = eventData.json();
|
||||
console.log(pushData); // TODO: delete this
|
||||
|
||||
if (typeof pushData.unread === 'number') {
|
||||
if (typeof pushData?.unread === 'number') {
|
||||
try {
|
||||
self.navigator.setAppBadge(pushData.unread);
|
||||
} catch (e) {
|
||||
@@ -146,15 +146,15 @@ const handlePushNotificationEventData = async (eventData: PushMessageData) => {
|
||||
}
|
||||
|
||||
let title = undefined;
|
||||
if (pushData.sender_display_name && pushData.room_name) {
|
||||
if (pushData?.sender_display_name && pushData?.room_name) {
|
||||
title = `${pushData.sender_display_name} in ${pushData.room_name}`;
|
||||
}
|
||||
title = title ?? "New Notification";
|
||||
|
||||
let body = "You have a new message";
|
||||
if (pushData.content.ciphertext) {
|
||||
if (pushData?.content?.ciphertext) {
|
||||
body = `Encrypted message`;
|
||||
} else if (pushData.content.body) {
|
||||
} else if (pushData?.content?.body) {
|
||||
body = pushData.content.body;
|
||||
} else {
|
||||
return;
|
||||
@@ -167,6 +167,8 @@ const handlePushNotificationEventData = async (eventData: PushMessageData) => {
|
||||
data: {
|
||||
url: self.registration.scope,
|
||||
timestamp: Date.now(),
|
||||
room_id: pushData.room_id,
|
||||
event_id: pushData.event_id,
|
||||
...pushData.data,
|
||||
},
|
||||
tag: "Cinny",
|
||||
@@ -187,17 +189,24 @@ self.addEventListener('push', (event: PushEvent) => event.waitUntil(onPushNotifi
|
||||
self.addEventListener('notificationclick', (event: NotificationEvent) => {
|
||||
event.notification.close();
|
||||
|
||||
/**
|
||||
* We should likely add a postMessage back to navigate to the room the event is from
|
||||
*/
|
||||
// const targetUrl = event.notification.data?.url || self.registration.scope;
|
||||
const targetUrl = `${self.registration.scope}/inbox/notifications/`;
|
||||
const messageData = event.notification.data;
|
||||
const scope = self.registration.scope;
|
||||
const targetUrl = (messageData?.room_id && messageData?.event_id)
|
||||
? `${scope}to/${messageData.room_id}/${messageData.event_id}`
|
||||
: `${scope}inbox/notifications/`;
|
||||
const postMessageToClient = (client: WindowClient) => {
|
||||
client.postMessage({
|
||||
type: "notificationToRoomEvent",
|
||||
room_id: event.notification.data?.room_id,
|
||||
event_id: event.notification.data?.event_id,
|
||||
});
|
||||
};
|
||||
|
||||
event.waitUntil(
|
||||
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => {
|
||||
for (const client of clientList) {
|
||||
if (client.url === targetUrl && 'focus' in client) {
|
||||
return (client as WindowClient).focus();
|
||||
if ('focus' in client) {
|
||||
return (client as WindowClient).focus().then(postMessageToClient);
|
||||
}
|
||||
}
|
||||
if (self.clients.openWindow) {
|
||||
|
||||
Reference in new issue
Block a user