Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lens] Add the bee #136132

Merged
merged 6 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import React, { useMemo, memo, useContext, useState, useEffect } from 'react';
import { i18n } from '@kbn/i18n';
import { EuiPopover, EuiButtonIcon, EuiContextMenuPanel, EuiContextMenuItem } from '@elastic/eui';
import { UiActionsStart } from '@kbn/ui-actions-plugin/public';
import { Easteregg } from './easteregg';
import { NativeRenderer } from '../../native_renderer';
import { DragContext, DragDropIdentifier } from '../../drag_drop';
import { StateSetter, DatasourceDataPanelProps, DatasourceMap } from '../../types';
Expand Down Expand Up @@ -97,6 +98,7 @@ export const DataPanelWrapper = memo((props: DataPanelWrapperProps) => {

return (
<>
<Easteregg query={externalContext?.query} />
{Object.keys(props.datasourceMap).length > 1 && (
<EuiPopover
id="datasource-switch"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React, { useState, useEffect } from 'react';
import beeImage from './elastic_elk_b.png';

function Bee() {
const [position, setPosition] = useState({
top: 50 + Math.random() * 100,
left: 50 + Math.random() * 50,
vX: 3 + Math.random(),
vY: 0.5 + Math.random() * 0.5,
});

useEffect(() => {
let handle: number = 0;
function tick() {
handle = requestAnimationFrame(() => {
setPosition(({ top, left, vX, vY }) => {
let newVx = vX + Math.random() * 0.5 - 0.25;
let newVy = vY + Math.random() - 0.5;
if (top < 50) {
newVy = Math.abs(newVy);
}
if (top > window.innerHeight - 300) {
newVy = -1 * Math.abs(newVy);
}
if (left < 50) {
newVx = Math.abs(newVx);
}
if (left > window.innerWidth - 300) {
newVx = -1 * Math.abs(newVx);
}

return {
top: top + vY,
left: left + vX,
vX: newVx,
vY: newVy,
};
});
tick();
});
}
tick();
return () => {
cancelAnimationFrame(handle);
};
}, []);

return (
<img
src={beeImage}
alt="ELK-Bee"
title="Bzzzzzzz"
style={{
position: 'absolute',
width: '80px',
height: 'auto',
transform: position.vX > 0 ? 'scale(-1, 1)' : undefined,
top: position.top,
left: position.left,
zIndex: 999,
}}
/>
);
}

// eslint-disable-next-line import/no-default-export
export default Bee;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React from 'react';
import type { Query } from '@kbn/es-query';
import { EuiErrorBoundary } from '@elastic/eui';

const Bee = React.lazy(() => import('./bee'));

const ELK_BEE_REGEX = /^What\'s (an|(\d+)) elk bees?\?$/;

function Bees({ query }: { query?: Query }) {
if (!query || typeof query !== 'object' || typeof query.query !== 'string') {
return null;
}
const match = ELK_BEE_REGEX.exec(query.query);
if (!match) {
return null;
}
let amount = parseInt(match[2] || '1', 10);
if (isNaN(amount)) {
amount = 1;
flash1293 marked this conversation as resolved.
Show resolved Hide resolved
}
amount = Math.max(1, Math.min(50, amount));
return (
<React.Suspense fallback={false}>
{new Array(amount).fill(undefined).map((v, i) => (
<Bee key={i} />
))}
</React.Suspense>
);
}

export function Easteregg(props: { query?: Query }) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loads the code (even though Lazy), even when the easter-egg query isn't triggered. Consider only loading the easter-egg when the query actually matches.

So I'd break here and return null here on the REGEX-check. And only load the Bee lazily after.

Copy link
Contributor Author

@flash1293 flash1293 Jul 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what the code does - the Bees component does nothing but checking the query in a safe way. Only if the amount is larger than one and a bee will be shown, a Bee component is rendered which will lazy-load the bee code (including the image). The Easteregg component is providing the error boundary, so an uncaught exception in the bee/bees component won't be visible in the app.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oki thx, you're right, I have misunderstood then

return (
// Do not break Lens for an easteregg
<EuiErrorBoundary style={{ display: 'none' }}>
<Bees {...props} />
</EuiErrorBoundary>
);
}