forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
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 * as Toast from '#/view/com/util/Toast'
11import {useTheme} from '#/alf'
12import * as Dialog from '#/components/Dialog'
13import {SearchablePeopleList} from '#/components/dialogs/SearchablePeopleList'
14import {PlusLarge_Stroke2_Corner0_Rounded as Plus} from '#/components/icons/Plus'
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`), 'xmark')
41 },
42 })
43
44 const onCreateChat = useCallback(
45 (did: string) => {
46 control.close(() => createChat([did]))
47 },
48 [control, createChat],
49 )
50
51 const onPress = useCallback(() => {
52 control.open()
53 }, [control])
54 const wrappedOnPress = requireEmailVerification(onPress, {
55 instructions: [
56 <Trans key="new-chat">
57 Before you can message another user, you must first verify your email.
58 </Trans>,
59 ],
60 })
61
62 return (
63 <>
64 <FAB
65 testID="newChatFAB"
66 onPress={wrappedOnPress}
67 icon={<Plus size="lg" fill={t.palette.white} />}
68 accessibilityRole="button"
69 accessibilityLabel={_(msg`New chat`)}
70 accessibilityHint=""
71 />
72
73 <Dialog.Outer control={control} testID="newChatDialog">
74 <Dialog.Handle />
75 <SearchablePeopleList
76 title={_(msg`Start a new chat`)}
77 onSelectChat={onCreateChat}
78 sortByMessageDeclaration
79 />
80 </Dialog.Outer>
81 </>
82 )
83}