refactor: ai menu (#5400)

This commit is contained in:
Aaryan Khandelwal 2024-08-21 16:19:28 +05:30 committed by GitHub
parent c100c0bd85
commit 881d0525cc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 25 additions and 8 deletions

View file

@ -87,6 +87,7 @@ export const AIFeaturesMenu: React.FC<Props> = (props) => {
> >
<div ref={menuRef} className="z-10"> <div ref={menuRef} className="z-10">
{menu?.({ {menu?.({
isOpen: isPopupVisible,
onClose: hidePopup, onClose: hidePopup,
})} })}
</div> </div>

View file

@ -1,4 +1,4 @@
export * from "./bubble-menu";
export * from "./ai-menu"; export * from "./ai-menu";
export * from "./bubble-menu";
export * from "./block-menu"; export * from "./block-menu";
export * from "./menu-items"; export * from "./menu-items";

View file

@ -182,7 +182,7 @@ const SideMenu = (options: SideMenuPluginProps) => {
aiHandleDOMEvents?.mousemove?.(); aiHandleDOMEvents?.mousemove?.();
} }
}, },
keydown: () => hideSideMenu(), // keydown: () => hideSideMenu(),
mousewheel: () => hideSideMenu(), mousewheel: () => hideSideMenu(),
dragenter: (view) => { dragenter: (view) => {
if (handlesConfig.dragDrop) { if (handlesConfig.dragDrop) {

View file

@ -1,7 +1,8 @@
type TMenuProps = { export type TAIMenuProps = {
isOpen: boolean;
onClose: () => void; onClose: () => void;
}; };
export type TAIHandler = { export type TAIHandler = {
menu?: (props: TMenuProps) => React.ReactNode; menu?: (props: TAIMenuProps) => React.ReactNode;
}; };

View file

@ -1,6 +1,6 @@
"use client"; "use client";
import React, { RefObject, useRef, useState } from "react"; import React, { RefObject, useEffect, useRef, useState } from "react";
import { useParams } from "next/navigation"; import { useParams } from "next/navigation";
import { ChevronRight, CornerDownRight, LucideIcon, RefreshCcw, Sparkles, TriangleAlert } from "lucide-react"; import { ChevronRight, CornerDownRight, LucideIcon, RefreshCcw, Sparkles, TriangleAlert } from "lucide-react";
// plane editor // plane editor
@ -20,6 +20,7 @@ const aiService = new AIService();
type Props = { type Props = {
editorRef: RefObject<EditorRefApi>; editorRef: RefObject<EditorRefApi>;
isOpen: boolean;
onClose: () => void; onClose: () => void;
}; };
@ -57,7 +58,7 @@ const TONES_LIST = [
]; ];
export const EditorAIMenu: React.FC<Props> = (props) => { export const EditorAIMenu: React.FC<Props> = (props) => {
const { editorRef, onClose } = props; const { editorRef, isOpen, onClose } = props;
// states // states
const [activeTask, setActiveTask] = useState<AI_EDITOR_TASKS | null>(null); const [activeTask, setActiveTask] = useState<AI_EDITOR_TASKS | null>(null);
const [response, setResponse] = useState<string | undefined>(undefined); const [response, setResponse] = useState<string | undefined>(undefined);
@ -126,6 +127,14 @@ export const EditorAIMenu: React.FC<Props> = (props) => {
onClose(); onClose();
}; };
// reset on close
useEffect(() => {
if (!isOpen) {
setActiveTask(null);
setResponse(undefined);
}
}, [isOpen]);
return ( return (
<div <div
className={cn( className={cn(

View file

@ -1,4 +1,4 @@
import { useEffect } from "react"; import { useCallback, useEffect } from "react";
import { observer } from "mobx-react"; import { observer } from "mobx-react";
import { useParams } from "next/navigation"; import { useParams } from "next/navigation";
// document-editor // document-editor
@ -8,6 +8,7 @@ import {
EditorReadOnlyRefApi, EditorReadOnlyRefApi,
EditorRefApi, EditorRefApi,
IMarking, IMarking,
TAIMenuProps,
TDisplayConfig, TDisplayConfig,
} from "@plane/editor"; } from "@plane/editor";
// types // types
@ -95,6 +96,11 @@ export const PageEditorBody: React.FC<Props> = observer((props) => {
fontStyle, fontStyle,
}; };
const getAIMenu = useCallback(
({ isOpen, onClose }: TAIMenuProps) => <EditorAIMenu editorRef={editorRef} isOpen={isOpen} onClose={onClose} />,
[editorRef]
);
useEffect(() => { useEffect(() => {
updateMarkings(pageDescription ?? "<p></p>"); updateMarkings(pageDescription ?? "<p></p>");
}, [pageDescription, updateMarkings]); }, [pageDescription, updateMarkings]);
@ -158,7 +164,7 @@ export const PageEditorBody: React.FC<Props> = observer((props) => {
}} }}
disabledExtensions={documentEditor} disabledExtensions={documentEditor}
aiHandler={{ aiHandler={{
menu: ({ onClose }) => <EditorAIMenu editorRef={editorRef} onClose={onClose} />, menu: getAIMenu,
}} }}
/> />
) : ( ) : (