import { Block, ButtonIcon, Check, Close, Input, LabelSmall, ParagraphSmall, Spinner, useStyletron, } from "@mezo-org/mezo-clay" import React, { useCallback, useEffect, useState } from "react" import EditIcon from "../../../assets/EditIcon" type InlineEditFieldProps = { label: string value: string placeholder?: string onSave: (value: string) => Promise validate?: (value: string) => string | null isLoading?: boolean } type InlineFieldWrapperProps = { label: string children: React.ReactNode } function InlineFieldWrapper({ label, children }: InlineFieldWrapperProps) { const [, theme] = useStyletron() return ( {label} {children} ) } export default function InlineEditField(props: InlineEditFieldProps) { const { label, value, placeholder, onSave, validate, isLoading } = props const [, theme] = useStyletron() const [isEditing, setIsEditing] = useState(false) const [editValue, setEditValue] = useState(value) const [error, setError] = useState(null) const [isSaving, setIsSaving] = useState(false) useEffect(() => { setEditValue(value) }, [value]) const handleEdit = useCallback(() => { setIsEditing(true) setEditValue(value) setError(null) }, [value]) const handleCancel = useCallback(() => { setIsEditing(false) setEditValue(value) setError(null) }, [value]) const handleSave = useCallback(async () => { if (validate) { const validationError = validate(editValue) if (validationError) { setError(validationError) return } } setIsSaving(true) try { await onSave(editValue) setIsEditing(false) setError(null) } catch (err) { setError(err instanceof Error ? err.message : "Failed to save") } finally { setIsSaving(false) } }, [editValue, onSave, validate]) const handleKeyDown = useCallback( (e: React.KeyboardEvent) => { if (e.key === "Enter") { handleSave() } else if (e.key === "Escape") { handleCancel() } }, [handleSave, handleCancel], ) if (isLoading) { return ( ) } if (isEditing) { return ( { setEditValue(e.currentTarget.value) setError(null) }} onKeyDown={handleKeyDown} placeholder={placeholder} size="small" error={!!error} disabled={isSaving} autoFocus /> {isSaving ? ( ) : ( )} {error && ( {error} )} ) } return ( {value || placeholder} ) }