feat: add handleLaunchModule function and intercept module launch URLs in notifications

This commit is contained in:
Yashwin 2026-06-25 18:44:51 +05:30
parent 2ca2afd1c1
commit ddbd2e5567

View File

@ -34,6 +34,7 @@ import { Layout } from "@/components/layout/Layout";
import { Button } from "@/components/ui/button";
import { PrimaryButton } from "@/components/shared/PrimaryButton";
import { notificationService } from "@/services/notification-service";
import { moduleService } from "@/services/module-service";
import { showToast } from "@/utils/toast";
const getNotificationIcon = (
@ -90,6 +91,7 @@ export const Notifications = () => {
const { notifications, unread_count, isLoading } = useAppSelector(
(state) => state.notifications,
);
const { roles, tenantId } = useAppSelector((state) => state.auth);
const [activeFilter, setActiveFilter] = useState<FilterType>("all");
const [searchQuery, setSearchQuery] = useState("");
@ -158,6 +160,34 @@ export const Notifications = () => {
}
};
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 handleAction = (notification: Notification) => {
if (!notification.is_read) {
handleMarkRead(notification.id);
@ -176,6 +206,15 @@ export const Notifications = () => {
// }
if (notification.action_url) {
const match = notification.action_url.match(
/^\/?modules\/([a-fA-F0-9-]+)\/launch\/?$/,
);
if (match) {
const moduleId = match[1];
handleLaunchModule(moduleId);
return;
}
const targetUrl = notification.action_url.startsWith("/tenant")
? notification.action_url
: `/tenant${notification.action_url.startsWith("/") ? "" : "/"}${notification.action_url}`;