import { useEntity } from "src/replicache";
import { ImageAltModal } from "./ImageAltModal";
import { EditTiny } from "components/Icons/EditTiny";
import { useState } from "react";
import { useSpring, animated } from "@react-spring/web";
import useMeasure from "react-use-measure";
import { ReadOnlyAltText } from "./ReadOnlyAltText";
// Overlaid on an image; visible on hover (within a `group/image`), while the
// containing block is selected, or whenever alt text already exists. Opens the
// alt-text modal. Shared by ImageBlock and the image gallery.
// Readers (canEdit === false) get only the "ALT" toggle to reveal existing alt
// text — the edit/add-alt trigger is hidden.
export function ImageAltButton(props: {
entityID: string;
selected: boolean;
canEdit?: boolean;
className?: string;
}) {
let canEdit = props.canEdit ?? true;
let hasAlt = !!useEntity(props.entityID, "image/alt")?.data.value;
let alt = useEntity(props.entityID, "image/alt")?.data.value;
let [showAlt, setShowAlt] = useState(false);
let [altRef, { height: altHeight }] = useMeasure();
let altStyle = useSpring({
height: showAlt ? altHeight : 0,
opacity: showAlt ? 1 : 0,
config: { tension: 450, friction: 30 },
});
// Readers have nothing to interact with when there's no alt text.
if (!canEdit && !hasAlt) return null;
// Readers get the plain ALT pill + preview, shared with published posts.
if (!canEdit)
return