Refactor Modules component to navigate to a redirect page with the launch URL as a query parameter instead of opening it in a new tab. Add new route for ModuleLaunchRedirect in tenant admin routes for improved URL handling.

This commit is contained in:
Yashwin 2026-02-03 19:40:34 +05:30
parent 2f10b23400
commit 46e8fd466b
3 changed files with 46 additions and 15 deletions

View File

@ -0,0 +1,36 @@
import { useEffect } from 'react';
import type { ReactElement } from 'react';
import { useSearchParams } from 'react-router-dom';
const ModuleLaunchRedirect = (): ReactElement => {
const [searchParams] = useSearchParams();
const launchUrl = searchParams.get('url');
useEffect(() => {
if (launchUrl) {
// Make a fetch request with custom headers
fetch(launchUrl, {
method: 'GET',
headers: {
'bypass-tunnel-reminder': 'true',
},
credentials: 'include',
})
.catch(() => {
// Ignore fetch errors, we'll still open the URL
})
.finally(() => {
// Open the external URL in a new tab
window.open(launchUrl, '_blank', 'noopener,noreferrer');
});
}
}, [launchUrl]);
return (
<div className="flex items-center justify-center min-h-screen">
<div className="text-sm text-[#6b7280]">Redirecting...</div>
</div>
);
};
export default ModuleLaunchRedirect;

View File

@ -1,5 +1,6 @@
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import type { ReactElement } from 'react'; import type { ReactElement } from 'react';
import { useNavigate } from 'react-router-dom';
import { Layout } from '@/components/layout/Layout'; import { Layout } from '@/components/layout/Layout';
import { import {
StatusBadge, StatusBadge,
@ -27,6 +28,7 @@ const getStatusVariant = (status: string | null): 'success' | 'failure' | 'proce
}; };
const Modules = (): ReactElement => { const Modules = (): ReactElement => {
const navigate = useNavigate();
const { roles, tenantId } = useAppSelector((state) => state.auth); const { roles, tenantId } = useAppSelector((state) => state.auth);
// const tenantId = useAppSelector((state) => state.auth.tenantId); // const tenantId = useAppSelector((state) => state.auth.tenantId);
const [modules, setModules] = useState<AssignedModule[]>([]); const [modules, setModules] = useState<AssignedModule[]>([]);
@ -80,21 +82,9 @@ const Modules = (): ReactElement => {
const launchTenantId = isSuperAdmin ? tenantId : null; const launchTenantId = isSuperAdmin ? tenantId : null;
const response = await moduleService.launch(moduleId, launchTenantId); const response = await moduleService.launch(moduleId, launchTenantId);
if (response.success && response.data.launch_url) { if (response.success && response.data.launch_url) {
// Make a fetch request with custom headers first // Navigate to redirect page with launch URL as query param
try { const encodedUrl = encodeURIComponent(response.data.launch_url);
await fetch(response.data.launch_url, { navigate(`/tenant/module-launch?url=${encodedUrl}`);
method: 'GET',
headers: {
'bypass-tunnel-reminder': 'true',
},
credentials: 'include',
});
} catch (fetchError) {
// Ignore fetch errors, we'll still open the URL
console.warn('Failed to send header request:', fetchError);
}
// Open the URL in a new tab
window.open(response.data.launch_url, '_blank', 'noopener,noreferrer');
} }
} catch (err: any) { } catch (err: any) {
setError(err?.response?.data?.error?.message || 'Failed to launch module'); setError(err?.response?.data?.error?.message || 'Failed to launch module');

View File

@ -8,6 +8,7 @@ const Settings = lazy(() => import('@/pages/tenant/Settings'));
const Users = lazy(() => import('@/pages/tenant/Users')); const Users = lazy(() => import('@/pages/tenant/Users'));
const AuditLogs = lazy(() => import('@/pages/tenant/AuditLogs')); const AuditLogs = lazy(() => import('@/pages/tenant/AuditLogs'));
const Modules = lazy(() => import('@/pages/tenant/Modules')); const Modules = lazy(() => import('@/pages/tenant/Modules'));
const ModuleLaunchRedirect = lazy(() => import('@/pages/ModuleLaunchRedirect'));
// Loading fallback component // Loading fallback component
const RouteLoader = (): ReactElement => ( const RouteLoader = (): ReactElement => (
@ -54,4 +55,8 @@ export const tenantAdminRoutes: RouteConfig[] = [
path: '/tenant/settings', path: '/tenant/settings',
element: <LazyRoute component={Settings} />, element: <LazyRoute component={Settings} />,
}, },
{
path: '/tenant/module-launch',
element: <LazyRoute component={ModuleLaunchRedirect} />,
},
]; ];