import React, { useEffect, useState, useCallback } from "react"; import { Globe2, Lock, LucideIcon } from "lucide-react"; // editor import { EditorMenuItemNames } from "@plane/document-editor"; import { EditorRefApi } from "@plane/lite-text-editor"; // ui import { Button, Tooltip } from "@plane/ui"; // constants import { TOOLBAR_ITEMS } from "@/constants/editor"; import { EIssueCommentAccessSpecifier } from "@/constants/issue"; // helpers import { cn } from "@/helpers/common.helper"; type Props = { accessSpecifier?: EIssueCommentAccessSpecifier; executeCommand: (commandName: EditorMenuItemNames) => void; handleAccessChange?: (accessKey: EIssueCommentAccessSpecifier) => void; handleSubmit: (event: React.MouseEvent) => void; isCommentEmpty: boolean; isSubmitting: boolean; showAccessSpecifier: boolean; showSubmitButton: boolean; editorRef: React.MutableRefObject | null; }; type TCommentAccessType = { icon: LucideIcon; key: EIssueCommentAccessSpecifier; label: "Private" | "Public"; }; const COMMENT_ACCESS_SPECIFIERS: TCommentAccessType[] = [ { icon: Lock, key: EIssueCommentAccessSpecifier.INTERNAL, label: "Private", }, { icon: Globe2, key: EIssueCommentAccessSpecifier.EXTERNAL, label: "Public", }, ]; const toolbarItems = TOOLBAR_ITEMS.lite; export const IssueCommentToolbar: React.FC = (props) => { const { accessSpecifier, executeCommand, handleAccessChange, handleSubmit, isCommentEmpty, isSubmitting, showAccessSpecifier, showSubmitButton, editorRef, } = props; // State to manage active states of toolbar items const [activeStates, setActiveStates] = useState>({}); // Function to update active states const updateActiveStates = useCallback(() => { if (editorRef?.current) { const newActiveStates: Record = {}; Object.values(toolbarItems) .flat() .forEach((item) => { // Assert that editorRef.current is not null newActiveStates[item.key] = (editorRef.current as EditorRefApi).isMenuItemActive(item.key); }); setActiveStates(newActiveStates); } }, [editorRef]); // useEffect to call updateActiveStates when isActive prop changes useEffect(() => { if (!editorRef?.current) return; const unsubscribe = editorRef.current.onStateChange(updateActiveStates); updateActiveStates(); return () => unsubscribe(); }, [editorRef, updateActiveStates]); return (
{showAccessSpecifier && (
{COMMENT_ACCESS_SPECIFIERS.map((access) => { const isAccessActive = accessSpecifier === access.key; return ( ); })}
)}
{Object.keys(toolbarItems).map((key, index) => (
{toolbarItems[key].map((item) => ( {item.name} {item.shortcut && {item.shortcut.join(" + ")}}

} >
))}
))}
{showSubmitButton && (
)}
); };