Skip to content

Commit

Permalink
Refactor top-level Kibana props to a global context state
Browse files Browse the repository at this point in the history
- rather them passing them around verbosely as props, the components that need them should be able to call the useContext hook

+ Remove IAppSearchProps in favor of IKibanaContext

+ Also rename `appSearchUrl` to `enterpriseSearchUrl`, since this context will contained shared/Kibana-wide values/actions useful to both AS and WS
  • Loading branch information
cee-chen committed Jun 4, 2020
1 parent 68773c0 commit 2f37a66
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import React, { useContext } from 'react';
import { EuiPage, EuiPageBody, EuiPageContent, EuiEmptyPrompt, EuiButton } from '@elastic/eui';

import { SetAppSearchBreadcrumbs as SetBreadcrumbs } from '../../../shared/kibana_breadcrumbs';
import { IAppSearchProps } from '../../index';
import { KibanaContext, IKibanaContext } from '../../../index';

import { EngineOverviewHeader } from '../engine_overview_header';

import './empty_states.scss';

export const EmptyState: React.FC<IAppSearchProps> = ({ appSearchUrl, setBreadcrumbs }) => {
export const EmptyState: React.FC<> = () => {
const { enterpriseSearchUrl } = useContext(KibanaContext) as IKibanaContext;

return (
<EuiPage restrictWidth className="empty-state">
<SetBreadcrumbs setBreadcrumbs={setBreadcrumbs} isRoot />
<SetBreadcrumbs isRoot />

<EuiPageBody>
<EngineOverviewHeader appSearchUrl={appSearchUrl} />
<EngineOverviewHeader />
<EuiPageContent>
<EuiEmptyPrompt
iconType="eyeClosed"
Expand All @@ -36,7 +38,7 @@ export const EmptyState: React.FC<IAppSearchProps> = ({ appSearchUrl, setBreadcr
<EuiButton
iconType="popout"
fill
href={`${appSearchUrl}/as/engines/new`}
href={`${enterpriseSearchUrl}/as/engines/new`}
target="_blank"
>
Create your first Engine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import React, { useContext } from 'react';
import { EuiPage, EuiPageBody, EuiPageContent, EuiEmptyPrompt, EuiCode } from '@elastic/eui';

import { EuiButton } from '../../../shared/react_router_helpers';
import { SetAppSearchBreadcrumbs as SetBreadcrumbs } from '../../../shared/kibana_breadcrumbs';
import { IAppSearchProps } from '../../index';
import { KibanaContext, IKibanaContext } from '../../../index';

import { EngineOverviewHeader } from '../engine_overview_header';

import './empty_states.scss';

export const ErrorState: ReactFC<IAppSearchProps> = ({ appSearchUrl, setBreadcrumbs }) => {
export const ErrorState: ReactFC<> = () => {
const { enterpriseSearchUrl } = useContext(KibanaContext) as IKibanaContext;

return (
<EuiPage restrictWidth className="empty-state">
<SetBreadcrumbs setBreadcrumbs={setBreadcrumbs} isRoot />
<SetBreadcrumbs isRoot />

<EuiPageBody>
<EngineOverviewHeader />
Expand All @@ -32,7 +34,7 @@ export const ErrorState: ReactFC<IAppSearchProps> = ({ appSearchUrl, setBreadcru
<>
<p>
We cannot connect to the App Search instance at the configured host URL:{' '}
<EuiCode>{appSearchUrl}</EuiCode>
<EuiCode>{enterpriseSearchUrl}</EuiCode>
</p>
<p>
Please ensure your App Search host URL is configured correctly within{' '}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ import React from 'react';
import { EuiPage, EuiPageBody, EuiPageContent, EuiSpacer, EuiLoadingContent } from '@elastic/eui';

import { SetAppSearchBreadcrumbs as SetBreadcrumbs } from '../../../shared/kibana_breadcrumbs';
import { IAppSearchProps } from '../../index';

import { EngineOverviewHeader } from '../engine_overview_header';

import './empty_states.scss';

export const LoadingState: React.FC<IAppSearchProps> = ({ appSearchUrl, setBreadcrumbs }) => {
export const LoadingState: React.FC<> = () => {
return (
<EuiPage restrictWidth className="engine-overview empty-state">
<SetBreadcrumbs setBreadcrumbs={setBreadcrumbs} isRoot />
<SetBreadcrumbs isRoot />

<EuiPageBody>
<EngineOverviewHeader appSearchUrl={appSearchUrl} />
<EngineOverviewHeader />
<EuiPageContent>
<EuiLoadingContent lines={5} />
<EuiSpacer size="xxl" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ import React from 'react';
import { EuiPage, EuiPageBody, EuiPageContent, EuiEmptyPrompt, EuiCode } from '@elastic/eui';

import { SetAppSearchBreadcrumbs as SetBreadcrumbs } from '../../../shared/kibana_breadcrumbs';
import { IAppSearchProps } from '../../index';

import { EngineOverviewHeader } from '../engine_overview_header';
import { getUserName } from '../../utils/get_username';

import './empty_states.scss';

export const NoUserState: React.FC<IAppSearchProps> = ({ appSearchUrl, setBreadcrumbs }) => {
export const NoUserState: React.FC<> = () => {
const username = getUserName();

return (
<EuiPage restrictWidth className="empty-state">
<SetBreadcrumbs setBreadcrumbs={setBreadcrumbs} isRoot />
<SetBreadcrumbs isRoot />

<EuiPageBody>
<EngineOverviewHeader appSearchUrl={appSearchUrl} />
<EngineOverviewHeader />
<EuiPageContent>
<EuiEmptyPrompt
iconType="lock"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { useEffect, useState } from 'react';
import React, { useContext, useEffect, useState } from 'react';
import {
EuiPage,
EuiPageBody,
Expand All @@ -16,7 +16,7 @@ import {
} from '@elastic/eui';

import { SetAppSearchBreadcrumbs as SetBreadcrumbs } from '../../../shared/kibana_breadcrumbs';
import { IAppSearchProps } from '../../index';
import { KibanaContext, IKibanaContext } from '../../../index';

import EnginesIcon from '../../assets/engine.svg';
import MetaEnginesIcon from '../../assets/meta_engine.svg';
Expand All @@ -27,8 +27,8 @@ import { EngineTable } from './engine_table';

import './engine_overview.scss';

export const EngineOverview: ReactFC<IAppSearchProps> = props => {
const { http, appSearchUrl } = props;
export const EngineOverview: ReactFC<> = () => {
const { http } = useContext(KibanaContext) as IKibanaContext;

const [isLoading, setIsLoading] = useState(true);
const [hasNoAccount, setHasNoAccount] = useState(false);
Expand Down Expand Up @@ -88,17 +88,17 @@ export const EngineOverview: ReactFC<IAppSearchProps> = props => {
}, [metaEnginesPage]); // eslint-disable-line
// TODO: https://reactjs.org/docs/hooks-faq.html#is-it-safe-to-omit-functions-from-the-list-of-dependencies

if (hasErrorConnecting) return <ErrorState {...props} />;
if (hasNoAccount) return <NoUserState {...props} />;
if (isLoading) return <LoadingState {...props} />;
if (!engines.length) return <EmptyState {...props} />;
if (hasErrorConnecting) return <ErrorState />;
if (hasNoAccount) return <NoUserState />;
if (isLoading) return <LoadingState />;
if (!engines.length) return <EmptyState />;

return (
<EuiPage restrictWidth className="engine-overview">
<SetBreadcrumbs {...props} isRoot />
<SetBreadcrumbs isRoot />

<EuiPageBody>
<EngineOverviewHeader appSearchUrl={appSearchUrl} />
<EngineOverviewHeader />

<EuiPageContent>
<EuiPageContentHeader>
Expand All @@ -117,7 +117,6 @@ export const EngineOverview: ReactFC<IAppSearchProps> = props => {
pageIndex: enginesPage - 1,
onPaginate: setEnginesPage,
}}
appSearchUrl={appSearchUrl}
/>
</EuiPageContentBody>

Expand All @@ -140,7 +139,6 @@ export const EngineOverview: ReactFC<IAppSearchProps> = props => {
pageIndex: metaEnginesPage - 1,
onPaginate: setMetaEnginesPage,
}}
appSearchUrl={appSearchUrl}
/>
</EuiPageContentBody>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import React, { useContext } from 'react';
import { EuiBasicTable, EuiLink } from '@elastic/eui';

import { KibanaContext, IKibanaContext } from '../../../index';

interface IEngineTableProps {
data: Array<{
name: string;
Expand All @@ -19,20 +21,20 @@ interface IEngineTableProps {
pageIndex: number;
onPaginate(pageIndex: number);
};
appSearchUrl: string;
}

export const EngineTable: ReactFC<IEngineTableProps> = ({
data,
pagination: { totalEngines, pageIndex = 0, onPaginate },
appSearchUrl,
}) => {
const { enterpriseSearchUrl } = useContext(KibanaContext) as IKibanaContext;

const columns = [
{
field: 'name',
name: 'Name',
render: name => (
<EuiLink href={`${appSearchUrl}/as/engines/${name}`} target="_blank">
<EuiLink href={`${enterpriseSearchUrl}/as/engines/${name}`} target="_blank">
{name}
</EuiLink>
),
Expand Down Expand Up @@ -77,7 +79,7 @@ export const EngineTable: ReactFC<IEngineTableProps> = ({
name: 'Actions',
dataType: 'string',
render: name => (
<EuiLink href={`${appSearchUrl}/as/engines/${name}`} target="_blank" color="primary">
<EuiLink href={`${enterpriseSearchUrl}/as/engines/${name}`} target="_blank" color="primary">
Manage
</EuiLink>
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import React, { useContext } from 'react';
import { EuiPageHeader, EuiPageHeaderSection, EuiTitle, EuiButton } from '@elastic/eui';

interface IEngineOverviewHeader {
appSearchUrl?: string;
}
import { KibanaContext, IKibanaContext } from '../../../index';

export const EngineOverviewHeader: React.FC<> = () => {
const { enterpriseSearchUrl } = useContext(KibanaContext) as IKibanaContext;

export const EngineOverviewHeader: React.FC<IEngineOverviewHeader> = ({ appSearchUrl }) => {
const buttonProps = {
fill: true,
iconType: 'popout',
};
if (appSearchUrl) {
buttonProps.href = `${appSearchUrl}/as`;
if (enterpriseSearchUrl) {
buttonProps.href = `${enterpriseSearchUrl}/as`;
buttonProps.target = '_blank';
} else {
buttonProps.isDisabled = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import { SetAppSearchBreadcrumbs as SetBreadcrumbs } from '../../../shared/kiban
import GettingStarted from '../../assets/getting_started.png';
import './setup_guide.scss';

export const SetupGuide: React.FC<> = props => {
export const SetupGuide: React.FC<> = () => {
return (
<EuiPage className="setup-guide">
<SetBreadcrumbs {...props} text="Setup Guide" />
<SetBreadcrumbs text="Setup Guide" />
<EuiPageSideBar>
<EuiText color="subdued" size="s">
<strong>Setup Guide</strong>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,25 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import React, { useContext } from 'react';
import { Route, Redirect } from 'react-router-dom';

import { HttpHandler } from 'src/core/public';
import { TSetBreadcrumbs } from '../../../shared/kibana_breadcrumbs';
import { KibanaContext, IKibanaContext } from '../index';

import { SetupGuide } from './components/setup_guide';
import { EngineOverview } from './components/engine_overview';

export interface IAppSearchProps {
appSearchUrl?: string;
http(): HttpHandler;
setBreadCrumbs(): TSetBreadcrumbs;
}
export const AppSearch: React.FC<> = () => {
const { enterpriseSearchUrl } = useContext(KibanaContext) as IKibanaContext;

export const AppSearch: React.FC<IAppSearchProps> = props => (
<>
<Route exact path="/app_search">
{!props.appSearchUrl ? (
<Redirect to="/app_search/setup_guide" />
) : (
<EngineOverview {...props} />
)}
</Route>
<Route path="/app_search/setup_guide">
<SetupGuide {...props} />
</Route>
</>
);
return (
<>
<Route exact path="/app_search">
{!enterpriseSearchUrl ? <Redirect to="/app_search/setup_guide" /> : <EngineOverview />}
</Route>
<Route path="/app_search/setup_guide">
<SetupGuide />
</Route>
</>
);
};
43 changes: 28 additions & 15 deletions x-pack/plugins/enterprise_search/public/applications/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,40 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter, Route, Redirect } from 'react-router-dom';

import { CoreStart, AppMountParams } from 'src/core/public';
import { CoreStart, AppMountParams, HttpHandler } from 'src/core/public';
import { ClientConfigType } from '../plugin';
import { TSetBreadcrumbs } from './shared/kibana_breadcrumbs';

import { AppSearch } from './app_search';

export interface IKibanaContext {
enterpriseSearchUrl?: string;
http(): HttpHandler;
setBreadCrumbs(): TSetBreadcrumbs;
}

export const KibanaContext = React.createContext();

export const renderApp = (core: CoreStart, params: AppMountParams, config: ClientConfigType) => {
ReactDOM.render(
<BrowserRouter basename={params.appBasePath}>
<Route exact path="/">
{/* This will eventually contain an Enterprise Search landing page,
and we'll also actually have a /workplace_search route */}
<Redirect to="/app_search" />
</Route>
<Route path="/app_search">
<AppSearch
http={core.http}
appSearchUrl={config.host}
setBreadcrumbs={core.chrome.setBreadcrumbs}
/>
</Route>
</BrowserRouter>,
<KibanaContext.Provider
value={{
http: core.http,
enterpriseSearchUrl: config.host,
setBreadcrumbs: core.chrome.setBreadcrumbs,
}}
>
<BrowserRouter basename={params.appBasePath}>
<Route exact path="/">
{/* This will eventually contain an Enterprise Search landing page,
and we'll also actually have a /workplace_search route */}
<Redirect to="/app_search" />
</Route>
<Route path="/app_search">
<AppSearch />
</Route>
</BrowserRouter>
</KibanaContext.Provider>,
params.element
);
return () => ReactDOM.unmountComponentAtNode(params.element);
Expand Down
Loading

0 comments on commit 2f37a66

Please sign in to comment.