/* * 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, { useEffect, useState } from 'react'; import { useHistory } from 'react-router-dom'; import { FormattedMessage } from '@kbn/i18n-react'; import { i18n } from '@kbn/i18n'; import { CoreStart } from '@kbn/core/public'; import { EuiButton, EuiFieldText, EuiFlexGroup, EuiFlexItem, EuiFormRow, EuiHorizontalRule, EuiPageSection, EuiPageHeader, EuiSelect, EuiSpacer, EuiText, EuiTitle, EuiSelectOption, EuiFlexGrid, } from '@elastic/eui'; import type { GuideState, GuideStepIds, GuideId, GuideStep } from '@kbn/guided-onboarding'; import type { GuidedOnboardingPluginStart } from '@kbn/guided-onboarding-plugin/public'; interface MainProps { guidedOnboarding: GuidedOnboardingPluginStart; notifications: CoreStart['notifications']; } const exampleGuideIds: GuideId[] = [ 'appSearch', 'websiteSearch', 'databaseSearch', 'siem', 'kubernetes', 'testGuide', ]; const selectOptions: EuiSelectOption[] = exampleGuideIds.map((guideId) => ({ value: guideId, text: guideId, })); export const Main = (props: MainProps) => { const { guidedOnboarding: { guidedOnboardingApi }, notifications, } = props; const history = useHistory(); const [guidesState, setGuidesState] = useState(undefined); const [activeGuide, setActiveGuide] = useState(undefined); const [selectedGuide, setSelectedGuide] = useState('kubernetes'); const [selectedStep, setSelectedStep] = useState(undefined); useEffect(() => { const fetchGuidesState = async () => { const newGuidesState = await guidedOnboardingApi?.fetchAllGuidesState(); setGuidesState(newGuidesState ? newGuidesState.state : []); }; fetchGuidesState(); }, [guidedOnboardingApi]); useEffect(() => { const newActiveGuide = guidesState?.find((guide) => guide.isActive === true); if (newActiveGuide) { setActiveGuide(newActiveGuide); } }, [guidesState, setActiveGuide]); const activateGuide = async (guideId: GuideId, guideState?: GuideState) => { const response = await guidedOnboardingApi?.activateGuide(guideId, guideState); if (response) { notifications.toasts.addSuccess( i18n.translate('guidedOnboardingExample.startGuide.toastLabel', { defaultMessage: 'Guide (re-)started', }) ); } }; const updateGuideState = async () => { if (!selectedGuide) { return; } const selectedGuideConfig = await guidedOnboardingApi?.getGuideConfig(selectedGuide); if (!selectedGuideConfig) { return; } const selectedStepIndex = selectedGuideConfig.steps.findIndex( (step) => step.id === selectedStep! ); // Noop if the selected step is invalid if (selectedStepIndex === -1) { return; } const updatedSteps: GuideStep[] = selectedGuideConfig.steps.map((step, stepIndex) => { if (selectedStepIndex > stepIndex) { return { id: step.id, status: 'complete', }; } if (selectedStepIndex < stepIndex) { return { id: step.id, status: 'inactive', }; } return { id: step.id, status: 'active', }; }); const updatedGuideState: GuideState = { isActive: true, status: 'in_progress', steps: updatedSteps, guideId: selectedGuide!, }; const response = await guidedOnboardingApi?.updatePluginState( { status: 'in_progress', guide: updatedGuideState }, true ); if (response) { notifications.toasts.addSuccess( i18n.translate('guidedOnboardingExample.updateGuideState.toastLabel', { defaultMessage: 'Guide state updated', }) ); } }; return ( <>

{activeGuide ? (
{activeGuide.guideId}
{activeGuide.steps.map((step) => { return ( <> {`Step "${step.id}": ${step.status}`}
); })}
) : (

)}

{exampleGuideIds.map((guideId) => { const guideState = guidesState?.find((guide) => guide.guideId === guideId); return ( activateGuide(guideId, guideState)} fill disabled={guideState?.status === 'complete'} > {guideState === undefined && ( )} {(guideState?.isActive === true || guideState?.status === 'in_progress' || guideState?.status === 'ready_to_complete' || guideState?.status === 'not_started') && ( )} {guideState?.status === 'complete' && ( )} ); })}

{ const value = e.target.value as GuideId; const shouldResetState = value.trim().length === 0; if (shouldResetState) { setSelectedGuide(undefined); setSelectedStep(undefined); } else { setSelectedGuide(value); } }} /> setSelectedStep(e.target.value as GuideStepIds)} /> Save

history.push('stepOne')}> history.push('stepTwo')}> history.push('stepThree')}> history.push('stepFour')}>
); };