chat 1.0.6 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -96
- package/readme.md +1 -201
- package/lib/module/ChatContext/index.js +0 -2
- package/lib/module/ChatContext/index.js.map +0 -1
- package/lib/module/ChatContext/types.js +0 -2
- package/lib/module/ChatContext/types.js.map +0 -1
- package/lib/module/classes/Chat/index.js +0 -2
- package/lib/module/classes/Chat/index.js.map +0 -1
- package/lib/module/classes/Chat/types.js +0 -2
- package/lib/module/classes/Chat/types.js.map +0 -1
- package/lib/module/classes/Inbox/index.js +0 -2
- package/lib/module/classes/Inbox/index.js.map +0 -1
- package/lib/module/classes/Inbox/types.js +0 -2
- package/lib/module/classes/Inbox/types.js.map +0 -1
- package/lib/module/classes/Message/index.js +0 -2
- package/lib/module/classes/Message/index.js.map +0 -1
- package/lib/module/classes/Message/types.js +0 -2
- package/lib/module/classes/Message/types.js.map +0 -1
- package/lib/module/classes/Reaction/index.js +0 -2
- package/lib/module/classes/Reaction/index.js.map +0 -1
- package/lib/module/classes/Reaction/types.js +0 -2
- package/lib/module/classes/Reaction/types.js.map +0 -1
- package/lib/module/classes/Room/index.js +0 -2
- package/lib/module/classes/Room/index.js.map +0 -1
- package/lib/module/classes/Room/types.js +0 -2
- package/lib/module/classes/Room/types.js.map +0 -1
- package/lib/module/classes/index.js +0 -2
- package/lib/module/classes/index.js.map +0 -1
- package/lib/module/components/Avatar/index.js +0 -2
- package/lib/module/components/Avatar/index.js.map +0 -1
- package/lib/module/components/Chat/Bubble/index.js +0 -2
- package/lib/module/components/Chat/Bubble/index.js.map +0 -1
- package/lib/module/components/Chat/Composer/Input/index.js +0 -2
- package/lib/module/components/Chat/Composer/Input/index.js.map +0 -1
- package/lib/module/components/Chat/Composer/Send/index.js +0 -2
- package/lib/module/components/Chat/Composer/Send/index.js.map +0 -1
- package/lib/module/components/Chat/Composer/index.js +0 -2
- package/lib/module/components/Chat/Composer/index.js.map +0 -1
- package/lib/module/components/Chat/Media/Image/index.js +0 -2
- package/lib/module/components/Chat/Media/Image/index.js.map +0 -1
- package/lib/module/components/Chat/Media/index.js +0 -2
- package/lib/module/components/Chat/Media/index.js.map +0 -1
- package/lib/module/components/Chat/Message/Swiper/Action/index.js +0 -2
- package/lib/module/components/Chat/Message/Swiper/Action/index.js.map +0 -1
- package/lib/module/components/Chat/Message/Swiper/index.js +0 -2
- package/lib/module/components/Chat/Message/Swiper/index.js.map +0 -1
- package/lib/module/components/Chat/Message/Touchable/index.js +0 -2
- package/lib/module/components/Chat/Message/Touchable/index.js.map +0 -1
- package/lib/module/components/Chat/Message/index.js +0 -2
- package/lib/module/components/Chat/Message/index.js.map +0 -1
- package/lib/module/components/Chat/SystemMessage/index.js +0 -2
- package/lib/module/components/Chat/SystemMessage/index.js.map +0 -1
- package/lib/module/components/Chat/TypingFooter/index.js +0 -2
- package/lib/module/components/Chat/TypingFooter/index.js.map +0 -1
- package/lib/module/components/Chat/index.js +0 -2
- package/lib/module/components/Chat/index.js.map +0 -1
- package/lib/module/components/Chat/types.js +0 -2
- package/lib/module/components/Chat/types.js.map +0 -1
- package/lib/module/components/Chat-Old/Bubble/index.js +0 -2
- package/lib/module/components/Chat-Old/Bubble/index.js.map +0 -1
- package/lib/module/components/Chat-Old/Composer/Input/index.js +0 -2
- package/lib/module/components/Chat-Old/Composer/Input/index.js.map +0 -1
- package/lib/module/components/Chat-Old/Composer/Send/index.js +0 -2
- package/lib/module/components/Chat-Old/Composer/Send/index.js.map +0 -1
- package/lib/module/components/Chat-Old/Composer/index.js +0 -2
- package/lib/module/components/Chat-Old/Composer/index.js.map +0 -1
- package/lib/module/components/Chat-Old/Message/index.js +0 -2
- package/lib/module/components/Chat-Old/Message/index.js.map +0 -1
- package/lib/module/components/Chat-Old/Swiper/Action/index.js +0 -2
- package/lib/module/components/Chat-Old/Swiper/Action/index.js.map +0 -1
- package/lib/module/components/Chat-Old/Swiper/index.js +0 -2
- package/lib/module/components/Chat-Old/Swiper/index.js.map +0 -1
- package/lib/module/components/Chat-Old/SystemMessage/index.js +0 -2
- package/lib/module/components/Chat-Old/SystemMessage/index.js.map +0 -1
- package/lib/module/components/Chat-Old/index.js +0 -2
- package/lib/module/components/Chat-Old/index.js.map +0 -1
- package/lib/module/components/Chat-Old/types.js +0 -2
- package/lib/module/components/Chat-Old/types.js.map +0 -1
- package/lib/module/components/ChatProvider/index.js +0 -2
- package/lib/module/components/ChatProvider/index.js.map +0 -1
- package/lib/module/components/ChatProvider/types.js +0 -2
- package/lib/module/components/ChatProvider/types.js.map +0 -1
- package/lib/module/components/Inbox/Item/Circle/index.js +0 -2
- package/lib/module/components/Inbox/Item/Circle/index.js.map +0 -1
- package/lib/module/components/Inbox/Item/Circle/types.js +0 -2
- package/lib/module/components/Inbox/Item/Circle/types.js.map +0 -1
- package/lib/module/components/Inbox/Item/Initials/index.js +0 -2
- package/lib/module/components/Inbox/Item/Initials/index.js.map +0 -1
- package/lib/module/components/Inbox/Item/ItemDate/index.js +0 -2
- package/lib/module/components/Inbox/Item/ItemDate/index.js.map +0 -1
- package/lib/module/components/Inbox/Item/Separator/index.js +0 -2
- package/lib/module/components/Inbox/Item/Separator/index.js.map +0 -1
- package/lib/module/components/Inbox/Item/Subtitle/index.js +0 -2
- package/lib/module/components/Inbox/Item/Subtitle/index.js.map +0 -1
- package/lib/module/components/Inbox/Item/Swipeable/index.js +0 -2
- package/lib/module/components/Inbox/Item/Swipeable/index.js.map +0 -1
- package/lib/module/components/Inbox/Item/Title/index.js +0 -2
- package/lib/module/components/Inbox/Item/Title/index.js.map +0 -1
- package/lib/module/components/Inbox/Item/UnreadIndicator/index.js +0 -2
- package/lib/module/components/Inbox/Item/UnreadIndicator/index.js.map +0 -1
- package/lib/module/components/Inbox/Item/index.js +0 -2
- package/lib/module/components/Inbox/Item/index.js.map +0 -1
- package/lib/module/components/Inbox/index.js +0 -2
- package/lib/module/components/Inbox/index.js.map +0 -1
- package/lib/module/components/Inbox/question.md +0 -21
- package/lib/module/components/Inbox/readme.md +0 -21
- package/lib/module/components/Inbox/types.js +0 -2
- package/lib/module/components/Inbox/types.js.map +0 -1
- package/lib/module/components/RoomCreator/Content/Footer/Button/index.js +0 -2
- package/lib/module/components/RoomCreator/Content/Footer/Button/index.js.map +0 -1
- package/lib/module/components/RoomCreator/Content/Footer/Input/index.js +0 -2
- package/lib/module/components/RoomCreator/Content/Footer/Input/index.js.map +0 -1
- package/lib/module/components/RoomCreator/Content/Footer/index.js +0 -2
- package/lib/module/components/RoomCreator/Content/Footer/index.js.map +0 -1
- package/lib/module/components/RoomCreator/Content/Item/Icon/index.js +0 -2
- package/lib/module/components/RoomCreator/Content/Item/Icon/index.js.map +0 -1
- package/lib/module/components/RoomCreator/Content/Item/index.js +0 -2
- package/lib/module/components/RoomCreator/Content/Item/index.js.map +0 -1
- package/lib/module/components/RoomCreator/Content/index.js +0 -2
- package/lib/module/components/RoomCreator/Content/index.js.map +0 -1
- package/lib/module/components/RoomCreator/Header/index.js +0 -2
- package/lib/module/components/RoomCreator/Header/index.js.map +0 -1
- package/lib/module/components/RoomCreator/index.js +0 -2
- package/lib/module/components/RoomCreator/index.js.map +0 -1
- package/lib/module/components/RoomCreator/types.js +0 -2
- package/lib/module/components/RoomCreator/types.js.map +0 -1
- package/lib/module/components/TypingIndicator/index.js +0 -2
- package/lib/module/components/TypingIndicator/index.js.map +0 -1
- package/lib/module/components/index.js +0 -2
- package/lib/module/components/index.js.map +0 -1
- package/lib/module/helpers/getInitials.js +0 -2
- package/lib/module/helpers/getInitials.js.map +0 -1
- package/lib/module/helpers/sortByAvatar.js +0 -2
- package/lib/module/helpers/sortByAvatar.js.map +0 -1
- package/lib/module/hooks/index.js +0 -2
- package/lib/module/hooks/index.js.map +0 -1
- package/lib/module/hooks/useCreateRoom/index.js +0 -2
- package/lib/module/hooks/useCreateRoom/index.js.map +0 -1
- package/lib/module/hooks/useCreator/index.js +0 -2
- package/lib/module/hooks/useCreator/index.js.map +0 -1
- package/lib/module/hooks/useInbox/index.js +0 -2
- package/lib/module/hooks/useInbox/index.js.map +0 -1
- package/lib/module/hooks/useInbox/types.js +0 -2
- package/lib/module/hooks/useInbox/types.js.map +0 -1
- package/lib/module/hooks/useMessages/index.js +0 -2
- package/lib/module/hooks/useMessages/index.js.map +0 -1
- package/lib/module/hooks/useMessages/types.js +0 -2
- package/lib/module/hooks/useMessages/types.js.map +0 -1
- package/lib/module/hooks/useRoom/index.js +0 -2
- package/lib/module/hooks/useRoom/index.js.map +0 -1
- package/lib/module/hooks/useRoom/types.js +0 -2
- package/lib/module/hooks/useRoom/types.js.map +0 -1
- package/lib/module/hooks/useSend/useSendToGroup/index.js +0 -2
- package/lib/module/hooks/useSend/useSendToGroup/index.js.map +0 -1
- package/lib/module/hooks/useSend/useSendToPeople/index.js +0 -2
- package/lib/module/hooks/useSend/useSendToPeople/index.js.map +0 -1
- package/lib/module/hooks/useTyping/index.js +0 -2
- package/lib/module/hooks/useTyping/index.js.map +0 -1
- package/lib/module/hooks/useTypingOLD/index.js +0 -2
- package/lib/module/hooks/useTypingOLD/index.js.map +0 -1
- package/lib/module/hooks/useTypingOLD/types.js +0 -2
- package/lib/module/hooks/useTypingOLD/types.js.map +0 -1
- package/lib/module/hooks/useViewMessages/index.js +0 -2
- package/lib/module/hooks/useViewMessages/index.js.map +0 -1
- package/lib/module/hooks/useViewMessages/types.js +0 -2
- package/lib/module/hooks/useViewMessages/types.js.map +0 -1
- package/lib/module/hooks/useViewMessagesOld/index.js +0 -2
- package/lib/module/hooks/useViewMessagesOld/index.js.map +0 -1
- package/lib/module/index.js +0 -2
- package/lib/module/index.js.map +0 -1
- package/lib/typescript/ChatContext/index.d.ts +0 -2
- package/lib/typescript/ChatContext/types.d.ts +0 -4
- package/lib/typescript/classes/Chat/index.d.ts +0 -14
- package/lib/typescript/classes/Chat/types.d.ts +0 -21
- package/lib/typescript/classes/Inbox/types.d.ts +0 -2
- package/lib/typescript/classes/Message/index.d.ts +0 -47
- package/lib/typescript/classes/Message/types.d.ts +0 -66
- package/lib/typescript/classes/Reaction/index.d.ts +0 -17
- package/lib/typescript/classes/Reaction/types.d.ts +0 -33
- package/lib/typescript/classes/Room/index.d.ts +0 -42
- package/lib/typescript/classes/Room/types.d.ts +0 -112
- package/lib/typescript/classes/index.d.ts +0 -4
- package/lib/typescript/components/Avatar/index.d.ts +0 -4
- package/lib/typescript/components/Chat/Bubble/index.d.ts +0 -4
- package/lib/typescript/components/Chat/Composer/Input/index.d.ts +0 -7
- package/lib/typescript/components/Chat/Composer/Send/index.d.ts +0 -4
- package/lib/typescript/components/Chat/Composer/index.d.ts +0 -8
- package/lib/typescript/components/Chat/Media/Image/index.d.ts +0 -7
- package/lib/typescript/components/Chat/Media/index.d.ts +0 -4
- package/lib/typescript/components/Chat/Message/Touchable/index.d.ts +0 -4
- package/lib/typescript/components/Chat/Message/index.d.ts +0 -8
- package/lib/typescript/components/Chat/SystemMessage/index.d.ts +0 -4
- package/lib/typescript/components/Chat/TypingFooter/index.d.ts +0 -4
- package/lib/typescript/components/Chat/index.d.ts +0 -8
- package/lib/typescript/components/Chat/types.d.ts +0 -844
- package/lib/typescript/components/ChatProvider/index.d.ts +0 -10
- package/lib/typescript/components/ChatProvider/types.d.ts +0 -7
- package/lib/typescript/components/Inbox/Item/Initials/index.d.ts +0 -4
- package/lib/typescript/components/Inbox/Item/ItemDate/index.d.ts +0 -4
- package/lib/typescript/components/Inbox/Item/Separator/index.d.ts +0 -6
- package/lib/typescript/components/Inbox/Item/Subtitle/index.d.ts +0 -3
- package/lib/typescript/components/Inbox/Item/Swipeable/index.d.ts +0 -4
- package/lib/typescript/components/Inbox/Item/Title/index.d.ts +0 -3
- package/lib/typescript/components/Inbox/Item/UnreadIndicator/index.d.ts +0 -4
- package/lib/typescript/components/Inbox/Item/index.d.ts +0 -6
- package/lib/typescript/components/Inbox/index.d.ts +0 -4
- package/lib/typescript/components/Inbox/types.d.ts +0 -304
- package/lib/typescript/components/RoomCreator/Content/Footer/Button/index.d.ts +0 -4
- package/lib/typescript/components/RoomCreator/Content/Footer/Input/index.d.ts +0 -4
- package/lib/typescript/components/RoomCreator/Content/Footer/index.d.ts +0 -3
- package/lib/typescript/components/RoomCreator/Content/Item/Icon/index.d.ts +0 -4
- package/lib/typescript/components/RoomCreator/Content/Item/index.d.ts +0 -4
- package/lib/typescript/components/RoomCreator/Content/index.d.ts +0 -4
- package/lib/typescript/components/RoomCreator/Header/index.d.ts +0 -4
- package/lib/typescript/components/RoomCreator/index.d.ts +0 -4
- package/lib/typescript/components/RoomCreator/types.d.ts +0 -208
- package/lib/typescript/components/TypingIndicator/index.d.ts +0 -9
- package/lib/typescript/components/index.d.ts +0 -12
- package/lib/typescript/helpers/getInitials.d.ts +0 -1
- package/lib/typescript/hooks/index.d.ts +0 -8
- package/lib/typescript/hooks/useCreator/index.d.ts +0 -27
- package/lib/typescript/hooks/useInbox/index.d.ts +0 -29
- package/lib/typescript/hooks/useInbox/types.d.ts +0 -5
- package/lib/typescript/hooks/useMessages/index.d.ts +0 -24
- package/lib/typescript/hooks/useMessages/types.d.ts +0 -5
- package/lib/typescript/hooks/useRoom/index.d.ts +0 -26
- package/lib/typescript/hooks/useRoom/types.d.ts +0 -5
- package/lib/typescript/hooks/useSend/useSendToGroup/index.d.ts +0 -6
- package/lib/typescript/hooks/useSend/useSendToPeople/index.d.ts +0 -6
- package/lib/typescript/hooks/useTyping/index.d.ts +0 -36
- package/lib/typescript/hooks/useViewMessages/index.d.ts +0 -18
- package/lib/typescript/hooks/useViewMessages/types.d.ts +0 -20
- package/lib/typescript/index.d.ts +0 -11
- package/src/ChatContext/index.tsx +0 -9
- package/src/ChatContext/types.ts +0 -4
- package/src/classes/Chat/index.ts +0 -46
- package/src/classes/Chat/types.ts +0 -23
- package/src/classes/Inbox/index.ts +0 -12
- package/src/classes/Inbox/types.ts +0 -3
- package/src/classes/Message/index.ts +0 -310
- package/src/classes/Message/types.ts +0 -118
- package/src/classes/Reaction/index.ts +0 -108
- package/src/classes/Reaction/types.ts +0 -37
- package/src/classes/Room/index.ts +0 -323
- package/src/classes/Room/types.ts +0 -161
- package/src/classes/index.ts +0 -4
- package/src/components/Avatar/index.tsx +0 -25
- package/src/components/Chat/Bubble/index.tsx +0 -139
- package/src/components/Chat/Composer/Input/index.tsx +0 -45
- package/src/components/Chat/Composer/Send/index.tsx +0 -51
- package/src/components/Chat/Composer/index.tsx +0 -184
- package/src/components/Chat/Media/Image/index.tsx +0 -26
- package/src/components/Chat/Media/index.tsx +0 -11
- package/src/components/Chat/Message/Swiper/Action/index.tsx +0 -7
- package/src/components/Chat/Message/Swiper/index.tsx +0 -32
- package/src/components/Chat/Message/Touchable/index.tsx +0 -60
- package/src/components/Chat/Message/index.tsx +0 -328
- package/src/components/Chat/SystemMessage/index.tsx +0 -44
- package/src/components/Chat/TypingFooter/index.tsx +0 -48
- package/src/components/Chat/index.tsx +0 -592
- package/src/components/Chat/types.ts +0 -911
- package/src/components/Chat-Old/Bubble/index.tsx +0 -58
- package/src/components/Chat-Old/Composer/Input/index.tsx +0 -34
- package/src/components/Chat-Old/Composer/Send/index.tsx +0 -37
- package/src/components/Chat-Old/Composer/index.tsx +0 -131
- package/src/components/Chat-Old/Message/index.tsx +0 -160
- package/src/components/Chat-Old/Swiper/Action/index.tsx +0 -7
- package/src/components/Chat-Old/Swiper/index.tsx +0 -30
- package/src/components/Chat-Old/SystemMessage/index.tsx +0 -44
- package/src/components/Chat-Old/index.tsx +0 -356
- package/src/components/Chat-Old/types.ts +0 -752
- package/src/components/ChatProvider/index.tsx +0 -31
- package/src/components/ChatProvider/types.ts +0 -8
- package/src/components/Inbox/Item/Circle/index.tsx +0 -47
- package/src/components/Inbox/Item/Circle/types.ts +0 -8
- package/src/components/Inbox/Item/Initials/index.tsx +0 -41
- package/src/components/Inbox/Item/ItemDate/index.tsx +0 -26
- package/src/components/Inbox/Item/Separator/index.tsx +0 -13
- package/src/components/Inbox/Item/Subtitle/index.tsx +0 -23
- package/src/components/Inbox/Item/Swipeable/index.tsx +0 -143
- package/src/components/Inbox/Item/Title/index.tsx +0 -23
- package/src/components/Inbox/Item/UnreadIndicator/index.tsx +0 -84
- package/src/components/Inbox/Item/index.tsx +0 -307
- package/src/components/Inbox/index.tsx +0 -315
- package/src/components/Inbox/question.md +0 -21
- package/src/components/Inbox/readme.md +0 -21
- package/src/components/Inbox/types.ts +0 -371
- package/src/components/RoomCreator/Content/Footer/Button/index.tsx +0 -55
- package/src/components/RoomCreator/Content/Footer/Input/index.tsx +0 -27
- package/src/components/RoomCreator/Content/Footer/index.tsx +0 -72
- package/src/components/RoomCreator/Content/Item/Icon/index.tsx +0 -16
- package/src/components/RoomCreator/Content/Item/index.tsx +0 -87
- package/src/components/RoomCreator/Content/index.tsx +0 -75
- package/src/components/RoomCreator/Header/index.tsx +0 -74
- package/src/components/RoomCreator/index.tsx +0 -177
- package/src/components/RoomCreator/types.ts +0 -233
- package/src/components/TypingIndicator/index.tsx +0 -169
- package/src/components/index.ts +0 -18
- package/src/helpers/getInitials.ts +0 -11
- package/src/helpers/sortByAvatar.ts +0 -1
- package/src/hooks/index.ts +0 -8
- package/src/hooks/useCreateRoom/index.ts +0 -34
- package/src/hooks/useCreator/index.ts +0 -33
- package/src/hooks/useInbox/index.ts +0 -76
- package/src/hooks/useInbox/types.ts +0 -6
- package/src/hooks/useMessages/index.ts +0 -109
- package/src/hooks/useMessages/types.ts +0 -6
- package/src/hooks/useRoom/index.ts +0 -82
- package/src/hooks/useRoom/types.ts +0 -6
- package/src/hooks/useSend/useSendToGroup/index.ts +0 -38
- package/src/hooks/useSend/useSendToPeople/index.ts +0 -38
- package/src/hooks/useTyping/index.ts +0 -154
- package/src/hooks/useTypingOLD/index.ts +0 -67
- package/src/hooks/useTypingOLD/types.ts +0 -7
- package/src/hooks/useViewMessages/index.ts +0 -61
- package/src/hooks/useViewMessages/types.ts +0 -22
- package/src/hooks/useViewMessagesOld/index.ts +0 -39
- package/src/index.ts +0 -13
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { useState, useRef, useEffect } from 'react'
|
|
2
|
-
import { NewRoomModel } from '../../classes/Room/types'
|
|
3
|
-
import Room from '../../classes/Room'
|
|
4
|
-
|
|
5
|
-
function useCreateRoom<RoomModel extends NewRoomModel>() {
|
|
6
|
-
const [loading, setLoading] = useState(false)
|
|
7
|
-
const [error, setError] = useState(null)
|
|
8
|
-
const roomRef = useRef<RoomModel | null>(null)
|
|
9
|
-
const recipientsRef = useRef<string[] | null>(null)
|
|
10
|
-
|
|
11
|
-
useEffect(() => {
|
|
12
|
-
const start = async () => {
|
|
13
|
-
if (loading && roomRef.current && recipientsRef.current) {
|
|
14
|
-
try {
|
|
15
|
-
roomRef.current = null
|
|
16
|
-
recipientsRef.current = null
|
|
17
|
-
await new Room(roomRef.current).create()
|
|
18
|
-
} catch (e) {
|
|
19
|
-
console.error('useCreateRoom hook error:', e)
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
start()
|
|
24
|
-
}, [loading])
|
|
25
|
-
|
|
26
|
-
const create = (room: RoomModel) => {
|
|
27
|
-
roomRef.current = room
|
|
28
|
-
setLoading(true)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return { create, loading, error }
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export default useCreateRoom
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { useRef, MutableRefObject } from 'react'
|
|
2
|
-
import { RoomCreatorRef } from '../../components/RoomCreator/types'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* React hook for managing RoomCreator component in usechat
|
|
6
|
-
*
|
|
7
|
-
* @returns An array with a ref and then convenience functions for the creator
|
|
8
|
-
* [ref, { open, close }]
|
|
9
|
-
*
|
|
10
|
-
* **Required** In order for the methods to work, you must pass creator to sheetRef prop.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```javascript
|
|
14
|
-
* export default () => {
|
|
15
|
-
* const [creator, { open, close }]
|
|
16
|
-
*
|
|
17
|
-
* // ...some other method
|
|
18
|
-
* const onPress = () => open()
|
|
19
|
-
*
|
|
20
|
-
* return <RoomCreator sheetRef={creator} />
|
|
21
|
-
* }
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
export default (): [
|
|
25
|
-
MutableRefObject<RoomCreatorRef | undefined>,
|
|
26
|
-
{ open: () => void; close: () => void }
|
|
27
|
-
] => {
|
|
28
|
-
const creator = useRef<RoomCreatorRef>()
|
|
29
|
-
const open = () => creator.current && creator.current.open()
|
|
30
|
-
const close = () => creator.current && creator.current.close()
|
|
31
|
-
|
|
32
|
-
return [creator, { open, close }]
|
|
33
|
-
}
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { useFuego } from '@nandorojo/fuego'
|
|
2
|
-
import { useMemo } from 'react'
|
|
3
|
-
import { WhereFilterOp } from '@firebase/firestore-types'
|
|
4
|
-
import { QueryDataHandler } from '@nandorojo/fuego/lib/typescript/hooks/useFuego/types'
|
|
5
|
-
import { OrderByItem } from '@nandorojo/fuego/lib/typescript/FuegoQuery/types'
|
|
6
|
-
import { fuego } from '../../components/ChatProvider/index'
|
|
7
|
-
import { chat } from '../../components/ChatProvider'
|
|
8
|
-
import { ExistingRoomDocument } from '../../classes/Room/types'
|
|
9
|
-
import { InboxModel } from '../../classes/Inbox/types'
|
|
10
|
-
import { UseInboxConfig } from './types'
|
|
11
|
-
import { Room } from '../..'
|
|
12
|
-
|
|
13
|
-
const orderBy: OrderByItem = ['lastMessage.createdAt', 'desc']
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* React hook to query the chat inbox in realtime. Used with the [Juice.Inbox] component from Juice.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```
|
|
20
|
-
* const { data } = useInbox()
|
|
21
|
-
*
|
|
22
|
-
* return <Juice.Inbox rooms={data} />
|
|
23
|
-
* ```
|
|
24
|
-
*
|
|
25
|
-
* @returns
|
|
26
|
-
* { data, loading, path, error } and other values returned by useFuego() hook. Data is properly formatted for rendering.
|
|
27
|
-
*/
|
|
28
|
-
export default (
|
|
29
|
-
config: UseInboxConfig = {},
|
|
30
|
-
options: QueryDataHandler<ExistingRoomDocument> = {}
|
|
31
|
-
) => {
|
|
32
|
-
const currentUser = fuego.auth().currentUser as firebase.User
|
|
33
|
-
const uid = config.uid || (currentUser && currentUser.uid)
|
|
34
|
-
|
|
35
|
-
const where: [string, WhereFilterOp, string] = useMemo(() => {
|
|
36
|
-
// useMemo to avoid re-making the array every render,
|
|
37
|
-
// since that would refresh the useFuego each time
|
|
38
|
-
return ['currentMembers', 'array-contains' as WhereFilterOp, uid]
|
|
39
|
-
// if the uid updates, then it recreates
|
|
40
|
-
// re: [uid]
|
|
41
|
-
}, [uid])
|
|
42
|
-
|
|
43
|
-
const path = chat.roomsCollection
|
|
44
|
-
const query = useMemo(
|
|
45
|
-
() => ({
|
|
46
|
-
listen: true,
|
|
47
|
-
where,
|
|
48
|
-
orderBy,
|
|
49
|
-
...config,
|
|
50
|
-
path
|
|
51
|
-
}),
|
|
52
|
-
[config, where, path]
|
|
53
|
-
)
|
|
54
|
-
const { data, ...response } = useFuego<ExistingRoomDocument>(query, options)
|
|
55
|
-
const rooms = useMemo(() => {
|
|
56
|
-
function isListOfRoomDocs(
|
|
57
|
-
inbox: typeof data
|
|
58
|
-
): inbox is ExistingRoomDocument[] {
|
|
59
|
-
return !!(inbox as ExistingRoomDocument[])
|
|
60
|
-
}
|
|
61
|
-
if (data && isListOfRoomDocs(data)) {
|
|
62
|
-
const formatted: InboxModel = data.map(room => ({
|
|
63
|
-
...room,
|
|
64
|
-
lastMessage: {
|
|
65
|
-
...room.lastMessage,
|
|
66
|
-
createdAt: room.lastMessage.createdAt.toDate()
|
|
67
|
-
},
|
|
68
|
-
unreadCount: new Room(room).getUnreadCount()
|
|
69
|
-
}))
|
|
70
|
-
return formatted
|
|
71
|
-
}
|
|
72
|
-
return []
|
|
73
|
-
}, [data])
|
|
74
|
-
|
|
75
|
-
return { data: rooms, ...response, path }
|
|
76
|
-
}
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { useMemo, useCallback, useState, useEffect, useRef } from 'react'
|
|
2
|
-
import { useFuego } from '@nandorojo/fuego'
|
|
3
|
-
import { UseMessagesConfig } from './types'
|
|
4
|
-
import Room from '../../classes/Room'
|
|
5
|
-
import {
|
|
6
|
-
NewMessageModel,
|
|
7
|
-
EditMessageModel,
|
|
8
|
-
MessageModelDocument,
|
|
9
|
-
} from '../../classes/Message/types'
|
|
10
|
-
import Message from '../../classes/Message'
|
|
11
|
-
import {
|
|
12
|
-
UseQueryConfig,
|
|
13
|
-
QueryDataHandler,
|
|
14
|
-
} from '@nandorojo/fuego/lib/typescript/hooks/useFuego/types'
|
|
15
|
-
|
|
16
|
-
const LIMIT = 30
|
|
17
|
-
|
|
18
|
-
const orderBy: UseQueryConfig['orderBy'] = ['createdAt', 'desc']
|
|
19
|
-
/**
|
|
20
|
-
* React hook to set up a lisener for a room's messages
|
|
21
|
-
*
|
|
22
|
-
* @param { id } the roomId, along with other query options from `useFuego` hook
|
|
23
|
-
*
|
|
24
|
-
* @returns { data: formattedMessages, loading, error, ref, path, empty, send, edit, fetchMore }
|
|
25
|
-
* data is formatted for rendering.
|
|
26
|
-
*/
|
|
27
|
-
export default (
|
|
28
|
-
{ id, ...config }: UseMessagesConfig,
|
|
29
|
-
options: QueryDataHandler<MessageModelDocument> = {}
|
|
30
|
-
) => {
|
|
31
|
-
const { messagesPath: path } = new Room({ id })
|
|
32
|
-
const [limit, setLimit] = useState(LIMIT)
|
|
33
|
-
const currentLimit = useRef(limit)
|
|
34
|
-
const latestFetchMoreId = useRef<null | string>(null)
|
|
35
|
-
const query = useMemo(
|
|
36
|
-
() => ({
|
|
37
|
-
listen: true,
|
|
38
|
-
orderBy,
|
|
39
|
-
...config,
|
|
40
|
-
path,
|
|
41
|
-
limit,
|
|
42
|
-
}),
|
|
43
|
-
[path, config, limit]
|
|
44
|
-
)
|
|
45
|
-
useEffect(() => {
|
|
46
|
-
currentLimit.current = limit
|
|
47
|
-
}, [limit])
|
|
48
|
-
const { data, loading, error, ref } = useFuego<MessageModelDocument>(
|
|
49
|
-
query,
|
|
50
|
-
options
|
|
51
|
-
)
|
|
52
|
-
const send = useCallback(
|
|
53
|
-
(message: NewMessageModel[]) => {
|
|
54
|
-
return new Message(message as NewMessageModel[]).formatForSend().send(id)
|
|
55
|
-
},
|
|
56
|
-
[id]
|
|
57
|
-
)
|
|
58
|
-
const edit = useCallback(
|
|
59
|
-
(message: EditMessageModel) => {
|
|
60
|
-
return new Message(message).edit(id)
|
|
61
|
-
},
|
|
62
|
-
[id]
|
|
63
|
-
)
|
|
64
|
-
const formattedMessages: NewMessageModel[] = useMemo(() => {
|
|
65
|
-
function isArrayOfMessageDocuments(
|
|
66
|
-
m: typeof data
|
|
67
|
-
): m is MessageModelDocument[] {
|
|
68
|
-
return !!(m as MessageModelDocument[])
|
|
69
|
-
}
|
|
70
|
-
if (data && isArrayOfMessageDocuments(data)) {
|
|
71
|
-
const formatted: NewMessageModel[] = new Message(
|
|
72
|
-
data
|
|
73
|
-
).formatForRender() as NewMessageModel[]
|
|
74
|
-
return formatted
|
|
75
|
-
}
|
|
76
|
-
return []
|
|
77
|
-
}, [data])
|
|
78
|
-
const fetchMore = () => {
|
|
79
|
-
const lastMessageId =
|
|
80
|
-
formattedMessages[formattedMessages.length - 1] &&
|
|
81
|
-
formattedMessages[formattedMessages.length - 1].id
|
|
82
|
-
// make sure we don't fetch more if the latest message is unchanged, or if it doesn't even exist
|
|
83
|
-
if (lastMessageId && lastMessageId !== latestFetchMoreId.current) {
|
|
84
|
-
latestFetchMoreId.current = lastMessageId
|
|
85
|
-
setLimit(l => l + LIMIT)
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
const empty: boolean = useMemo(
|
|
89
|
-
() => !(formattedMessages && formattedMessages.length),
|
|
90
|
-
[formattedMessages]
|
|
91
|
-
)
|
|
92
|
-
|
|
93
|
-
const fetchingMore = loading && formattedMessages.length
|
|
94
|
-
|
|
95
|
-
// TODO add reply to message function
|
|
96
|
-
|
|
97
|
-
return {
|
|
98
|
-
data: formattedMessages,
|
|
99
|
-
loading,
|
|
100
|
-
error,
|
|
101
|
-
ref,
|
|
102
|
-
path,
|
|
103
|
-
empty,
|
|
104
|
-
send,
|
|
105
|
-
edit,
|
|
106
|
-
fetchMore,
|
|
107
|
-
fetchingMore,
|
|
108
|
-
}
|
|
109
|
-
}
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { useFuego } from '@nandorojo/fuego'
|
|
2
|
-
import Room from '../../classes/Room'
|
|
3
|
-
import {
|
|
4
|
-
EditRoomModel,
|
|
5
|
-
RoomPropertiesModel,
|
|
6
|
-
CreateCallableRoomModel,
|
|
7
|
-
ExistingRoomDocument,
|
|
8
|
-
ExistingRoomModel
|
|
9
|
-
} from '../../classes/Room/types'
|
|
10
|
-
import { UseRoomType } from './types'
|
|
11
|
-
import { QueryDataHandler } from '@nandorojo/fuego/lib/typescript/hooks/useFuego/types'
|
|
12
|
-
import { useMemo } from 'react'
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Fetches or subscribes to a chatroom. It automatically handles unmounts, etc.
|
|
16
|
-
*
|
|
17
|
-
* @param config - config dictionary.
|
|
18
|
-
* @param config.id - Required: the unique ID for this chatroom, typically generated by the backend. If using the [[useInbox]] hook, each item in [[useInbox]]'s data array has this ID.
|
|
19
|
-
* @param config.listen Boolean to determine if this room should update state as a subscription. Default false.
|
|
20
|
-
* @param options - added options dictionary for useRoom
|
|
21
|
-
*
|
|
22
|
-
* @remarks Look at the fuego library's useFuego hook to see all the available arguments for config and options.
|
|
23
|
-
*
|
|
24
|
-
* @returns { data: room, loading, error, ref, create, edit, usersAreTyping, namesOfUsersTyping } data is formatted for rendering room.
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
export default (
|
|
28
|
-
{ id, ...config }: UseRoomType,
|
|
29
|
-
options: QueryDataHandler<ExistingRoomDocument> = {}
|
|
30
|
-
) => {
|
|
31
|
-
const { path } = new Room({ id } as RoomPropertiesModel)
|
|
32
|
-
const { data, loading, error, ref } = useFuego<ExistingRoomDocument>(
|
|
33
|
-
{
|
|
34
|
-
...config,
|
|
35
|
-
path
|
|
36
|
-
},
|
|
37
|
-
options
|
|
38
|
-
)
|
|
39
|
-
const create = (room: CreateCallableRoomModel) => {
|
|
40
|
-
return new Room(room).createViaCallable()
|
|
41
|
-
}
|
|
42
|
-
const edit = (room: EditRoomModel) => {
|
|
43
|
-
return new Room(room).edit()
|
|
44
|
-
}
|
|
45
|
-
const members = data && (data as ExistingRoomDocument).members
|
|
46
|
-
const usersAreTyping = useMemo(() => {
|
|
47
|
-
return !!(members && new Room({ members }).getUsersTyping().length)
|
|
48
|
-
}, [members])
|
|
49
|
-
const namesOfUsersTyping = useMemo(() => {
|
|
50
|
-
return (
|
|
51
|
-
(members &&
|
|
52
|
-
new Room({ members }).getUsersTyping().map(user => user.name)) ||
|
|
53
|
-
[]
|
|
54
|
-
)
|
|
55
|
-
}, [members])
|
|
56
|
-
const room = useMemo(() => {
|
|
57
|
-
function isDocument(r: typeof data): r is ExistingRoomDocument {
|
|
58
|
-
return !!(r as ExistingRoomDocument)
|
|
59
|
-
}
|
|
60
|
-
if (data && isDocument(data)) {
|
|
61
|
-
const model: ExistingRoomModel = {
|
|
62
|
-
...data,
|
|
63
|
-
lastMessage: {
|
|
64
|
-
...data.lastMessage,
|
|
65
|
-
createdAt: data.lastMessage.createdAt.toDate()
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
return model
|
|
69
|
-
}
|
|
70
|
-
return null
|
|
71
|
-
}, [data])
|
|
72
|
-
return {
|
|
73
|
-
data: room,
|
|
74
|
-
loading,
|
|
75
|
-
error,
|
|
76
|
-
ref,
|
|
77
|
-
create,
|
|
78
|
-
edit,
|
|
79
|
-
usersAreTyping,
|
|
80
|
-
namesOfUsersTyping
|
|
81
|
-
}
|
|
82
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { useState, useRef, useEffect, useCallback } from 'react'
|
|
2
|
-
import { NewMessageModel } from '../../../classes/Message/types'
|
|
3
|
-
import Message from '../../../classes/Message'
|
|
4
|
-
|
|
5
|
-
export default function<MessageModel extends NewMessageModel>() {
|
|
6
|
-
const [uploading, setUploading] = useState(false)
|
|
7
|
-
const [error, setError] = useState(null)
|
|
8
|
-
const messageRef = useRef<MessageModel | null>(null)
|
|
9
|
-
const recipientsRef = useRef<string[] | null>(null)
|
|
10
|
-
|
|
11
|
-
useEffect(() => {
|
|
12
|
-
if (uploading) {
|
|
13
|
-
const run = async () => {
|
|
14
|
-
try {
|
|
15
|
-
if (messageRef.current && recipientsRef.current)
|
|
16
|
-
await new Message(messageRef.current).sendToGroup(
|
|
17
|
-
recipientsRef.current
|
|
18
|
-
)
|
|
19
|
-
messageRef.current = null
|
|
20
|
-
recipientsRef.current = null
|
|
21
|
-
setUploading(false)
|
|
22
|
-
} catch (e) {
|
|
23
|
-
console.error(`failure sending message from useSendToPeople`, e)
|
|
24
|
-
setError(e)
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
run()
|
|
28
|
-
}
|
|
29
|
-
}, [uploading])
|
|
30
|
-
|
|
31
|
-
const send = useCallback((message: MessageModel, recipients: string[]) => {
|
|
32
|
-
messageRef.current = message
|
|
33
|
-
recipientsRef.current = recipients
|
|
34
|
-
setUploading(true)
|
|
35
|
-
}, [])
|
|
36
|
-
|
|
37
|
-
return { send, uploading, error }
|
|
38
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { useState, useRef, useEffect, useCallback } from 'react'
|
|
2
|
-
import { NewMessageModel } from '../../../classes/Message/types'
|
|
3
|
-
import Message from '../../../classes/Message'
|
|
4
|
-
|
|
5
|
-
export default function<MessageModel extends NewMessageModel>() {
|
|
6
|
-
const [uploading, setUploading] = useState(false)
|
|
7
|
-
const [error, setError] = useState(null)
|
|
8
|
-
const messageRef = useRef<MessageModel | null>(null)
|
|
9
|
-
const recipientsRef = useRef<string[] | null>(null)
|
|
10
|
-
|
|
11
|
-
useEffect(() => {
|
|
12
|
-
if (uploading) {
|
|
13
|
-
const run = async () => {
|
|
14
|
-
try {
|
|
15
|
-
if (messageRef.current && recipientsRef.current)
|
|
16
|
-
await new Message(messageRef.current).sendToPeople(
|
|
17
|
-
recipientsRef.current
|
|
18
|
-
)
|
|
19
|
-
messageRef.current = null
|
|
20
|
-
recipientsRef.current = null
|
|
21
|
-
setUploading(false)
|
|
22
|
-
} catch (e) {
|
|
23
|
-
console.error(`failure sending message from useSendToPeople`, e)
|
|
24
|
-
setError(e)
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
run()
|
|
28
|
-
}
|
|
29
|
-
}, [uploading])
|
|
30
|
-
|
|
31
|
-
const send = useCallback((message: MessageModel, recipients: string[]) => {
|
|
32
|
-
messageRef.current = message
|
|
33
|
-
recipientsRef.current = recipients
|
|
34
|
-
setUploading(true)
|
|
35
|
-
}, [])
|
|
36
|
-
|
|
37
|
-
return { send, uploading, error }
|
|
38
|
-
}
|
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
import { useEffect, useState, useMemo, useRef } from 'react'
|
|
2
|
-
import { useFuegoContext } from '@nandorojo/fuego'
|
|
3
|
-
import { Typing } from '../../classes/Room/types'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* useTyping automatically updates the backend whenever a user is typing.
|
|
7
|
-
*
|
|
8
|
-
* @param options - A dictionary to configure the typing.
|
|
9
|
-
* @param options.roomId - **Required** unique id of the room you're typing in.
|
|
10
|
-
* @param options.text - **Required** The current text state of your chat input
|
|
11
|
-
* @param options.delay (Optional) Set the timeout delay to mark typing false. Set null if you don't want a time out.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```javascript
|
|
15
|
-
* // input text state
|
|
16
|
-
* const [text, setText] = useState('')
|
|
17
|
-
* // let's say we're getting our room id from our react-navigation params
|
|
18
|
-
* const navigation = useNavigation()
|
|
19
|
-
* const roomId = navigation.params.id
|
|
20
|
-
*
|
|
21
|
-
* useTyping({ roomId, text })
|
|
22
|
-
*
|
|
23
|
-
* <ChatComponent text={text} onChangeText={setText} />
|
|
24
|
-
* ```
|
|
25
|
-
*
|
|
26
|
-
* @returns
|
|
27
|
-
* Doesn't return anything. We live in an undefined world, I suppose.
|
|
28
|
-
*
|
|
29
|
-
* @remarks
|
|
30
|
-
* The actual data of who is typing is stored in the chatroom object.
|
|
31
|
-
* If you want to get this data realtime in a room, consider using useRoom({ listen: true }). This data will also be in useInbox().
|
|
32
|
-
* See docs for useRoom or useInbox for more.
|
|
33
|
-
*
|
|
34
|
-
*/
|
|
35
|
-
export default ({
|
|
36
|
-
roomId,
|
|
37
|
-
text,
|
|
38
|
-
delay = 5000
|
|
39
|
-
}: {
|
|
40
|
-
roomId: string
|
|
41
|
-
text: string
|
|
42
|
-
delay?: number | null
|
|
43
|
-
}) => {
|
|
44
|
-
const { auth, firebase } = useFuegoContext()
|
|
45
|
-
const [ready, setReady] = useState(false)
|
|
46
|
-
const { currentUser } = auth()
|
|
47
|
-
try {
|
|
48
|
-
if (!currentUser)
|
|
49
|
-
throw new Error(
|
|
50
|
-
'🤯 error in useTyping hook. Looks like there is not a valid user signed in. \nConsider using the FuegoGate component from @nandorojo/fuego and wrapping it around your app.'
|
|
51
|
-
)
|
|
52
|
-
} catch (e) {
|
|
53
|
-
console.error(e)
|
|
54
|
-
}
|
|
55
|
-
const { uid } = currentUser as firebase.User
|
|
56
|
-
const databaseRef = useMemo(() => firebase.database().ref(`/typing/${uid}`), [
|
|
57
|
-
uid,
|
|
58
|
-
firebase
|
|
59
|
-
])
|
|
60
|
-
const previouslyTyped = useRef('')
|
|
61
|
-
|
|
62
|
-
const timer = useRef<NodeJS.Timeout>()
|
|
63
|
-
|
|
64
|
-
const lastStateSent = useRef<IsTyping['state']>('nothing')
|
|
65
|
-
|
|
66
|
-
// initialize the typing set to false when we disconnect
|
|
67
|
-
// once that is set up, then we can mark that we are indeed typing elsewhere
|
|
68
|
-
// ^ that will be done in the next hook
|
|
69
|
-
useEffect(() => {
|
|
70
|
-
const subscriptionRef = firebase.database().ref('.info/connected')
|
|
71
|
-
const notTyping: IsTyping = {
|
|
72
|
-
state: 'nothing',
|
|
73
|
-
lastChanged: firebase.database.ServerValue.TIMESTAMP,
|
|
74
|
-
roomId
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
subscriptionRef.on('value', async snapshot => {
|
|
78
|
-
if (!snapshot.val()) return
|
|
79
|
-
|
|
80
|
-
try {
|
|
81
|
-
await databaseRef.onDisconnect().set(notTyping)
|
|
82
|
-
setReady(true)
|
|
83
|
-
} catch (e) {
|
|
84
|
-
console.error(`useTyping error within snapshot listener ${e}`)
|
|
85
|
-
}
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
return () => {
|
|
89
|
-
// clean up the listener on unmount
|
|
90
|
-
subscriptionRef.off('value')
|
|
91
|
-
;(async () => {
|
|
92
|
-
try {
|
|
93
|
-
lastStateSent.current = notTyping.state
|
|
94
|
-
await databaseRef.set(notTyping)
|
|
95
|
-
} catch (e) {
|
|
96
|
-
console.error('useTyping errored on unmount setting not typing')
|
|
97
|
-
}
|
|
98
|
-
})()
|
|
99
|
-
}
|
|
100
|
-
}, [roomId, databaseRef, firebase])
|
|
101
|
-
|
|
102
|
-
// every time we type something new, upload that we're typing
|
|
103
|
-
// and after a certain delay, we mark typing false
|
|
104
|
-
// however, if we type before that timer has completed,
|
|
105
|
-
// we clean it up and start it over
|
|
106
|
-
useEffect(() => {
|
|
107
|
-
try {
|
|
108
|
-
// we're ready to upload from the previous effect
|
|
109
|
-
// ...and the text we typed just changed.
|
|
110
|
-
// if text is empty, set to false
|
|
111
|
-
if (ready && text !== previouslyTyped.current) {
|
|
112
|
-
previouslyTyped.current = text
|
|
113
|
-
|
|
114
|
-
const notTyping: IsTyping = {
|
|
115
|
-
state: 'nothing',
|
|
116
|
-
lastChanged: firebase.database.ServerValue.TIMESTAMP,
|
|
117
|
-
roomId
|
|
118
|
-
}
|
|
119
|
-
const isTyping: IsTyping = {
|
|
120
|
-
state: 'typing',
|
|
121
|
-
lastChanged: firebase.database.ServerValue.TIMESTAMP,
|
|
122
|
-
roomId
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// if the text is empty, we set it to not typing
|
|
126
|
-
const setValue = text ? isTyping : notTyping
|
|
127
|
-
|
|
128
|
-
// don't resend the state we just sent to avoid redundancy
|
|
129
|
-
if (setValue.state !== lastStateSent.current) {
|
|
130
|
-
lastStateSent.current = setValue.state
|
|
131
|
-
databaseRef.set(setValue)
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// only need a notTyping timer timer if we're typing
|
|
135
|
-
if (delay !== null && setValue.state === 'typing') {
|
|
136
|
-
timer.current = setTimeout(() => {
|
|
137
|
-
lastStateSent.current = notTyping.state
|
|
138
|
-
databaseRef.set(notTyping)
|
|
139
|
-
}, delay)
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
} catch (e) {
|
|
143
|
-
console.error('error in useTyping after timer', e)
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return () => {
|
|
147
|
-
if (timer.current) clearTimeout(timer.current)
|
|
148
|
-
}
|
|
149
|
-
}, [ready, text, roomId, delay, uid, databaseRef, firebase])
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
interface IsTyping extends Typing {
|
|
153
|
-
roomId: string
|
|
154
|
-
}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { useEffect, useRef } from 'react'
|
|
2
|
-
import { Room } from '../../index'
|
|
3
|
-
import { fuego } from '../../components/ChatProvider'
|
|
4
|
-
import { UseTypingConfig } from './types'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* useTyping updates the backend every time a user types, setting it to false after a certain delay.
|
|
8
|
-
*
|
|
9
|
-
* @param config { text, id } A dictionary that takes in the Input's current text value and the chatroom ID.
|
|
10
|
-
* @param delay How long the timer delay should be before the backend is told we're no longer typing.
|
|
11
|
-
*
|
|
12
|
-
* @returns undefined
|
|
13
|
-
*/
|
|
14
|
-
export default (config: UseTypingConfig, delay: number = 1500) => {
|
|
15
|
-
const { text = '', id } = config
|
|
16
|
-
const { path } = new Room({ id })
|
|
17
|
-
const ref = fuego.db.doc(path)
|
|
18
|
-
const timer = useRef<number>()
|
|
19
|
-
const user = fuego.auth().currentUser as firebase.User
|
|
20
|
-
const uid = (user && user.uid) || 'tester_name'
|
|
21
|
-
// ensures that typing only happens on update, not on mount
|
|
22
|
-
const mounted = useRef(false)
|
|
23
|
-
const lastTyped = useRef(0)
|
|
24
|
-
|
|
25
|
-
useEffect(() => {
|
|
26
|
-
try {
|
|
27
|
-
if (mounted.current) {
|
|
28
|
-
const typedAt = Date.now()
|
|
29
|
-
// const last = lastTyped.current
|
|
30
|
-
lastTyped.current = typedAt
|
|
31
|
-
// if ((typedAt - last) / 1000 >= delay) {
|
|
32
|
-
ref.update({
|
|
33
|
-
[`members.${uid}.typing`]: true
|
|
34
|
-
// [`members.${uid}.lastTyped`]: new Date()
|
|
35
|
-
})
|
|
36
|
-
timer.current = setTimeout(() => {
|
|
37
|
-
ref.update({
|
|
38
|
-
[`members.${uid}.typing`]: false
|
|
39
|
-
})
|
|
40
|
-
}, delay)
|
|
41
|
-
// }
|
|
42
|
-
} else {
|
|
43
|
-
mounted.current = true
|
|
44
|
-
}
|
|
45
|
-
} catch (e) {
|
|
46
|
-
console.error('use typing update error', e)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return () => {
|
|
50
|
-
// cleanup on update
|
|
51
|
-
if (timer.current) clearTimeout(timer.current)
|
|
52
|
-
}
|
|
53
|
-
}, [text, uid, delay, path])
|
|
54
|
-
|
|
55
|
-
useEffect(() => {
|
|
56
|
-
return () => {
|
|
57
|
-
try {
|
|
58
|
-
// mark indicator as false
|
|
59
|
-
ref.update({
|
|
60
|
-
[`members.${uid}.typing`]: false
|
|
61
|
-
})
|
|
62
|
-
} catch (e) {
|
|
63
|
-
console.error('unmount usetyping error', e)
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}, [])
|
|
67
|
-
}
|