Skip to content

Commit a46b87f

Browse files
- Restructure to make the imports seperate for node and react
- Migrate useUsers - Migrate useRoom - Migrate useFirebaseUser
1 parent 8061aee commit a46b87f

File tree

11 files changed

+156
-157
lines changed

11 files changed

+156
-157
lines changed

src/index.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1 @@
1-
// React
2-
export * from './types'
3-
export * from './useFirebaseUser'
4-
export * from './useMessages'
5-
export * from './useRoom'
6-
export * from './useRooms'
7-
export * from './useUsers'
8-
export * from './utils'
9-
10-
// Nodejs
1+
export * from './react';

src/react/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export * from './types'
2+
export * from './useFirebaseUser'
3+
export * from './useMessages'
4+
export * from './useRoom'
5+
export * from './useRooms'
6+
export * from './useUsers'
7+
export * from './utils'

src/types.ts renamed to src/react/types.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
* for rooms and users collections. Call `setConfig` before doing anything else if
33
* you want to change the default collection names. When using custom names don't forget
44
* to update your security rules and indexes. */
5-
export interface FirebaseChatCoreConfig {
5+
export interface IFirebaseChatCoreConfig {
66
roomsCollectionName: string
77
usersCollectionName: string
88
}
99

1010
export namespace MessageType {
11-
export type Any = Custom | File | Image | Text | Unsupported
11+
export type Any = ICustom | IFile | IImage | IText | IUnsupported
1212
export type PartialAny =
13-
| PartialCustom
14-
| PartialFile
15-
| PartialImage
16-
| PartialText
13+
| IPartialCustom
14+
| IPartialFile
15+
| IPartialImage
16+
| IPartialText
1717

18-
interface Base {
19-
author: User
18+
interface IBase {
19+
author: IUser
2020
createdAt?: number
2121
id: string
2222
metadata?: Record<string, any>
@@ -26,16 +26,16 @@ export namespace MessageType {
2626
updatedAt?: number
2727
}
2828

29-
export interface PartialCustom extends Base {
29+
export interface IPartialCustom extends IBase {
3030
metadata?: Record<string, any>
3131
type: 'custom'
3232
}
3333

34-
export interface Custom extends Base, PartialCustom {
34+
export interface ICustom extends IBase, IPartialCustom {
3535
type: 'custom'
3636
}
3737

38-
export interface PartialFile {
38+
export interface IPartialFile {
3939
metadata?: Record<string, any>
4040
mimeType?: string
4141
name: string
@@ -44,11 +44,11 @@ export namespace MessageType {
4444
uri: string
4545
}
4646

47-
export interface File extends Base, PartialFile {
47+
export interface IFile extends IBase, IPartialFile {
4848
type: 'file'
4949
}
5050

51-
export interface PartialImage {
51+
export interface IPartialImage {
5252
height?: number
5353
metadata?: Record<string, any>
5454
name: string
@@ -58,40 +58,40 @@ export namespace MessageType {
5858
width?: number
5959
}
6060

61-
export interface Image extends Base, PartialImage {
61+
export interface IImage extends IBase, IPartialImage {
6262
type: 'image'
6363
}
6464

65-
export interface PartialText {
65+
export interface IPartialText {
6666
metadata?: Record<string, any>
67-
previewData?: PreviewData
67+
previewData?: IPreviewData
6868
text: string
6969
type: 'text'
7070
}
7171

72-
export interface Text extends Base, PartialText {
72+
export interface IText extends IBase, IPartialText {
7373
type: 'text'
7474
}
7575

76-
export interface Unsupported extends Base {
76+
export interface IUnsupported extends IBase {
7777
type: 'unsupported'
7878
}
7979
}
8080

81-
export interface PreviewData {
81+
export interface IPreviewData {
8282
description?: string
83-
image?: PreviewDataImage
83+
image?: IPreviewDataImage
8484
link?: string
8585
title?: string
8686
}
8787

88-
export interface PreviewDataImage {
88+
export interface IPreviewDataImage {
8989
height: number
9090
url: string
9191
width: number
9292
}
9393

94-
export interface Room {
94+
export interface IRoom {
9595
createdAt?: number
9696
id: string
9797
imageUrl?: string
@@ -100,10 +100,10 @@ export interface Room {
100100
name?: string
101101
type: 'channel' | 'direct' | 'group' | 'unsupported'
102102
updatedAt?: number
103-
users: User[]
103+
users: IUser[]
104104
}
105105

106-
export interface User {
106+
export interface IUser {
107107
createdAt?: number
108108
firstName?: string
109109
id: string
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import auth, { FirebaseAuthTypes } from '@react-native-firebase/auth'
2-
import * as React from 'react'
1+
import { getAuth, onAuthStateChanged, User } from 'firebase/auth';
2+
import React from 'react';
33

44
export const useFirebaseUser = () => {
5+
const auth = getAuth();
56
const [firebaseUser, setFirebaseUser] = React.useState<
6-
FirebaseAuthTypes.User | undefined
7+
User | undefined
78
>()
89

910
React.useEffect(() => {
10-
return auth().onAuthStateChanged((user) => {
11+
return onAuthStateChanged(auth, (user) => {
1112
setFirebaseUser(user ?? undefined)
1213
})
1314
})
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import firestore from '@react-native-firebase/firestore'
2-
import * as React from 'react'
1+
import firestore from '@react-native-firebase/firestore';
2+
import * as React from 'react';
33

4-
import { MessageType, Room } from './types'
5-
import { useFirebaseUser } from './useFirebaseUser'
4+
import { IRoom, MessageType } from './types';
5+
import { useFirebaseUser } from './useFirebaseUser';
66

77
/** Returns a stream of messages from Firebase for a given room */
8-
export const useMessages = (room: Room) => {
8+
export const useMessages = (room: IRoom) => {
99
const [messages, setMessages] = React.useState<MessageType.Any[]>([])
1010
const { firebaseUser } = useFirebaseUser()
1111

src/react/useRoom.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { collection, doc, Firestore, onSnapshot } from 'firebase/firestore';
2+
import React from 'react';
3+
4+
import { ROOMS_COLLECTION_NAME } from '.';
5+
import { IRoom } from './types';
6+
import { useFirebaseUser } from './useFirebaseUser';
7+
import { processRoomDocument } from './utils';
8+
9+
/** Returns a stream of changes in a room from Firebase */
10+
export const useRoom = (initialRoom: IRoom, db: Firestore) => {
11+
const [room, setRoom] = React.useState(initialRoom)
12+
const { firebaseUser } = useFirebaseUser()
13+
14+
React.useEffect(() => {
15+
if (!firebaseUser) return
16+
17+
const _collection = collection(db, ROOMS_COLLECTION_NAME);
18+
const _doc = doc(_collection, initialRoom.id);
19+
const _onSnapshot = onSnapshot(_doc, async (__doc) => {
20+
const newRoom = await processRoomDocument({ _doc: __doc, firebaseUser, db })
21+
22+
setRoom(newRoom)
23+
})
24+
return _onSnapshot;
25+
}, [firebaseUser, initialRoom.id])
26+
27+
return { room }
28+
}

src/useRooms.ts renamed to src/react/useRooms.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import firestore from '@react-native-firebase/firestore'
2-
import * as React from 'react'
1+
import firestore from '@react-native-firebase/firestore';
2+
import * as React from 'react';
33

4-
import { ROOMS_COLLECTION_NAME } from '.'
5-
import { Room, User } from './types'
6-
import { useFirebaseUser } from './useFirebaseUser'
7-
import { fetchUser, processRoomsQuery } from './utils'
4+
import { ROOMS_COLLECTION_NAME } from '.';
5+
import { IRoom, IUser } from './types';
6+
import { useFirebaseUser } from './useFirebaseUser';
7+
import { fetchUser, processRoomsQuery } from './utils';
88

99
/** Returns a stream of rooms from Firebase. Only rooms where current
1010
* logged in user exist are returned. `orderByUpdatedAt` is used in case
@@ -17,7 +17,7 @@ import { fetchUser, processRoomsQuery } from './utils'
1717
* is `rooms`, field indexed are `userIds` (type Arrays) and `updatedAt`
1818
* (type Descending), query scope is `Collection` */
1919
export const useRooms = (orderByUpdatedAt?: boolean) => {
20-
const [rooms, setRooms] = React.useState<Room[]>([])
20+
const [rooms, setRooms] = React.useState<IRoom[]>([])
2121
const { firebaseUser } = useFirebaseUser()
2222

2323
React.useEffect(() => {
@@ -28,12 +28,12 @@ export const useRooms = (orderByUpdatedAt?: boolean) => {
2828

2929
const collection = orderByUpdatedAt
3030
? firestore()
31-
.collection(ROOMS_COLLECTION_NAME)
32-
.where('userIds', 'array-contains', firebaseUser.uid)
33-
.orderBy('updatedAt', 'desc')
31+
.collection(ROOMS_COLLECTION_NAME)
32+
.where('userIds', 'array-contains', firebaseUser.uid)
33+
.orderBy('updatedAt', 'desc')
3434
: firestore()
35-
.collection(ROOMS_COLLECTION_NAME)
36-
.where('userIds', 'array-contains', firebaseUser.uid)
35+
.collection(ROOMS_COLLECTION_NAME)
36+
.where('userIds', 'array-contains', firebaseUser.uid)
3737

3838
return collection.onSnapshot(async (query) => {
3939
const newRooms = await processRoomsQuery({ firebaseUser, query })
@@ -55,7 +55,7 @@ export const useRooms = (orderByUpdatedAt?: boolean) => {
5555
imageUrl?: string
5656
metadata?: Record<string, any>
5757
name: string
58-
users: User[]
58+
users: IUser[]
5959
}) => {
6060
if (!firebaseUser) return
6161

@@ -86,12 +86,12 @@ export const useRooms = (orderByUpdatedAt?: boolean) => {
8686
name,
8787
type: 'group',
8888
users: roomUsers,
89-
} as Room
89+
} as IRoom
9090
}
9191

9292
/** Creates a direct chat for 2 people. Add `metadata` for any additional custom data. */
9393
const createRoom = async (
94-
otherUser: User,
94+
otherUser: IUser,
9595
metadata?: Record<string, any>
9696
) => {
9797
if (!firebaseUser) return
@@ -138,7 +138,7 @@ export const useRooms = (orderByUpdatedAt?: boolean) => {
138138
metadata,
139139
type: 'direct',
140140
users,
141-
} as Room
141+
} as IRoom
142142
}
143143

144144
return { createGroupRoom, createRoom, rooms }

src/react/useUsers.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { collection, Firestore, onSnapshot } from 'firebase/firestore';
2+
import React from 'react';
3+
4+
import { USERS_COLLECTION_NAME } from '.';
5+
import { IUser } from './types';
6+
import { useFirebaseUser } from './useFirebaseUser';
7+
8+
/** Returns a stream of all users from Firebase */
9+
export const useUsers = (db: Firestore) => {
10+
const [users, setUsers] = React.useState<IUser[]>([])
11+
const { firebaseUser } = useFirebaseUser()
12+
13+
React.useEffect(() => {
14+
if (!firebaseUser) {
15+
setUsers([])
16+
return
17+
}
18+
19+
const _collection = collection(db, USERS_COLLECTION_NAME);
20+
const _onSnapshot = onSnapshot(_collection, (query) => {
21+
const newUsers: IUser[] = []
22+
23+
query?.forEach((doc) => {
24+
if (firebaseUser.uid === doc.id) return
25+
26+
const data = doc.data()!
27+
28+
const user: IUser = {
29+
// Ignore types here, not provided by the Firebase library
30+
// type-coverage:ignore-next-line
31+
createdAt: data.createdAt?.toMillis() ?? undefined,
32+
// type-coverage:ignore-next-line
33+
firstName: data.firstName ?? undefined,
34+
id: doc.id,
35+
// type-coverage:ignore-next-line
36+
imageUrl: data.imageUrl ?? undefined,
37+
// type-coverage:ignore-next-line
38+
lastName: data.lastName ?? undefined,
39+
// type-coverage:ignore-next-line
40+
lastSeen: data.lastSeen?.toMillis() ?? undefined,
41+
// type-coverage:ignore-next-line
42+
metadata: data.metadata ?? undefined,
43+
// type-coverage:ignore-next-line
44+
updatedAt: data.updatedAt?.toMillis() ?? undefined,
45+
}
46+
47+
newUsers.push(user)
48+
})
49+
50+
setUsers(newUsers)
51+
})
52+
53+
return _onSnapshot;
54+
}, [firebaseUser])
55+
56+
return { users }
57+
}

0 commit comments

Comments
 (0)