"use client"; import { useActionState, useRef, useEffect, useState, useCallback, } from "react"; import { updateNameAction, type UpdateNameState } from "./actions"; const initialState: UpdateNameState = null; export function EditableName({ currentName }: Readonly<{ currentName: string }>) { const [editing, setEditing] = useState(false); const [state, action, pending] = useActionState( updateNameAction, initialState, ); const inputRef = useRef(null); // Focus input when entering edit mode useEffect(() => { if (editing) { inputRef.current?.focus(); inputRef.current?.select(); } }, [editing]); const handleEdit = useCallback(() => setEditing(true), []); if (!editing) { return (

{currentName}

); } return (
{state && "error" in state && (

{state.error}

)}
); }