feat: notify增加点击输入框跳转的功能,并增加api调用页面跳转的方式

pull/64/head
moonrailgun 2 years ago
parent 3b4224d50a
commit 1290c1e426

@ -3,6 +3,7 @@ import {
getGlobalState,
getCachedUserInfo,
getServiceWorkerRegistration,
navigate,
} from '@capital/common';
import { Translate } from './translate';
import { hasSilent } from './silent';
@ -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) => {
@ -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);
};
}
}
);
@ -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);
}

@ -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;
@ -108,6 +109,7 @@ export const App: React.FC = React.memo(() => {
<AppProvider>
<AppHeader />
<AppContainer>
<AppRouterApi />
<Routes>
<Route
path="/entry/*"

@ -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';

@ -56,6 +56,7 @@ export {
joinArray,
} from 'tailchat-shared';
export { navigate } from '@/components/AppRouterApi';
export { useLocation, useNavigate } from 'react-router';
export {

Loading…
Cancel
Save