// Copyright 2024 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import type { FormEvent } from 'react'; import React, { useCallback, useMemo, useState } from 'react'; import uuid from 'uuid'; import { z } from 'zod'; import { Modal } from './Modal'; import type { LocalizerType } from '../types/I18N'; import { Avatar, AvatarSize } from './Avatar'; import type { ConversationType, NicknameAndNote, } from '../state/ducks/conversations'; import { Input } from './Input'; import { AutoSizeTextArea } from './AutoSizeTextArea'; import { Button, ButtonVariant } from './Button'; import { strictAssert } from '../util/assert'; const formSchema = z.object({ nickname: z .object({ givenName: z.string().nullable(), familyName: z.string().nullable(), }) .nullable(), note: z.string().nullable(), }); function toOptionalStringValue(value: string): string | null { const trimmed = value.trim(); return trimmed === '' ? null : trimmed; } export type EditNicknameAndNoteModalProps = Readonly<{ conversation: ConversationType; i18n: LocalizerType; onSave: (result: NicknameAndNote) => void; onClose: () => void; }>; export function EditNicknameAndNoteModal({ conversation, i18n, onSave, onClose, }: EditNicknameAndNoteModalProps): JSX.Element { strictAssert( conversation.type === 'direct', 'Expected a direct conversation' ); const [givenName, setGivenName] = useState( conversation.nicknameGivenName ?? '' ); const [familyName, setFamilyName] = useState( conversation.nicknameFamilyName ?? '' ); const [note, setNote] = useState(conversation.note ?? ''); const [formId] = useState(() => uuid()); const [givenNameId] = useState(() => uuid()); const [familyNameId] = useState(() => uuid()); const [noteId] = useState(() => uuid()); const formResult = useMemo(() => { const givenNameValue = toOptionalStringValue(givenName); const familyNameValue = toOptionalStringValue(familyName); const noteValue = toOptionalStringValue(note); const hasEitherName = givenNameValue != null || familyNameValue != null; return formSchema.safeParse({ nickname: hasEitherName ? { givenName: givenNameValue, familyName: familyNameValue } : null, note: noteValue, }); }, [givenName, familyName, note]); const handleSubmit = useCallback( (event: FormEvent) => { event.preventDefault(); if (formResult.success) { onSave(formResult.data); onClose(); } }, [formResult, onSave, onClose] ); return ( } >

{i18n('icu:EditNicknameAndNoteModal__Description')}

{ setGivenName(value); }} /> { setFamilyName(value); }} /> { setNote(value); }} />
); }