import React, { useEffect } from 'react';
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { CLEAR_EDITOR_COMMAND } from 'lexical';
import { ClearEditorPlugin } from '@lexical/react/LexicalClearEditorPlugin';

function ClearTriggerPlugin({ clearTrigger }: { clearTrigger: number }) {
  const [editor] = useLexicalComposerContext();

  useEffect(() => {
    if (clearTrigger > 0) {
      editor.dispatchCommand(CLEAR_EDITOR_COMMAND, undefined);
    }
  }, [editor, clearTrigger]);

  return <ClearEditorPlugin />;
}

export default ClearTriggerPlugin;
