import { useEffect, useRef, useState } from "react"; import { ActionButtonWithTooltip } from "@/components/ui/action-button"; import { ButtonWithTooltip } from "@/components/ui/button"; import { Tooltip, TooltipContent, TooltipPortal, TooltipTrigger, } from "@/components/ui/tooltip"; import { Check, Pencil, X } from "lucide-react"; interface Props { viewClassName?: string; untitledClassName?: string; editClassName?: string; onSave: (title: string | null) => void; isSaving: boolean; originalText: string | null; setEditable: (editable: boolean) => void; } function EditMode({ onSave: onSaveCB, editClassName: className, isSaving, originalText, setEditable, }: Props) { const ref = useRef(null); useEffect(() => { if (ref.current) { ref.current.focus(); ref.current.textContent = originalText; } }, [ref]); const onSave = () => { let toSave: string | null = ref.current?.textContent ?? null; if (originalText == toSave) { // Nothing to do here return; } if (toSave == "") { toSave = null; } onSaveCB(toSave); setEditable(false); }; return (
{ if (e.key === "Enter") { e.preventDefault(); onSave(); } }} /> onSave()} > { setEditable(false); }} >
); } function ViewMode({ originalText, setEditable, viewClassName, untitledClassName, }: Props) { return (
{originalText ? (

{originalText}

) : (

Untitled

)}
{ setEditable(true); }} >
{originalText && ( {originalText} )}
); } export function EditableText(props: { viewClassName?: string; untitledClassName?: string; editClassName?: string; originalText: string | null; onSave: (title: string | null) => void; isSaving: boolean; }) { const [editable, setEditable] = useState(false); return editable ? ( ) : ( ); }