Skip to content

Commit

Permalink
refactor: replace useAuth imports with useShellHooks for improved con…
Browse files Browse the repository at this point in the history
…text management
  • Loading branch information
hervedombya committed Dec 18, 2024
1 parent 944c7cc commit ad803ca
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createContext, useContext } from 'react';
import { IAccountsLocationsEndpointsAdapter } from '../adapters/accounts-locations/IAccountsLocationsEndpointsBundledAdapter';
import { PensieveAccountsLocationsAdapter } from '../adapters/accounts-locations/PensieveAccountsLocationsAdapter';
import { useAuth, useInstanceId } from './AuthProvider';
import { useInstanceId } from './AuthProvider';
import { useConfig } from './ConfigProvider';

import { useShellHooks } from '../../ShellHooksContext';
const _AccountsLocationsEndpointsAdapterContext = createContext<null | {
accountsLocationsEndpointsAdapter: IAccountsLocationsEndpointsAdapter;
}>(null);
Expand All @@ -26,6 +26,7 @@ export const AccountsLocationsEndpointsAdapterProvider = ({
}: {
children: JSX.Element;
}) => {
const { useAuth } = useShellHooks();
const { getToken } = useAuth();
const instanceId = useInstanceId();
const { managementEndpoint } = useConfig();
Expand Down
9 changes: 6 additions & 3 deletions src/react/next-architecture/ui/AlertProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import {
FilterLabels,
} from 'shell/compiled-types/src/alerts/services/alertUtils';
import { useXcoreRuntimeConfig } from './ConfigProvider';
import { useShellAlerts } from '../../ShellHooksContext';

export const highestAlertToStatus = (alerts?: Alert[]): string => {
return (alerts?.[0] && alerts[0].severity) || 'healthy';
};

export const useAlerts = (filters: FilterLabels) => {
return window.shellAlerts.hooks.useAlerts(filters);
const { alertHooks } = useShellAlerts();
return alertHooks.useAlerts(filters);
};

const AlertProvider = ({ children }: { children: React.ReactNode }) => {
const xcoreConfig = useXcoreRuntimeConfig();
const { AlertsProvider } = useShellAlerts();

let alertManagerUrl = '';
if (xcoreConfig) {
Expand All @@ -24,9 +27,9 @@ const AlertProvider = ({ children }: { children: React.ReactNode }) => {
}

return (
<window.shellAlerts.AlertsProvider alertManagerUrl={alertManagerUrl}>
<AlertsProvider alertManagerUrl={alertManagerUrl}>
{children}
</window.shellAlerts.AlertsProvider>
</AlertsProvider>
);
};

Expand Down
4 changes: 3 additions & 1 deletion src/react/next-architecture/ui/LocationAdapterProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createContext, useContext } from 'react';
import { ILocationsAdapter } from '../adapters/accounts-locations/ILocationsAdapter';
import { PensieveAccountsLocationsAdapter } from '../adapters/accounts-locations/PensieveAccountsLocationsAdapter';
import { useAuth, useInstanceId } from './AuthProvider';
import { useInstanceId } from './AuthProvider';
import { useConfig } from './ConfigProvider';
import { useShellHooks } from '../../ShellHooksContext';

const _LocationAdapterContext = createContext<null | {
locationAdapter: ILocationsAdapter;
Expand All @@ -25,6 +26,7 @@ export const LocationAdapterProvider = ({
}: {
children: JSX.Element;
}) => {
const { useAuth } = useShellHooks();
const { getToken } = useAuth();
const instanceId = useInstanceId();
const { managementEndpoint } = useConfig();
Expand Down
4 changes: 3 additions & 1 deletion src/react/next-architecture/ui/MetricsAdapterProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createContext, useContext, ReactNode } from 'react';
import { PensieveMetricsAdapter } from '../adapters/metrics/PensieveMetricsAdapter';
import { IMetricsAdapter } from '../adapters/metrics/IMetricsAdapter';
import { useAuth, useInstanceId } from './AuthProvider';
import { useInstanceId } from './AuthProvider';
import { useConfig } from './ConfigProvider';
import { useShellHooks } from '../../ShellHooksContext';

const _MetricsAdapterContext = createContext<null | {
metricsAdapter: IMetricsAdapter;
Expand All @@ -21,6 +22,7 @@ export const useMetricsAdapter = (): IMetricsAdapter => {
};

const MetricsAdapterProvider = ({ children }: { children: ReactNode }) => {
const { useAuth } = useShellHooks();
const { getToken } = useAuth();
const instanceId = useInstanceId();
const { managementEndpoint } = useConfig();
Expand Down
4 changes: 2 additions & 2 deletions src/react/next-architecture/ui/S3ClientProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { createContext, PropsWithChildren, useContext, useMemo } from 'react';
import { AWSError, S3, STS } from 'aws-sdk';
import STSClient from '../../../js/STSClient';
import { useConfig } from './ConfigProvider';
import { useAuth } from './AuthProvider';
import { notFalsyTypeGuard } from '../../../types/typeGuards';
import ZenkoClient from '../../../js/ZenkoClient';
import { useDispatch } from 'react-redux';
import IAMClient from '../../../js/IAMClient';
import { _IAMContext } from '../../IAMProvider';
import { PromiseResult } from 'aws-sdk/lib/request';
import { genClientEndpoint } from '../../utils';
import { useShellHooks } from '../../ShellHooksContext';

const S3ClientContext = createContext<S3 | null>(null);
const ZenkoClientContext = createContext<ZenkoClient | null>(null);
Expand Down Expand Up @@ -148,7 +148,7 @@ export const S3ClientWithoutReduxProvider = ({

export const useAssumeRoleQuery = () => {
const { stsEndpoint } = useConfig();

const { useAuth } = useShellHooks();
const { getToken } = useAuth();
const user = useAuth();
const roleSessionName = `ui-${user.userData?.id}`;
Expand Down

0 comments on commit ad803ca

Please sign in to comment.