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