-
Notifications
You must be signed in to change notification settings - Fork 884
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
[D&D] Enable basic embeddable panels #1911
Merged
ashwin-pc
merged 1 commit into
opensearch-project:feature/d-and-d
from
joshuarrrr:feature/d-and-d-basic-embeddable
Jul 21, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export * from './wizard_embeddable'; | ||
export * from './wizard_embeddable_factory'; |
133 changes: 133 additions & 0 deletions
133
src/plugins/wizard/public/embeddable/wizard_component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React, { useEffect, useState } from 'react'; | ||
import { | ||
EuiFlexItem, | ||
EuiFlexGroup, | ||
EuiText, | ||
EuiAvatar, | ||
EuiFlexGrid, | ||
EuiCodeBlock, | ||
} from '@elastic/eui'; | ||
|
||
import { withEmbeddableSubscription } from '../../../embeddable/public'; | ||
import { WizardEmbeddable, WizardInput, WizardOutput } from './wizard_embeddable'; | ||
import { validateSchemaState } from '../application/utils/validate_schema_state'; | ||
|
||
interface Props { | ||
embeddable: WizardEmbeddable; | ||
input: WizardInput; | ||
output: WizardOutput; | ||
} | ||
|
||
function wrapSearchTerms(task?: string, search?: string) { | ||
if (!search) return task; | ||
if (!task) return task; | ||
const parts = task.split(new RegExp(`(${search})`, 'g')); | ||
return parts.map((part, i) => | ||
part === search ? ( | ||
<span key={i} style={{ backgroundColor: 'yellow' }}> | ||
{part} | ||
</span> | ||
) : ( | ||
part | ||
) | ||
); | ||
} | ||
|
||
function WizardEmbeddableComponentInner({ | ||
embeddable, | ||
input: { search }, | ||
output: { savedAttributes }, | ||
}: Props) { | ||
const { ReactExpressionRenderer, toasts, types, indexPatterns, aggs } = embeddable; | ||
const [expression, setExpression] = useState<string>(); | ||
const { title, description, visualizationState, styleState } = savedAttributes || {}; | ||
|
||
useEffect(() => { | ||
const { visualizationState: visualization, styleState: style } = savedAttributes || {}; | ||
if (savedAttributes === undefined || visualization === undefined || style === undefined) { | ||
return; | ||
} | ||
|
||
const rootState = { | ||
visualization: JSON.parse(visualization), | ||
style: JSON.parse(style), | ||
}; | ||
|
||
const visualizationType = types.get(rootState.visualization?.activeVisualization?.name ?? ''); | ||
if (!visualizationType) { | ||
throw new Error(`Invalid visualization type ${visualizationType}`); | ||
} | ||
const { toExpression, ui } = visualizationType; | ||
|
||
async function loadExpression() { | ||
const schemas = ui.containerConfig.data.schemas; | ||
const [valid, errorMsg] = validateSchemaState(schemas, rootState); | ||
|
||
if (!valid) { | ||
if (errorMsg) { | ||
toasts.addWarning(errorMsg); | ||
} | ||
setExpression(undefined); | ||
return; | ||
} | ||
const exp = await toExpression(rootState, indexPatterns, aggs); | ||
setExpression(exp); | ||
} | ||
|
||
if (savedAttributes !== undefined) { | ||
loadExpression(); | ||
} | ||
}, [aggs, indexPatterns, savedAttributes, toasts, types]); | ||
|
||
// TODO: add correct loading and error states, remove debugging mode | ||
return ( | ||
<> | ||
{expression ? ( | ||
<EuiFlexItem> | ||
<ReactExpressionRenderer expression={expression} /> | ||
</EuiFlexItem> | ||
) : ( | ||
<EuiFlexGroup> | ||
<EuiFlexItem grow={false}> | ||
<EuiAvatar name={title || description || ''} size="l" /> | ||
</EuiFlexItem> | ||
<EuiFlexItem> | ||
<EuiFlexGrid columns={1}> | ||
<EuiFlexItem> | ||
<EuiText data-test-subj="wizardEmbeddableTitle"> | ||
<h3>{wrapSearchTerms(title || '', search)}</h3> | ||
</EuiText> | ||
</EuiFlexItem> | ||
<EuiFlexItem> | ||
<EuiText data-test-subj="wizardEmbeddableDescription"> | ||
{wrapSearchTerms(description, search)} | ||
</EuiText> | ||
</EuiFlexItem> | ||
<EuiFlexItem> | ||
<EuiCodeBlock data-test-subj="wizardEmbeddableDescription"> | ||
{wrapSearchTerms(visualizationState, search)} | ||
</EuiCodeBlock> | ||
</EuiFlexItem> | ||
<EuiFlexItem> | ||
<EuiCodeBlock data-test-subj="wizardEmbeddableDescription"> | ||
{wrapSearchTerms(styleState, search)} | ||
</EuiCodeBlock> | ||
</EuiFlexItem> | ||
</EuiFlexGrid> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
export const WizardEmbeddableComponent = withEmbeddableSubscription< | ||
WizardInput, | ||
WizardOutput, | ||
WizardEmbeddable | ||
>(WizardEmbeddableComponentInner); |
160 changes: 160 additions & 0 deletions
160
src/plugins/wizard/public/embeddable/wizard_embeddable.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { Subscription } from 'rxjs'; | ||
|
||
import { WizardSavedObjectAttributes } from '../../common'; | ||
import { | ||
Embeddable, | ||
EmbeddableOutput, | ||
IContainer, | ||
SavedObjectEmbeddableInput, | ||
} from '../../../embeddable/public'; | ||
import { IToasts, SavedObjectsClientContract } from '../../../../core/public'; | ||
import { WizardEmbeddableComponent } from './wizard_component'; | ||
import { ReactExpressionRendererType } from '../../../expressions/public'; | ||
import { TypeServiceStart } from '../services/type_service'; | ||
import { DataPublicPluginStart } from '../../../data/public'; | ||
|
||
export const WIZARD_EMBEDDABLE = 'WIZARD_EMBEDDABLE'; | ||
|
||
// TODO: remove search, hasMatch or update as appropriate | ||
joshuarrrr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
export interface WizardInput extends SavedObjectEmbeddableInput { | ||
/** | ||
* Optional search string which will be used to highlight search terms as | ||
* well as calculate `output.hasMatch`. | ||
*/ | ||
search?: string; | ||
} | ||
|
||
export interface WizardOutput extends EmbeddableOutput { | ||
/** | ||
* Should be true if input.search is defined and the task or title contain | ||
* search as a substring. | ||
*/ | ||
hasMatch: boolean; | ||
/** | ||
* Will contain the saved object attributes of the Wizard Saved Object that matches | ||
* `input.savedObjectId`. If the id is invalid, this may be undefined. | ||
*/ | ||
savedAttributes?: WizardSavedObjectAttributes; | ||
} | ||
|
||
/** | ||
* Returns whether any attributes contain the search string. If search is empty, true is returned. If | ||
* there are no savedAttributes, false is returned. | ||
* @param search - the search string | ||
* @param savedAttributes - the saved object attributes for the saved object with id `input.savedObjectId` | ||
*/ | ||
function getHasMatch(search?: string, savedAttributes?: WizardSavedObjectAttributes): boolean { | ||
if (!search) return true; | ||
if (!savedAttributes) return false; | ||
return Boolean( | ||
(savedAttributes.description && savedAttributes.description.match(search)) || | ||
(savedAttributes.title && savedAttributes.title.match(search)) | ||
); | ||
} | ||
|
||
export class WizardEmbeddable extends Embeddable<WizardInput, WizardOutput> { | ||
public readonly type = WIZARD_EMBEDDABLE; | ||
private subscription: Subscription; | ||
private node?: HTMLElement; | ||
private savedObjectsClient: SavedObjectsClientContract; | ||
public ReactExpressionRenderer: ReactExpressionRendererType; | ||
public toasts: IToasts; | ||
public types: TypeServiceStart; | ||
public indexPatterns: DataPublicPluginStart['indexPatterns']; | ||
public aggs: DataPublicPluginStart['search']['aggs']; | ||
private savedObjectId?: string; | ||
|
||
constructor( | ||
initialInput: WizardInput, | ||
{ | ||
parent, | ||
savedObjectsClient, | ||
data, | ||
ReactExpressionRenderer, | ||
toasts, | ||
types, | ||
}: { | ||
parent?: IContainer; | ||
data: DataPublicPluginStart; | ||
savedObjectsClient: SavedObjectsClientContract; | ||
ReactExpressionRenderer: ReactExpressionRendererType; | ||
toasts: IToasts; | ||
types: TypeServiceStart; | ||
} | ||
) { | ||
// TODO: can default title come from saved object? | ||
super(initialInput, { defaultTitle: 'wizard', hasMatch: false }, parent); | ||
this.savedObjectsClient = savedObjectsClient; | ||
this.ReactExpressionRenderer = ReactExpressionRenderer; | ||
this.toasts = toasts; | ||
this.types = types; | ||
this.indexPatterns = data.indexPatterns; | ||
this.aggs = data.search.aggs; | ||
|
||
this.subscription = this.getInput$().subscribe(async () => { | ||
// There is a little more work today for this embeddable because it has | ||
// more output it needs to update in response to input state changes. | ||
let savedAttributes: WizardSavedObjectAttributes | undefined; | ||
|
||
// Since this is an expensive task, we save a local copy of the previous | ||
// savedObjectId locally and only retrieve the new saved object if the id | ||
// actually changed. | ||
if (this.savedObjectId !== this.input.savedObjectId) { | ||
this.savedObjectId = this.input.savedObjectId; | ||
const wizardSavedObject = await this.savedObjectsClient.get<WizardSavedObjectAttributes>( | ||
'wizard', | ||
this.input.savedObjectId | ||
); | ||
savedAttributes = wizardSavedObject?.attributes; | ||
} | ||
|
||
// The search string might have changed as well so we need to make sure we recalculate | ||
// hasMatch. | ||
this.updateOutput({ | ||
hasMatch: getHasMatch(this.input.search, savedAttributes), | ||
savedAttributes, | ||
title: savedAttributes?.title, | ||
}); | ||
}); | ||
} | ||
|
||
public render(node: HTMLElement) { | ||
if (this.node) { | ||
ReactDOM.unmountComponentAtNode(this.node); | ||
} | ||
this.node = node; | ||
ReactDOM.render(<WizardEmbeddableComponent embeddable={this} />, node); | ||
} | ||
|
||
/** | ||
* Lets re-sync our saved object to make sure it's up to date! | ||
*/ | ||
public async reload() { | ||
this.savedObjectId = this.input.savedObjectId; | ||
const wizardSavedObject = await this.savedObjectsClient.get<WizardSavedObjectAttributes>( | ||
'wizard', | ||
this.input.savedObjectId | ||
); | ||
const savedAttributes = wizardSavedObject?.attributes; | ||
this.updateOutput({ | ||
hasMatch: getHasMatch(this.input.search, savedAttributes), | ||
savedAttributes, | ||
title: wizardSavedObject?.attributes?.title, | ||
}); | ||
} | ||
|
||
public destroy() { | ||
super.destroy(); | ||
this.subscription.unsubscribe(); | ||
if (this.node) { | ||
ReactDOM.unmountComponentAtNode(this.node); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A lot of logic here seems to overlap with the existing D&D workspace code. Can we consolidate them instead of maintaining it in two places?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's by design for now - eventually we'll consolidate, but first we need to figure out which are the shared parts and which parts diverge.