kibana/packages/cloud/deployment_details/services.tsx
2023-09-28 04:20:53 -07:00

123 lines
2.9 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import React, { FC, useContext } from 'react';
export interface DeploymentDetailsContextValue {
cloudId?: string;
elasticsearchUrl?: string;
managementUrl?: string;
learnMoreUrl: string;
navigateToUrl(url: string): Promise<void>;
}
const DeploymentDetailsContext = React.createContext<DeploymentDetailsContextValue | null>(null);
/**
* Abstract external service Provider.
*/
export const DeploymentDetailsProvider: FC<DeploymentDetailsContextValue> = ({
children,
...services
}) => {
return (
<DeploymentDetailsContext.Provider value={services}>
{children}
</DeploymentDetailsContext.Provider>
);
};
/**
* Kibana-specific service types.
*/
export interface DeploymentDetailsKibanaDependencies {
/** CoreStart contract */
core: {
application: {
navigateToUrl(url: string): Promise<void>;
};
};
/** SharePluginStart contract */
share: {
url: {
locators: {
get(
id: string
): undefined | { useUrl: (params: { sectionId: string; appId: string }) => string };
};
};
};
/** CloudSetup contract */
cloud: {
isCloudEnabled: boolean;
cloudId?: string;
elasticsearchUrl?: string;
};
/** DocLinksStart contract */
docLinks: {
links: {
fleet: {
apiKeysLearnMore: string;
};
};
};
}
/**
* Kibana-specific Provider that maps to known dependency types.
*/
export const DeploymentDetailsKibanaProvider: FC<DeploymentDetailsKibanaDependencies> = ({
children,
...services
}) => {
const {
core: {
application: { navigateToUrl },
},
cloud: { isCloudEnabled, cloudId, elasticsearchUrl },
share: {
url: { locators },
},
docLinks: {
links: {
fleet: { apiKeysLearnMore },
},
},
} = services;
const managementUrl = locators
.get('MANAGEMENT_APP_LOCATOR')
?.useUrl({ sectionId: 'security', appId: 'api_keys' });
return (
<DeploymentDetailsProvider
cloudId={isCloudEnabled ? cloudId : undefined}
elasticsearchUrl={elasticsearchUrl}
managementUrl={managementUrl}
learnMoreUrl={apiKeysLearnMore}
navigateToUrl={navigateToUrl}
>
{children}
</DeploymentDetailsProvider>
);
};
/**
* React hook for accessing pre-wired services.
*/
export function useDeploymentDetails() {
const context = useContext(DeploymentDetailsContext);
if (!context) {
throw new Error(
'DeploymentDetailsContext is missing. Ensure your component or React root is wrapped with <DeploymentDetailsProvider /> or <DeploymentDetailsKibanaProvider />.'
);
}
return context;
}