Skip to content

The open alternative to Salesforce, designed for AI.

617 B
Raw Blame
import { useEffect, useState } from 'react';

import { unmountFrontComponent, useFrontComponentId } from '..';

export type CommandProps = {
  execute: () => void | Promise<void>;
};

export const Command = ({ execute }: CommandProps) => {
  const [hasExecuted, setHasExecuted] = useState(false);

  const frontComponentId = useFrontComponentId();

  useEffect(() => {
    if (hasExecuted) {
      return;
    }

    setHasExecuted(true);

    const run = async () => {
      await execute();

      await unmountFrontComponent();
    };

    run();
  }, [execute, hasExecuted, frontComponentId]);

  return null;
};