Skip to content

Commit

Permalink
feat: notify增加点击输入框跳转的功能,并增加api调用页面跳转的方式
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Jan 8, 2023
1 parent 3b4224d commit 1290c1e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
51 changes: 47 additions & 4 deletions client/web/plugins/com.msgbyte.notify/src/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getGlobalState,
getCachedUserInfo,
getServiceWorkerRegistration,
navigate,
} from '@capital/common';
import { Translate } from './translate';
import { hasSilent } from './silent';
Expand All @@ -12,6 +13,18 @@ export function initNotify() {
Notification.requestPermission();
}

const registration: ServiceWorkerRegistration | null =
getServiceWorkerRegistration();

if (registration) {
registration.addEventListener('notificationclick', (e: any) => {
const tag = e.notification.tag;
const data = e.notification.data;

handleMessageNotifyClick(tag, data);
});
}

regSocketEventListener({
eventName: 'chat.message.add',
eventFn: (message) => {
Expand All @@ -34,21 +47,26 @@ export function initNotify() {
const icon = userInfo?.avatar ?? undefined;
const content = message.content;

const registration: ServiceWorkerRegistration | null =
getServiceWorkerRegistration();

const title = `${Translate.from} ${nickname}`;
const options: NotificationOptions = {
body: content,
icon,
tag: 'tailchat-message',
renotify: true,
data: message,
};

if (registration && registration.showNotification) {
registration.showNotification(title, options);
} else {
// fallback
new Notification(title, options);
const notification = new Notification(title, options);
notification.onclick = (e: any) => {
const tag = e.target.tag;
const data = e.target.data;

handleMessageNotifyClick(tag, data);
};
}
}
);
Expand All @@ -57,3 +75,28 @@ export function initNotify() {
},
});
}

/**
* 点击通知栏事件
*/
function handleMessageNotifyClick(tag, data) {
if (tag === 'tailchat-message') {
const message = data;

window.focus();
const { converseId, groupId } = message ?? {};
if (!converseId) {
console.warn('[notify] Not found converseId');
return;
}
if (groupId) {
// 群组消息
navigate(`/main/group/${groupId}/${converseId}`);
} else {
// 私信会话
navigate(`/main/personal/converse/${converseId}`);
}
}

console.log(tag, data);
}
2 changes: 2 additions & 0 deletions client/web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { LoadingSpinner } from './components/LoadingSpinner';
import { pluginRootRoute } from './plugin/common';
import { PortalHost as FallbackPortalHost } from './components/Portal';
import isElectron from 'is-electron';
import { AppRouterApi } from './components/AppRouterApi';

const AppRouter: any = isElectron() ? HashRouter : BrowserRouter;

Expand Down Expand Up @@ -108,6 +109,7 @@ export const App: React.FC = React.memo(() => {
<AppProvider>
<AppHeader />
<AppContainer>
<AppRouterApi />
<Routes>
<Route
path="/entry/*"
Expand Down
15 changes: 15 additions & 0 deletions client/web/src/components/AppRouterApi.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import type { PropsWithChildren } from 'react';
import { useNavigate, NavigateFunction } from 'react-router';

export let navigate: NavigateFunction = () => {
throw new Error('route navigate not init');
};

export const AppRouterApi: React.FC<PropsWithChildren> = React.memo(() => {
const _navigate = useNavigate();
navigate = _navigate;

return null;
});
AppRouterApi.displayName = 'AppRouterApi';
1 change: 1 addition & 0 deletions client/web/src/plugin/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export {
joinArray,
} from 'tailchat-shared';

export { navigate } from '@/components/AppRouterApi';
export { useLocation, useNavigate } from 'react-router';

export {
Expand Down

0 comments on commit 1290c1e

Please sign in to comment.