// Copyright 2024 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import React, { useCallback, useMemo, useState } from 'react'; import { v4 as generateUuid } from 'uuid'; import { Modal } from './Modal'; import type { LocalizerType } from '../types/I18N'; import { CallLinkRestrictions, toCallLinkRestrictions, type CallLinkType, } from '../types/CallLink'; import { Input } from './Input'; import { Select } from './Select'; import { linkCallRoute } from '../util/signalRoutes'; import { Button, ButtonSize, ButtonVariant } from './Button'; import { Avatar, AvatarSize } from './Avatar'; import { formatUrlWithoutProtocol } from '../util/url'; export type CallLinkEditModalProps = { i18n: LocalizerType; callLink: CallLinkType; onClose: () => void; onCopyCallLink: () => void; onUpdateCallLinkName: (name: string) => void; onUpdateCallLinkRestrictions: (restrictions: CallLinkRestrictions) => void; onShareCallLinkViaSignal: () => void; onStartCallLinkLobby: () => void; }; export function CallLinkEditModal({ i18n, callLink, onClose, onCopyCallLink, onUpdateCallLinkName, onUpdateCallLinkRestrictions, onShareCallLinkViaSignal, onStartCallLinkLobby, }: CallLinkEditModalProps): JSX.Element { const { name: savedName, restrictions: savedRestrictions } = callLink; const [nameId] = useState(() => generateUuid()); const [restrictionsId] = useState(() => generateUuid()); const [nameInput, setNameInput] = useState(savedName); const [restrictionsInput, setRestrictionsInput] = useState(savedRestrictions); // We only want to use the default name "Signal Call" as a value if the user // modified the input and then chose that name. Doesn't revert when saved. const [nameTouched, setNameTouched] = useState(false); const callLinkWebUrl = useMemo(() => { return formatUrlWithoutProtocol( linkCallRoute.toWebUrl({ key: callLink.rootKey }) ); }, [callLink.rootKey]); const onSaveName = useCallback( (newName: string) => { if (!nameTouched) { return; } if (newName === savedName) { return; } onUpdateCallLinkName(newName); }, [nameTouched, savedName, onUpdateCallLinkName] ); const onSaveRestrictions = useCallback( (newRestrictions: CallLinkRestrictions) => { if (newRestrictions === savedRestrictions) { return; } onUpdateCallLinkRestrictions(newRestrictions); }, [savedRestrictions, onUpdateCallLinkRestrictions] ); return ( { // Save the modal in case the user hits escape onSaveName(nameInput); onClose(); }} >
{ setNameTouched(true); setNameInput(value); }} onBlur={() => { onSaveName(nameInput); }} onEnter={() => { onSaveName(nameInput); }} placeholder={i18n('icu:calling__call-link-default-title')} />