|
| 1 | +import { getInstanceClient } from '@/config/getInstanceClient'; |
1 | 2 | import { getInstanceClientIdFromParams } from '@/config/useInstanceClient'; |
| 3 | +import { |
| 4 | + AuthenticatedCloudConnection, |
| 5 | + AuthenticatedConnection, |
| 6 | + authStore, |
| 7 | + OverallAppSignIn, |
| 8 | +} from '@/features/auth/store/authStore'; |
2 | 9 | import { clusterLayoutRoute } from '@/features/cluster/clusterLayoutRoute'; |
3 | 10 | import { InstanceLayout } from '@/features/instance/InstanceLayout'; |
| 11 | +import { getInstanceUserInfo } from '@/features/instance/operations/queries/getInstanceUserInfo'; |
4 | 12 | import { getRegistrationInfoQueryOptions } from '@/features/instance/operations/queries/getRegistrationInfo'; |
| 13 | +import { getOrganizationClusterInstancePermissions, getOrganizationClusterPermissions } from '@/hooks/usePermissions'; |
5 | 14 | import { buildRedirectInSearch } from '@/lib/urls/buildRedirectInSearch'; |
6 | 15 | import { dashboardLayout } from '@/router/dashboardRoute'; |
| 16 | +import { QueryClient } from '@tanstack/react-query'; |
7 | 17 | import { createRoute, redirect } from '@tanstack/react-router'; |
8 | 18 |
|
9 | 19 | export function createInstanceLayoutRoute(mode: 'local' | 'cluster' | 'instance') { |
10 | | - if (mode === 'local') { |
11 | | - return createRoute({ |
12 | | - getParentRoute: () => dashboardLayout, |
13 | | - id: '_instanceLayout', |
14 | | - component: InstanceLayout, |
15 | | - loader: async ({ context, params }) => { |
16 | | - const operationsParams = getInstanceClientIdFromParams(params); |
17 | | - return context.queryClient.ensureQueryData(getRegistrationInfoQueryOptions(operationsParams)); |
18 | | - }, |
19 | | - }); |
| 20 | + switch (mode) { |
| 21 | + case 'local': |
| 22 | + return createRoute({ |
| 23 | + getParentRoute: () => dashboardLayout, |
| 24 | + id: '_instanceLayout', |
| 25 | + component: InstanceLayout, |
| 26 | + loader: registrationInfoLoader, |
| 27 | + }); |
| 28 | + case 'cluster': |
| 29 | + return createRoute({ |
| 30 | + getParentRoute: () => clusterLayoutRoute, |
| 31 | + id: '_instanceLayout', |
| 32 | + component: InstanceLayout, |
| 33 | + beforeLoad: checkClusterInstanceAuthenticationBeforeLoad, |
| 34 | + loader: registrationInfoLoader, |
| 35 | + }); |
| 36 | + case 'instance': |
| 37 | + return createRoute({ |
| 38 | + getParentRoute: () => clusterLayoutRoute, |
| 39 | + path: 'instance/$instanceId', |
| 40 | + component: InstanceLayout, |
| 41 | + beforeLoad: checkClusterInstanceAuthenticationBeforeLoad, |
| 42 | + loader: registrationInfoLoader, |
| 43 | + }); |
20 | 44 | } |
21 | | - if (mode === 'cluster') { |
22 | | - return createRoute({ |
23 | | - getParentRoute: () => clusterLayoutRoute, |
24 | | - id: '_instanceLayout', |
25 | | - component: InstanceLayout, |
26 | | - beforeLoad: async ({ context, params }) => { |
27 | | - const auth = context.authentication[params.clusterId]; |
28 | | - if (!auth || (!auth.isLoading && !auth.user)) { |
29 | | - const to = `/${params.organizationId}/${params.clusterId}/sign-in`; |
30 | | - throw redirect({ to, search: buildRedirectInSearch() }); |
31 | | - } |
32 | | - }, |
33 | | - loader: async ({ context, params }) => { |
34 | | - const operationsParams = getInstanceClientIdFromParams(params); |
35 | | - return context.queryClient.ensureQueryData(getRegistrationInfoQueryOptions(operationsParams)); |
36 | | - }, |
| 45 | +} |
| 46 | + |
| 47 | +async function checkClusterInstanceAuthenticationBeforeLoad({ |
| 48 | + context, |
| 49 | + params, |
| 50 | +}: { |
| 51 | + context: { authentication: Record<string, AuthenticatedConnection> }, |
| 52 | + params: { organizationId: string; clusterId: string, instanceId?: string } |
| 53 | +}) { |
| 54 | + // Are we already authenticated? |
| 55 | + const auth = context.authentication[params.instanceId || params.clusterId]; |
| 56 | + if (auth?.isLoading || auth?.user) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // See if we can Fabric Connect. |
| 61 | + const overallAuth = context.authentication[OverallAppSignIn] as AuthenticatedCloudConnection; |
| 62 | + if (overallAuth?.isLoading) { |
| 63 | + return; |
| 64 | + } |
| 65 | + const { update } = params.instanceId |
| 66 | + ? getOrganizationClusterInstancePermissions(overallAuth.user, params.organizationId, params.clusterId) |
| 67 | + : getOrganizationClusterPermissions(overallAuth.user, params.organizationId, params.clusterId); |
| 68 | + if (update) { |
| 69 | + const entityId = params.instanceId || params.clusterId; |
| 70 | + const instanceClient = getInstanceClient({ |
| 71 | + id: entityId, |
| 72 | + forceFabricConnect: true, |
37 | 73 | }); |
| 74 | + try { |
| 75 | + const user = await getInstanceUserInfo({ instanceClient }); |
| 76 | + authStore.setUserForIdAndKey( |
| 77 | + entityId, |
| 78 | + instanceClient.defaults.baseURL!, |
| 79 | + user, |
| 80 | + ); |
| 81 | + authStore.flagForFabricConnect(entityId, true); |
| 82 | + return; |
| 83 | + } catch (err) { |
| 84 | + console.error('Fabric Connect not established', err); |
| 85 | + } |
38 | 86 | } |
39 | | - return createRoute({ |
40 | | - getParentRoute: () => clusterLayoutRoute, |
41 | | - path: 'instance/$instanceId', |
42 | | - component: InstanceLayout, |
43 | | - beforeLoad: async ({ context, params }) => { |
44 | | - const auth = context.authentication[params.instanceId]; |
45 | | - if (!auth || (!auth.isLoading && !auth.user)) { |
46 | | - const to = `/${params.organizationId}/${params.clusterId}/instance/${params.instanceId}/sign-in`; |
47 | | - throw redirect({ to, search: buildRedirectInSearch() }); |
48 | | - } |
49 | | - }, |
50 | | - loader: async ({ context, params }) => { |
51 | | - const operationsParams = getInstanceClientIdFromParams(params); |
52 | | - return context.queryClient.ensureQueryData(getRegistrationInfoQueryOptions(operationsParams)); |
53 | | - }, |
54 | | - }); |
| 87 | + |
| 88 | + const to = `/${params.organizationId}/${params.clusterId}/` |
| 89 | + + (params.instanceId ? `instance/${params.instanceId}` : '') |
| 90 | + + `/sign-in`; |
| 91 | + throw redirect({ to, search: buildRedirectInSearch() }); |
| 92 | +} |
| 93 | + |
| 94 | +function registrationInfoLoader({ |
| 95 | + context, |
| 96 | + params, |
| 97 | +}: { |
| 98 | + context: { queryClient: QueryClient }, |
| 99 | + params: { organizationId?: string; clusterId?: string, instanceId?: string }, |
| 100 | +}) { |
| 101 | + const operationsParams = getInstanceClientIdFromParams(params); |
| 102 | + return context.queryClient.ensureQueryData(getRegistrationInfoQueryOptions(operationsParams)); |
55 | 103 | } |
0 commit comments