Skip to content

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

5.3 KB
Raw Blame
"use client";

import { PAGE_TITLE_CLASS } from '@/components/layout/FeaturePage'
import { Check, Pencil, X } from "lucide-react";
import { useTranslation } from "react-i18next";
import { useEffect, useRef, useState } from "react";
import Tooltip from "@/shared/ui/Tooltip";

import type { Translate } from "./format";

/**
 * The path's name, and the only place it can be changed.
 *
 * A path is named by the tutor when it is built, which makes that name a good
 * default and a poor final answer — it is the learner's course, so the learner
 * gets to say what it is called. Editing happens in place rather than through a
 * dialog: the title is one line, and a modal for one line reads as a bigger
 * commitment than renaming actually is.
 *
 * Submitting an empty name is allowed and meaningful — it hands the path back
 * to its derived name (the tutor's, or the first module's) instead of pinning
 * an empty string.
 *
 * Losing focus does NOT save. A rename is a deliberate act, and committing on
 * blur turns "clicked somewhere else" into a silent rename — while also racing
 * whatever focus the field was given when it opened. Blur only closes an
 * editor the learner never changed.
 */
export function PathTitle({
  displayName,
  storedName,
  onRename,
}: {
  /** What the learner sees — may be derived when the path has no name yet. */
  displayName: string;
  /**
   * What is actually stored, which is what the editor opens with. Seeding the
   * field with a derived label would let "save" pin that placeholder as the
   * real name and quietly cost the path its fallback.
   */
  storedName: string;
  /** Resolves once the new name is committed; may reject. */
  onRename: (name: string) => Promise<void>;
}) {
  const { t } = useTranslation();
  const [editing, setEditing] = useState(false);
  const [draft, setDraft] = useState(storedName);
  const [saving, setSaving] = useState(false);
  const inputRef = useRef<HTMLInputElement>(null);

  // Reopen on a different path (or after a rename lands) with that path's name,
  // never with whatever was half-typed for the previous one.
  useEffect(() => {
    if (!editing) setDraft(storedName);
  }, [storedName, editing]);

  useEffect(() => {
    if (editing) inputRef.current?.select();
  }, [editing]);

  const commit = async () => {
    if (saving) return;
    const next = draft.trim();
    if (next === storedName.trim()) {
      setEditing(false);
      return;
    }
    setSaving(true);
    try {
      await onRename(next);
      setEditing(false);
    } catch {
      // Keep the editor open with the text still in it — the alternative is
      // silently discarding what they typed.
    } finally {
      setSaving(false);
    }
  };

  const cancel = () => {
    setDraft(storedName);
    setEditing(false);
  };

  const TITLE_TYPE = PAGE_TITLE_CLASS;

  if (!editing) {
    return (
      <div className="flex min-w-0 items-center gap-2">
        <h1 className={`truncate ${TITLE_TYPE}`}>{displayName}</h1>
        {/* Always present, like the reset/delete controls beside it. A
            hover-revealed affordance does not exist on a touch screen. */}
        <Tooltip label={t("Rename")}>
          <button
            type="button"
            onClick={() => setEditing(true)}
            aria-label={t("Rename")}
            className="shrink-0 cursor-pointer rounded-lg p-1.5 text-[var(--muted-foreground)]/60 transition-colors hover:bg-[var(--accent)] hover:text-[var(--foreground)]"
          >
            <Pencil className="h-4 w-4" />
          </button>
        </Tooltip>
      </div>
    );
  }

  return (
    <div className="flex min-w-0 items-center gap-2">
      <input
        ref={inputRef}
        value={draft}
        autoFocus
        disabled={saving}
        onChange={(event) => setDraft(event.target.value)}
        onKeyDown={(event) => {
          if (event.key === "Enter") void commit();
          if (event.key === "Escape") cancel();
        }}
        onBlur={() => {
          if (draft.trim() === storedName.trim()) setEditing(false);
        }}
        maxLength={200}
        aria-label={t("Topic name")}
        placeholder={t("What is this path called?")}
        className={`min-w-0 flex-1 rounded-xl border border-[var(--border)] bg-[var(--background)] px-3 py-1 outline-none focus:border-[var(--primary)] disabled:opacity-60 ${TITLE_TYPE}`}
      />
      {/* mousedown, not click: blur fires first and would commit-then-cancel. */}
      <Tooltip label={t("Save")}>
        <button
          type="button"
          onMouseDown={(event) => {
            event.preventDefault();
            void commit();
          }}
          aria-label={t("Save")}
          className="shrink-0 cursor-pointer rounded-lg p-1.5 text-[var(--muted-foreground)] hover:bg-[var(--accent)] hover:text-[var(--foreground)]"
        >
          <Check className="h-4 w-4" />
        </button>
      </Tooltip>
      <Tooltip label={t("Cancel")}>
        <button
          type="button"
          onMouseDown={(event) => {
            event.preventDefault();
            cancel();
          }}
          aria-label={t("Cancel")}
          className="shrink-0 cursor-pointer rounded-lg p-1.5 text-[var(--muted-foreground)] hover:bg-[var(--accent)] hover:text-[var(--foreground)]"
        >
          <X className="h-4 w-4" />
        </button>
      </Tooltip>
    </div>
  );
}