feat: update main chat and system chat UI
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { appendClientIdHeader } from './clientIdentity';
|
||||
import { appendClientIdHeader, getOrCreateClientId } from './clientIdentity';
|
||||
|
||||
function resolveNotificationApiBaseUrl() {
|
||||
if (import.meta.env.VITE_WORK_SERVER_URL) {
|
||||
@@ -100,6 +100,10 @@ export type ClientNotificationSendResult = {
|
||||
};
|
||||
};
|
||||
|
||||
function normalizeNotificationOriginValue(value: unknown) {
|
||||
return typeof value === 'string' ? value.trim() : '';
|
||||
}
|
||||
|
||||
function getCurrentAppOrigin() {
|
||||
if (typeof window === 'undefined') {
|
||||
return '';
|
||||
@@ -116,6 +120,41 @@ function getCurrentAppDomain() {
|
||||
return window.location.hostname;
|
||||
}
|
||||
|
||||
function appendNotificationOriginToBody(body: string, data?: Record<string, string>) {
|
||||
const normalizedBody = String(body ?? '').trim();
|
||||
const appOrigin = normalizeNotificationOriginValue(data?.appOrigin);
|
||||
const appDomain = normalizeNotificationOriginValue(data?.appDomain);
|
||||
const originLabel = appOrigin || appDomain;
|
||||
|
||||
if (!originLabel) {
|
||||
return normalizedBody;
|
||||
}
|
||||
|
||||
const originLine = `origin: ${originLabel}`;
|
||||
|
||||
if (normalizedBody.includes(originLine)) {
|
||||
return normalizedBody;
|
||||
}
|
||||
|
||||
return normalizedBody ? `${normalizedBody}\n${originLine}` : originLine;
|
||||
}
|
||||
|
||||
function withCurrentAppOriginMetadata(data?: Record<string, string>) {
|
||||
const metadata = { ...(data ?? {}) };
|
||||
const appOrigin = getCurrentAppOrigin().trim();
|
||||
const appDomain = getCurrentAppDomain().trim();
|
||||
|
||||
if (appOrigin && !normalizeNotificationOriginValue(metadata.appOrigin)) {
|
||||
metadata.appOrigin = appOrigin;
|
||||
}
|
||||
|
||||
if (appDomain && !normalizeNotificationOriginValue(metadata.appDomain)) {
|
||||
metadata.appDomain = appDomain;
|
||||
}
|
||||
|
||||
return metadata;
|
||||
}
|
||||
|
||||
export type NotificationMessagePriority = 'low' | 'normal' | 'high' | 'urgent';
|
||||
export type NotificationMessageListStatus = 'all' | 'unread';
|
||||
export const NOTIFICATION_MESSAGES_UPDATED_EVENT = 'work-server.notification-messages-updated';
|
||||
@@ -761,11 +800,14 @@ export async function registerWebPushSubscription(
|
||||
subscription: WebPushSubscriptionPayload,
|
||||
deviceId?: string,
|
||||
) {
|
||||
const clientId = getOrCreateClientId().trim();
|
||||
|
||||
return request<{ ok: boolean; endpoint: string }>('/notifications/subscriptions/web', {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify({
|
||||
subscription,
|
||||
deviceId,
|
||||
clientId: clientId || undefined,
|
||||
userAgent: typeof navigator !== 'undefined' ? navigator.userAgent : '',
|
||||
appOrigin: getCurrentAppOrigin(),
|
||||
appDomain: getCurrentAppDomain(),
|
||||
@@ -801,9 +843,10 @@ export async function showLocalClientNotification(payload: ClientNotificationPay
|
||||
return false;
|
||||
}
|
||||
|
||||
const notificationData = withCurrentAppOriginMetadata(payload.data);
|
||||
const notificationOptions = {
|
||||
body: payload.body,
|
||||
data: payload.data ?? {},
|
||||
body: appendNotificationOriginToBody(payload.body, notificationData),
|
||||
data: notificationData,
|
||||
tag: payload.threadId ?? payload.data?.notificationKey ?? undefined,
|
||||
badge: '/pwa-192x192.svg',
|
||||
icon: '/pwa-192x192.svg',
|
||||
@@ -831,8 +874,12 @@ export async function showLocalClientNotification(payload: ClientNotificationPay
|
||||
}
|
||||
|
||||
export async function sendClientNotification(payload: ClientNotificationPayload) {
|
||||
const notificationData = withCurrentAppOriginMetadata(payload.data);
|
||||
return request<ClientNotificationSendResult>('/notifications/send', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload),
|
||||
body: JSON.stringify({
|
||||
...payload,
|
||||
data: notificationData,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user