Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

2.7 KB
Raw Blame
import { COMMAND_CONFIRMATION_FAILED } from "@/features/chat/transport/command-delivery";
import { useCallback, useState } from "react";

export interface UserReplyPayload {
  text?: string;
  answers?: Array<{ questionId: string; text: string }>;
}

/** What a host does with a card's answers, and whether they landed. */
export type SubmitUserReply = (
  payload: UserReplyPayload,
) => void | boolean | Promise<void | boolean>;

/**
 * Send a card's answers, and know whether they reached a turn waiting for them.
 *
 * A question card outlives the turn that asked it — a backend restart drops
 * the waiter while the card stays on screen, fully interactive — so "sending"
 * is a claim that can turn out false. When it does the card has to reopen:
 * a submission that cannot succeed must not look like one still in flight.
 *
 * Both question cards implement that same three-state dance, so it lives here
 * rather than twice.
 */
export function useCardSubmission(onSubmit: SubmitUserReply, rejectionKey?: string, acknowledgementKey?: string) {
  const [sending, setSending] = useState(false);
  const [failed, setFailed] = useState(false);
  const [failureMessage, setFailureMessage] = useState<string | null>(null);
  const [submittedAfterRejection, setSubmittedAfterRejection] = useState<string>();
  const [submittedAfterAcknowledgement, setSubmittedAfterAcknowledgement] = useState<string>();

  const submit = useCallback(
    async (payload: UserReplyPayload) => {
      setSending(true);
      setFailed(false);
      setFailureMessage(null);
      setSubmittedAfterRejection(rejectionKey);
      setSubmittedAfterAcknowledgement(acknowledgementKey);
      let accepted: void | boolean;
      try {
        accepted = await onSubmit(payload);
      } catch {
        setFailureMessage(COMMAND_CONFIRMATION_FAILED);
        accepted = false;
      }
      // ``undefined`` is a host that does not report a verdict; only an
      // explicit ``false`` reopens the card. The picked answers survive in the
      // caller's state, so retrying is one click.
      if (accepted === false) {
        setSending(false);
        setFailed(true);
      }
    },
    [onSubmit, rejectionKey, acknowledgementKey],
  );

  // A delivered turn may still reject its answer contract on the server.
  // Compare durable event identities, not copy: identical retries must each
  // reopen the existing editors, without clearing the learner's answers.
  const rejected = Boolean(rejectionKey && rejectionKey !== submittedAfterRejection);
  const acknowledged = acknowledgementKey !== submittedAfterAcknowledgement;
  return { sending: sending && !rejected && !acknowledged, failed: (failed && !acknowledged) || rejected, failureMessage, submit };
}