Skip to content

Commit

Permalink
Merge branch 'main' into improve_preview_pane
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine committed Feb 21, 2022
2 parents 33de410 + 1df6ecf commit 115b90f
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 32 deletions.
45 changes: 32 additions & 13 deletions src/plugins/vis_types/vega/public/components/vega_vis_component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* Side Public License, v 1.
*/

import React, { useEffect, useCallback, useRef } from 'react';
import { EuiResizeObserver } from '@elastic/eui';
import React, { useEffect, useRef, useMemo, useCallback } from 'react';
import { EuiResizeObserver, EuiResizeObserverProps } from '@elastic/eui';
import { throttle } from 'lodash';

import type { IInterpreterRenderHandlers, RenderMode } from 'src/plugins/expressions';
Expand All @@ -27,6 +27,8 @@ interface VegaVisComponentProps {

type VegaVisController = InstanceType<ReturnType<typeof createVegaVisualization>>;

const THROTTLE_INTERVAL = 300;

export const VegaVisComponent = ({
visData,
fireEvent,
Expand All @@ -35,38 +37,55 @@ export const VegaVisComponent = ({
renderMode,
}: VegaVisComponentProps) => {
const chartDiv = useRef<HTMLDivElement>(null);
const renderCompleted = useRef(false);
const visController = useRef<VegaVisController | null>(null);

useEffect(() => {
if (chartDiv.current) {
const VegaVis = createVegaVisualization(deps, renderMode);
visController.current = new VegaVis(chartDiv.current, fireEvent);
}

return () => {
visController.current?.destroy();
visController.current = null;
};
}, [deps, fireEvent, renderMode]);

useEffect(() => {
const asyncRender = async (visCtrl: VegaVisController) => {
await visCtrl.render(visData);
renderCompleted.current = true;
renderComplete();
};

if (visController.current) {
visController.current.render(visData).then(renderComplete);
asyncRender(visController.current);
}
}, [visData, renderComplete]);
}, [renderComplete, visData]);

const resizeChart = useMemo(
() =>
throttle(
(dimensions) => {
visController.current?.resize(dimensions);
},
THROTTLE_INTERVAL,
{ leading: false, trailing: true }
),
[]
);

/* eslint-disable-next-line react-hooks/exhaustive-deps */
const updateChartSize = useCallback(
throttle(() => {
if (visController.current) {
visController.current.render(visData).then(renderComplete);
const onContainerResize: EuiResizeObserverProps['onResize'] = useCallback(
(dimensions) => {
if (renderCompleted.current) {
resizeChart(dimensions);
}
}, 300),
[renderComplete, visData]
},
[resizeChart]
);

return (
<EuiResizeObserver onResize={updateChartSize}>
<EuiResizeObserver onResize={onContainerResize}>
{(resizeRef) => (
<div className="vgaVis__wrapper" ref={resizeRef}>
<div ref={chartDiv} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class VegaBaseView {
_addDestroyHandler(handler: Function): void;

destroy(): Promise<void>;
resize(dimensions?: { height: number; width: number }): Promise<void>;

_$container: any;
_$controls: any;
Expand Down
15 changes: 9 additions & 6 deletions src/plugins/vis_types/vega/public/vega_view/vega_base_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,19 @@ export class VegaBaseView {
}
}

resize() {
async resize(dimensions) {
if (this._parser.useResize && this._view) {
this.updateVegaSize(this._view);
return this._view.runAsync();
this.updateVegaSize(this._view, dimensions);
await this._view.runAsync();

// The derived class should create this method
this.onViewContainerResize?.();
}
}

updateVegaSize(view) {
const width = Math.floor(Math.max(0, this._$container.width()));
const height = Math.floor(Math.max(0, this._$container.height()));
updateVegaSize(view, dimensions) {
const width = Math.floor(Math.max(0, dimensions?.width ?? this._$container.width()));
const height = Math.floor(Math.max(0, dimensions?.height ?? this._$container.height()));

if (view.width() !== width || view.height() !== height) {
view.width(width).height(height);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ async function updateVegaView(mapBoxInstance: Map, vegaView: View) {
}

export class VegaMapView extends VegaBaseView {
private mapBoxInstance?: Map;

private get shouldShowZoomControl() {
return Boolean(this._parser.mapConfig.zoomControl);
}
Expand Down Expand Up @@ -139,6 +141,7 @@ export class VegaMapView extends VegaBaseView {
};

mapBoxInstance.once('load', initMapComponents);
this.mapBoxInstance = mapBoxInstance;
});
}

Expand Down Expand Up @@ -193,4 +196,8 @@ export class VegaMapView extends VegaBaseView {

await this.initMapContainer(vegaView);
}

protected async onViewContainerResize() {
this.mapBoxInstance?.resize();
}
}
1 change: 0 additions & 1 deletion src/plugins/vis_types/vega/public/vega_vis_renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const getVegaVisRenderer: (
handlers.onDestroy(() => {
unmountComponentAtNode(domNode);
});

render(
<KibanaThemeProvider theme$={deps.core.theme.theme$}>
<VisualizationContainer handlers={handlers}>
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/vis_types/vega/public/vega_visualization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { createVegaStateRestorer } from './lib/vega_state_restorer';

type VegaVisType = new (el: HTMLDivElement, fireEvent: IInterpreterRenderHandlers['event']) => {
render(visData: VegaParser): Promise<void>;
resize(dimensions?: { height: number; width: number }): Promise<void>;
destroy(): void;
};

Expand Down Expand Up @@ -97,6 +98,10 @@ export const createVegaVisualization = (
}
}

async resize(dimensions?: { height: number; width: number }) {
return this.vegaView?.resize(dimensions);
}

destroy() {
this.vegaStateRestorer.clear();
this.vegaView?.destroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const StyledEuiAccordion = styled(EuiAccordion)`
`;

interface Props {
updateAgentPolicy: (u: AgentPolicy | null) => void;
updateAgentPolicy: (u: AgentPolicy | null, errorMessage?: JSX.Element) => void;
isFleetServerPolicy?: boolean;
agentPolicyName: string;
}
Expand Down Expand Up @@ -84,12 +84,24 @@ export const AgentPolicyCreateInlineForm: React.FunctionComponent<Props> = ({
updateAgentPolicy(resp.data.item);
}
} catch (e) {
updateAgentPolicy(null);
updateAgentPolicy(null, mapError(e));
} finally {
setIsLoading(false);
}
}, [newAgentPolicy, withSysMonitoring, updateAgentPolicy]);

function mapError(e: { statusCode: number }): JSX.Element | undefined {
switch (e.statusCode) {
case 409:
return (
<FormattedMessage
id="xpack.fleet.agentPolicyCreation.errorMessage"
defaultMessage="An agent policy already exists with this name."
/>
);
}
}

return (
<EuiForm>
<EuiText>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,27 @@
import React from 'react';
import { EuiSpacer, EuiCallOut } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';

export enum CREATE_STATUS {
INITIAL = 'initial',
CREATED = 'created',
FAILED = 'failed',
}

export interface AgentPolicyCreateState {
status: CREATE_STATUS;
errorMessage?: JSX.Element;
}

interface Props {
createStatus: CREATE_STATUS;
createState: AgentPolicyCreateState;
}

export const AgentPolicyCreatedCallOut: React.FunctionComponent<Props> = ({ createStatus }) => {
export const AgentPolicyCreatedCallOut: React.FunctionComponent<Props> = ({ createState }) => {
return (
<>
<EuiSpacer size="m" />
{createStatus === CREATE_STATUS.CREATED ? (
{createState.status === CREATE_STATUS.CREATED ? (
<EuiCallOut
data-test-subj="agentPolicyCreateStatusCallOut"
title={
Expand All @@ -45,7 +51,9 @@ export const AgentPolicyCreatedCallOut: React.FunctionComponent<Props> = ({ crea
}
color="danger"
iconType="cross"
/>
>
{createState.errorMessage ?? null}
</EuiCallOut>
)}
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import React, { useState, useCallback, useEffect } from 'react';

import type { AgentPolicyCreateState } from '../../applications/fleet/sections/agents/components';
import {
AgentPolicyCreatedCallOut,
CREATE_STATUS,
Expand Down Expand Up @@ -40,7 +41,9 @@ export const SelectCreateAgentPolicy: React.FC<Props> = ({
}) => {
const [showCreatePolicy, setShowCreatePolicy] = useState(agentPolicies.length === 0);

const [createStatus, setCreateStatus] = useState(CREATE_STATUS.INITIAL);
const [createState, setCreateState] = useState<AgentPolicyCreateState>({
status: CREATE_STATUS.INITIAL,
});

const [newName, setNewName] = useState(incrementPolicyName(agentPolicies, isFleetServerPolicy));

Expand All @@ -52,13 +55,13 @@ export const SelectCreateAgentPolicy: React.FC<Props> = ({
}, [agentPolicies, isFleetServerPolicy]);

const onAgentPolicyCreated = useCallback(
async (policy: AgentPolicy | null) => {
async (policy: AgentPolicy | null, errorMessage?: JSX.Element) => {
if (!policy) {
setCreateStatus(CREATE_STATUS.FAILED);
setCreateState({ status: CREATE_STATUS.FAILED, errorMessage });
return;
}
setShowCreatePolicy(false);
setCreateStatus(CREATE_STATUS.CREATED);
setCreateState({ status: CREATE_STATUS.CREATED });
if (onAgentPolicyChange) {
onAgentPolicyChange(policy.id, policy!);
}
Expand Down Expand Up @@ -88,8 +91,8 @@ export const SelectCreateAgentPolicy: React.FC<Props> = ({
isFleetServerPolicy={isFleetServerPolicy}
/>
)}
{createStatus !== CREATE_STATUS.INITIAL && (
<AgentPolicyCreatedCallOut createStatus={createStatus} />
{createState.status !== CREATE_STATUS.INITIAL && (
<AgentPolicyCreatedCallOut createState={createState} />
)}
</>
);
Expand Down

0 comments on commit 115b90f

Please sign in to comment.