feat: implement module launching logic and intercept module-related notification action URLs in NotificationBell
This commit is contained in:
parent
ddbd2e5567
commit
61aa2dbb8c
@ -28,6 +28,7 @@ import type {
|
||||
NotificationCategory,
|
||||
} from "@/types/notification";
|
||||
import { notificationService } from "@/services/notification-service";
|
||||
import { moduleService } from "@/services/module-service";
|
||||
import { showToast } from "@/utils/toast";
|
||||
|
||||
const getNotificationIcon = (
|
||||
@ -83,7 +84,7 @@ export const NotificationBell = () => {
|
||||
const { notifications, unread_count } = useAppSelector(
|
||||
(state) => state.notifications,
|
||||
);
|
||||
const { roles } = useAppSelector((state) => state.auth);
|
||||
const { roles, tenantId } = useAppSelector((state) => state.auth);
|
||||
const dropdownRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
// Handle click outside
|
||||
@ -115,6 +116,34 @@ export const NotificationBell = () => {
|
||||
dispatch(readAllAsync());
|
||||
};
|
||||
|
||||
const handleLaunchModule = async (moduleId: string): Promise<void> => {
|
||||
try {
|
||||
let rolesArray: string[] = [];
|
||||
if (Array.isArray(roles)) {
|
||||
rolesArray = roles;
|
||||
} else if (typeof roles === "string") {
|
||||
try {
|
||||
rolesArray = JSON.parse(roles);
|
||||
} catch {
|
||||
rolesArray = [];
|
||||
}
|
||||
}
|
||||
const isSuperAdmin = rolesArray.includes("super_admin");
|
||||
const launchTenantId = isSuperAdmin ? tenantId : null;
|
||||
|
||||
const response = await moduleService.launch(moduleId, launchTenantId);
|
||||
if (response.success && response.data?.launch_url) {
|
||||
window.open(response.data.launch_url, "_blank", "noopener,noreferrer");
|
||||
} else {
|
||||
showToast.error("Failed to retrieve launch URL");
|
||||
}
|
||||
} catch (err: any) {
|
||||
showToast.error(
|
||||
err?.response?.data?.error?.message || "Failed to launch module",
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const handleNotificationClick = (notification: Notification) => {
|
||||
if (!notification.is_read) {
|
||||
dispatch(markReadAsync(notification.id));
|
||||
@ -135,6 +164,16 @@ export const NotificationBell = () => {
|
||||
// }
|
||||
|
||||
if (notification.action_url) {
|
||||
const match = notification.action_url.match(
|
||||
/^\/?modules\/([a-fA-F0-9-]+)\/launch\/?$/,
|
||||
);
|
||||
if (match) {
|
||||
const moduleId = match[1];
|
||||
handleLaunchModule(moduleId);
|
||||
setIsOpen(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const targetUrl = notification.action_url.startsWith("/tenant")
|
||||
? notification.action_url
|
||||
: `/tenant${notification.action_url.startsWith("/") ? "" : "/"}${notification.action_url}`;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user