import React, { useState, useEffect, useMemo } from "react"; import { Modal, Text, TextInput, TouchableOpacity, View, } from "react-native"; import { useTheme } from "../../../theme"; import { useCometChatTranslation } from "../../resources/CometChatLocalizeNew"; import { getLinkConfirmPopupStyleLight, getLinkConfirmPopupStyleDark, } from "./style"; export interface CometChatLinkConfirmPopupInterface { /** Whether the popup is visible */ visible: boolean; /** The URL of the tapped link */ url: string; /** Initial display text for edit mode */ initialEditText?: string; /** Initial URL for edit mode */ initialEditUrl?: string; /** When true, opens directly in edit mode with "Insert Link" title. * The confirm view (Edit/Remove) is skipped entirely. */ insertMode?: boolean; /** Called when the popup is dismissed (backdrop press or cancel) */ onDismiss: () => void; /** Called when the user saves an edited link */ onEdit: (newUrl: string, newText: string) => void; /** Called when the user removes the link */ onRemove: () => void; } export const CometChatLinkConfirmPopup = (props: CometChatLinkConfirmPopupInterface) => { const { visible, url, initialEditText = "", initialEditUrl = "", insertMode = false, onDismiss, onEdit, onRemove, } = props; const theme = useTheme(); const { t } = useCometChatTranslation(); const styles = useMemo( () => theme.mode === "dark" ? getLinkConfirmPopupStyleDark(theme.color, theme.spacing, theme.typography) : getLinkConfirmPopupStyleLight(theme.color, theme.spacing, theme.typography), [theme] ); const [editMode, setEditMode] = useState(false); const [editUrl, setEditUrl] = useState(initialEditUrl || url); const [editText, setEditText] = useState(initialEditText); // Sync state when popup opens with new data useEffect(() => { if (visible) { setEditMode(insertMode); setEditUrl(initialEditUrl || url); setEditText(initialEditText); } }, [visible, url, initialEditUrl, initialEditText, insertMode]); const handleDismiss = () => { setEditMode(false); onDismiss(); }; const handleSave = () => { if (editUrl.trim()) { onEdit(editUrl.trim(), editText.trim() || editUrl.trim()); } }; return ( {editMode ? ( <> {insertMode ? t("INSERT_LINK") : t("EDIT_LINK")} { t("CANCEL") } {insertMode ? t("INSERT") : t("SAVE")} ) : ( <> { t("LINK") } {url} setEditMode(true)} > { t("EDIT") } { t("REMOVE") } )} ); };