sadpandajoe commented on code in PR #43133:
URL: https://github.com/apache/superset/pull/43133#discussion_r3995802030


##########
superset-frontend/src/features/ai/hooks/useAIAction.ts:
##########
@@ -0,0 +1,102 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * @fileoverview How the rest of the app asks the assistant something.
+ *
+ * A "Debug this query" button in SQL Lab, or a chart menu item, needs to open 
the
+ * assistant with a question already asked. It does that by dispatching one DOM
+ * event that the panel listens for.
+ *
+ * A DOM event rather than a shared module or a global: the caller may be 
mounted
+ * in a different React tree from the panel (the panel is mounted by the chat 
host)
+ * and must not import it, or the assistant's code would be pulled into every
+ * bundle that offers an action. The panel is the only listener, and if it is 
not
+ * mounted the event is simply unheard.
+ */
+
+import { useCallback } from 'react';
+import { chat } from 'src/core/chat';
+import type { AIActionPayload } from '../types';
+
+/** The event the panel listens for. */
+export const AI_ACTION_EVENT = 'superset-ai-action';
+
+export type AiActionEvent = CustomEvent<AIActionPayload>;
+
+/**
+ * Asks the assistant a question in a new conversation.
+ *
+ * Safe to call whether or not the panel is mounted: the chat host is asked to 
open
+ * first, which mounts it, and the event is dispatched after so a panel that 
has
+ * just mounted has its listener attached.
+ */
+export const triggerAIAction = (payload: AIActionPayload): void => {
+  if (!payload.prompt.trim()) {
+    return;
+  }
+  chat.open();
+  window.dispatchEvent(
+    new CustomEvent<AIActionPayload>(AI_ACTION_EVENT, { detail: payload }),
+  );
+};

Review Comment:
   Agreed—the synchronous event can fire before React mounts the panel 
listener, so an action invoked while the assistant is closed opens an empty 
panel and loses its prompt. Could the payload be queued until the panel 
acknowledges it instead of being dispatched immediately after `chat.open()`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to