Skip to content

πŸ™Œ OpenHands: AI-Driven Development

6.6 KB
Raw Blame
import React from "react";
import { useTranslation } from "react-i18next";
import { ChatSendButton } from "#/components/features/chat/chat-send-button";
import { RecommendedAutomationsLauncher } from "#/components/features/automations/recommended-automations-launcher";
import { BrandButton } from "#/components/features/settings/brand-button";
import { useNavigation } from "#/context/navigation-context";
import { useCreateConversation } from "#/hooks/mutation/use-create-conversation";
import { useIsCreatingConversation } from "#/hooks/use-is-creating-conversation";
import { I18nKey } from "#/i18n/declaration";

interface SayHelloStepProps {
  onBack: () => void;
  /** Dismisses onboarding without launching a conversation. */
  onClose: () => void;
  /** Called once the conversation has been created β€” used by the parent
   * modal to mark the onboarding as complete before unmounting. */
  onLaunched: () => void;
}

/**
 * Step 3: a simple text input pre-filled with "hello OpenHands!" that
 * launches a brand-new conversation with no workspace and navigates
 * to it. Completing this step finishes the onboarding flow.
 */
export function SayHelloStep({
  onBack,
  onClose,
  onLaunched,
}: SayHelloStepProps) {
  const { t } = useTranslation("openhands");
  const { navigate } = useNavigation();
  const defaultMessage = t(I18nKey.ONBOARDING$HELLO_DEFAULT_MESSAGE);
  const [message, setMessage] = React.useState(defaultMessage);

  const {
    mutate: createConversation,
    isPending,
    isSuccess,
  } = useCreateConversation();
  const isCreatingElsewhere = useIsCreatingConversation();
  const [launchStatus, setLaunchStatus] = React.useState<
    "idle" | "pending" | "error"
  >("idle");
  const isPendingLaunch =
    launchStatus === "pending" || isPending || isCreatingElsewhere;
  const isLaunching = isPendingLaunch || isSuccess;
  const launchInFlightRef = React.useRef(false);

  const canSubmit =
    message.trim().length > 0 && !isLaunching && !launchInFlightRef.current;

  const launchConversation = () => {
    if (!canSubmit || launchInFlightRef.current) return;
    launchInFlightRef.current = true;
    setLaunchStatus("pending");

    // Explicitly omit `repository` and `workingDir` so the
    // conversation starts with no workspace, per the spec.
    createConversation(
      { query: message.trim(), entryPoint: "onboarding_say_hello" },
      {
        onSuccess: (data) => {
          navigate(`/conversations/${data.conversation_id}`);
          onLaunched();
        },
        onError: () => {
          launchInFlightRef.current = false;
          setLaunchStatus("error");
        },
      },
    );
  };

  const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
    event.preventDefault();
    launchConversation();
  };

  return (
    <div
      data-testid="onboarding-step-say-hello"
      className="flex max-h-[calc(90vh-7rem)] flex-col"
    >
      <header className="flex shrink-0 flex-col gap-2">
        <h2 className="text-2xl font-medium text-contrast">
          {t(I18nKey.ONBOARDING$HELLO_TITLE)}
        </h2>
        <p className="text-sm text-muted">
          {t(I18nKey.ONBOARDING$HELLO_SUBTITLE)}
        </p>
      </header>

      <form
        onSubmit={handleSubmit}
        data-testid="onboarding-hello-input-form"
        className="mt-6 box-border flex w-full shrink-0 flex-col items-start justify-center rounded-[15px] border border-border bg-surface-raised p-4"
      >
        <div className="relative w-full">
          <div className="box-border flex w-full shrink-0 flex-row items-end justify-between gap-2 p-0 pb-4.5">
            <input
              data-testid="onboarding-hello-input"
              aria-label={t(I18nKey.ONBOARDING$HELLO_TITLE)}
              type="text"
              value={message}
              onChange={(event) => setMessage(event.target.value)}
              onKeyDown={(event) => {
                if (event.key === "Enter" && !event.shiftKey) {
                  event.preventDefault();
                  launchConversation();
                }
              }}
              placeholder={defaultMessage}
              disabled={isLaunching}
              // Keep a fixed 16px input size; text-base scales with the host root font size.
              // eslint-disable-next-line shadcn/no-arbitrary-values
              className="min-h-5 w-full flex-1 bg-transparent text-[16px] font-normal leading-5 text-contrast outline-none placeholder:text-text-tertiary disabled:cursor-not-allowed disabled:opacity-50"
            />
          </div>
        </div>
        <div className="flex w-full min-w-0 items-center justify-end gap-2">
          <ChatSendButton
            buttonClassName=""
            handleSubmit={launchConversation}
            disabled={!canSubmit}
            isPending={isPendingLaunch}
          />
        </div>
      </form>

      {isPendingLaunch ? (
        <p
          role="status"
          aria-live="polite"
          className="mt-3 text-sm text-[var(--oh-muted)]"
        >
          {t(I18nKey.ONBOARDING$HELLO_LAUNCHING)}
        </p>
      ) : null}
      {launchStatus === "error" ? (
        <div
          role="alert"
          className="mt-3 flex items-center gap-2 text-sm text-danger"
        >
          <span>{t(I18nKey.ERROR$GENERIC)}</span>
          <button
            type="button"
            className="underline underline-offset-2"
            onClick={launchConversation}
          >
            {t(I18nKey.LAUNCH$TRY_AGAIN)}
          </button>
        </div>
      ) : null}

      <div
        data-testid="onboarding-hello-or-separator"
        className="mt-6 flex w-full items-center gap-3"
      >
        <div className="h-px flex-1 bg-border" />
        <span className="text-xs uppercase text-muted">
          {t(I18nKey.LANDING$OR)}
        </span>
        <div className="h-px flex-1 bg-border" />
      </div>

      <div
        data-testid="onboarding-recommended-automations"
        className="flex min-h-0 flex-1 flex-col"
      >
        <RecommendedAutomationsLauncher
          onLaunched={onLaunched}
          scrollableGrid
        />
      </div>

      <div className="flex shrink-0 items-center justify-between gap-2 bg-base-secondary pt-7 pb-7">
        <BrandButton
          testId="onboarding-hello-back"
          type="button"
          variant="secondary"
          onClick={onBack}
          isDisabled={isLaunching}
        >
          {t(I18nKey.ONBOARDING$BACK)}
        </BrandButton>
        <BrandButton
          testId="onboarding-hello-close"
          type="button"
          variant="secondary"
          onClick={onClose}
          isDisabled={isLaunching}
        >
          {t(I18nKey.BUTTON$CLOSE)}
        </BrandButton>
      </div>
    </div>
  );
}