From b3d521332d581c637f707b72f6292a0362cd31e5 Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]@users.noreply.github.com>
Date: Thu, 17 Oct 2024 03:19:36 +0000
Subject: [PATCH] feat: take index pattern and user input to t2viz from
 discover (#349)

* feat: take index pattern and user input to t2viz from discover

Signed-off-by: Yulong Ruan <ruanyl@amazon.com>

* import data set type INDEX_PATTERN from data plugin

Signed-off-by: Yulong Ruan <ruanyl@amazon.com>

* chore: update CHANGELOG

Signed-off-by: Yulong Ruan <ruanyl@amazon.com>

---------

Signed-off-by: Yulong Ruan <ruanyl@amazon.com>
(cherry picked from commit 98bf315d3109c3c2cab77ecf85e384e051586690)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

# Conflicts:
#	CHANGELOG.md
---
 public/components/visualization/text2viz.tsx | 13 +++++++---
 public/plugin.tsx                            | 27 +++++++++++++++++---
 2 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/public/components/visualization/text2viz.tsx b/public/components/visualization/text2viz.tsx
index 1efb507d..bdc4f8ab 100644
--- a/public/components/visualization/text2viz.tsx
+++ b/public/components/visualization/text2viz.tsx
@@ -22,7 +22,7 @@ import { v4 as uuidv4 } from 'uuid';
 
 import { useCallback } from 'react';
 import { useObservable } from 'react-use';
-import { useParams } from 'react-router-dom';
+import { useLocation, useParams } from 'react-router-dom';
 import { SourceSelector } from './source_selector';
 import type { IndexPattern } from '../../../../../src/plugins/data/public';
 import chatIcon from '../../assets/chat.svg';
@@ -52,9 +52,16 @@ import { HeaderVariant } from '../../../../../src/core/public';
 import { TEXT2VEGA_INPUT_SIZE_LIMIT } from '../../../common/constants/llm';
 import { FeedbackThumbs } from '../feedback_thumbs';
 
+export const INDEX_PATTERN_URL_SEARCH_KEY = 'indexPatternId';
+export const ASSISTANT_INPUT_URL_SEARCH_KEY = 'assistantInput';
+
 export const Text2Viz = () => {
   const { savedObjectId } = useParams<{ savedObjectId?: string }>();
-  const [selectedSource, setSelectedSource] = useState('');
+  const { search } = useLocation();
+  const searchParams = useMemo(() => new URLSearchParams(search), [search]);
+  const [selectedSource, setSelectedSource] = useState(
+    searchParams.get(INDEX_PATTERN_URL_SEARCH_KEY) ?? ''
+  );
   const [savedObjectLoading, setSavedObjectLoading] = useState(false);
   const [submitting, setSubmitting] = useState(false);
   const {
@@ -89,7 +96,7 @@ export const Text2Viz = () => {
 
   const useUpdatedUX = uiSettings.get('home:useNewHomePage');
 
-  const [input, setInput] = useState('');
+  const [input, setInput] = useState(searchParams.get(ASSISTANT_INPUT_URL_SEARCH_KEY) ?? '');
   const [editorInput, setEditorInput] = useState('');
   const text2vegaRef = useRef(new Text2Vega(http, data.search, savedObjects));
 
diff --git a/public/plugin.tsx b/public/plugin.tsx
index eac6bafc..954a4704 100644
--- a/public/plugin.tsx
+++ b/public/plugin.tsx
@@ -66,6 +66,11 @@ import {
 } from './vis_nlq/saved_object_loader';
 import { NLQVisualizationEmbeddableFactory } from './components/visualization/embeddable/nlq_vis_embeddable_factory';
 import { NLQ_VISUALIZATION_EMBEDDABLE_TYPE } from './components/visualization/embeddable/nlq_vis_embeddable';
+import {
+  ASSISTANT_INPUT_URL_SEARCH_KEY,
+  INDEX_PATTERN_URL_SEARCH_KEY,
+} from './components/visualization/text2viz';
+import { DEFAULT_DATA } from '../../../src/plugins/data/common';
 
 export const [getCoreStart, setCoreStart] = createGetterSetter<CoreStart>('CoreStart');
 
@@ -337,7 +342,7 @@ export class AssistantPlugin
         // T2Viz is only compatible with data sources that have certain agents configured
         isCompatible: async (context) => {
           // t2viz only supports selecting index pattern at the moment
-          if (context.datasetType === 'INDEX_PATTERN' && context.datasetId) {
+          if (context.datasetType === DEFAULT_DATA.SET_TYPES.INDEX_PATTERN && context.datasetId) {
             const res = await assistantServiceStart.client.agentConfigExists(
               [
                 TEXT2VEGA_RULE_BASED_AGENT_CONFIG_ID,
@@ -352,8 +357,24 @@ export class AssistantPlugin
           }
           return false;
         },
-        execute: async () => {
-          core.application.navigateToApp(TEXT2VIZ_APP_ID);
+        execute: async (context) => {
+          const url = new URL(core.application.getUrlForApp(TEXT2VIZ_APP_ID, { absolute: true }));
+          if (context.datasetId && context.datasetType === DEFAULT_DATA.SET_TYPES.INDEX_PATTERN) {
+            url.searchParams.set(INDEX_PATTERN_URL_SEARCH_KEY, context.datasetId);
+          }
+          /**
+           * TODO: the current implementation of getting query assistant input needs to be refactored
+           * once query assistant is moved to dashboard-assistant repo. Currently, there is no better
+           * way to get the input value as the query assistant is currently implemented in OSD core.
+           */
+          const queryAssistInputEle = document.getElementsByClassName('queryAssist__input')[0];
+          if (queryAssistInputEle instanceof HTMLInputElement) {
+            const input = queryAssistInputEle.value;
+            if (input) {
+              url.searchParams.set(ASSISTANT_INPUT_URL_SEARCH_KEY, input);
+            }
+          }
+          core.application.navigateToUrl(url.toString());
         },
       });
       const savedVisNLQLoader = createVisNLQSavedObjectLoader({