this repo has no description
1import {useCallback} from 'react'
2import {msg} from '@lingui/core/macro'
3import {useLingui} from '@lingui/react'
4import {Trans} from '@lingui/react/macro'
5
6import {useRequireEmailVerification} from '#/lib/hooks/useRequireEmailVerification'
7import {logger} from '#/logger'
8import {useGetConvoForMembers} from '#/state/queries/messages/get-convo-for-members'
9import {FAB} from '#/view/com/util/fab/FAB'
10import {useTheme} from '#/alf'
11import * as Dialog from '#/components/Dialog'
12import {SearchablePeopleList} from '#/components/dialogs/SearchablePeopleList'
13import {PlusLarge_Stroke2_Corner0_Rounded as Plus} from '#/components/icons/Plus'
14import * as Toast from '#/components/Toast'
15import {useAnalytics} from '#/analytics'
16
17export function NewChat({
18 control,
19 onNewChat,
20}: {
21 control: Dialog.DialogControlProps
22 onNewChat: (chatId: string) => void
23}) {
24 const t = useTheme()
25 const {_} = useLingui()
26 const ax = useAnalytics()
27 const requireEmailVerification = useRequireEmailVerification()
28
29 const {mutate: createChat} = useGetConvoForMembers({
30 onSuccess: data => {
31 onNewChat(data.convo.id)
32
33 if (!data.convo.lastMessage) {
34 ax.metric('chat:create', {logContext: 'NewChatDialog'})
35 }
36 ax.metric('chat:open', {logContext: 'NewChatDialog'})
37 },
38 onError: error => {
39 logger.error('Failed to create chat', {safeMessage: error})
40 Toast.show(_(msg`An issue occurred starting the chat`), {
41 type: 'error',
42 })
43 },
44 })
45
46 const onCreateChat = useCallback(
47 (did: string) => {
48 control.close(() => createChat([did]))
49 },
50 [control, createChat],
51 )
52
53 const onPress = useCallback(() => {
54 control.open()
55 }, [control])
56 const wrappedOnPress = requireEmailVerification(onPress, {
57 instructions: [
58 <Trans key="new-chat">
59 Before you can message another user, you must first verify your email.
60 </Trans>,
61 ],
62 })
63
64 return (
65 <>
66 <FAB
67 testID="newChatFAB"
68 onPress={wrappedOnPress}
69 icon={<Plus size="lg" fill={t.palette.white} />}
70 accessibilityRole="button"
71 accessibilityLabel={_(msg`New chat`)}
72 accessibilityHint=""
73 />
74
75 <Dialog.Outer
76 control={control}
77 testID="newChatDialog"
78 nativeOptions={{fullHeight: true}}>
79 <Dialog.Handle />
80 <SearchablePeopleList
81 title={_(msg`Start a new chat`)}
82 onSelectChat={onCreateChat}
83 sortByMessageDeclaration
84 />
85 </Dialog.Outer>
86 </>
87 )
88}