Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@ export const ChatScreen: React.FC = () => {

```

## Features

#### Leave conversation

- Use `useConversation` hook to get `leaveConversation` function.

```typescript
import {useConversation} from 'rn-firebase-chat';

const {leaveConversation} = useConversation();

const result = await leaveConversation(conversationId, isSilent);
```

| Parameter | Type | Description |
| :--------------- | :-------- | :------------------------------------------------------------------------ |
| `conversationId` | `string` | **Required** |
| `isSilent` | `boolean` | If `true`, send a system message to the conversation to notify the action |

## Contributing

See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
Expand Down
7 changes: 6 additions & 1 deletion src/chat/ChatProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
chatReducer,
setListConversation,
updateConversation,
updateListConversation,
} from '../reducer';

const firestoreServices = FirestoreServices.getInstance();
Expand Down Expand Up @@ -36,7 +37,11 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
dispatch(setListConversation(res));
});
unsubscribeListener = firestoreServices.listenConversationUpdate(
(data) => {
(data, type) => {
if (type === 'removed') {
dispatch(updateListConversation(data));
return;
}
dispatch(updateConversation(data));
}
);
Expand Down
9 changes: 8 additions & 1 deletion src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useCallback, useRef } from 'react';
import { useContext } from 'react';
import { ChatContext } from './chat';
import type { ChatState } from './reducer';
import { FirestoreServices } from './services/firebase';

const useChat = () => {
// const firebaseInstant
Expand Down Expand Up @@ -72,4 +73,10 @@ const useTypingIndicator = (
};
};

export { useChatContext, useChatSelector, useTypingIndicator };
const useConversation = () => {
const firebaseInstance = useRef(FirestoreServices.getInstance()).current;

return { leaveConversation: firebaseInstance.leaveConversation };
};

export { useChatContext, useChatSelector, useTypingIndicator, useConversation };
3 changes: 3 additions & 0 deletions src/interfaces/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface LatestMessageProps {
type?: MediaType;
path?: string;
extension?: string;
system?: boolean;
}

interface MessageProps extends BaseEntity, IMessage {
Expand All @@ -29,6 +30,7 @@ interface MessageProps extends BaseEntity, IMessage {
type?: MediaType;
path?: string;
extension?: string;
system?: boolean;
}

interface SendMessageProps {
Expand All @@ -42,6 +44,7 @@ interface SendMessageProps {
type?: MediaType;
path?: string;
extension?: string;
system?: boolean;
}

type MediaType = 'image' | 'video' | 'text' | undefined;
Expand Down
6 changes: 6 additions & 0 deletions src/reducer/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ export enum ChatActionKind {
SET_CONVERSATION = 'SET_CONVERSATION',
CLEAR_CONVERSATION = 'CLEAR_CONVERSATION',
UPDATE_CONVERSATION = 'UPDATE_CONVERSATION',
UPDATE_LIST_CONVERSATION = 'UPDATE_LIST_CONVERSATION',
}

export const setListConversation = (payload: ConversationProps[]) => ({
type: ChatActionKind.SET_LIST_CONVERSATION,
payload,
});

export const updateListConversation = (payload: ConversationProps) => ({
type: ChatActionKind.UPDATE_LIST_CONVERSATION,
payload,
});

export const setConversation = (payload: ConversationProps) => ({
type: ChatActionKind.SET_CONVERSATION,
payload,
Expand Down
17 changes: 16 additions & 1 deletion src/reducer/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ export const chatReducer = (
...state,
listConversation: action.payload as ConversationProps[],
};
case ChatActionKind.UPDATE_LIST_CONVERSATION: {
const message = action.payload as ConversationProps;
const listConversation = state.listConversation?.filter(
(e) => e.id !== message.id
);

return {
...state,
listConversation: listConversation as ConversationProps[],
};
}
case ChatActionKind.SET_CONVERSATION:
return {
...state,
Expand All @@ -31,7 +42,7 @@ export const chatReducer = (
...state,
conversation: undefined,
};
case ChatActionKind.UPDATE_CONVERSATION:
case ChatActionKind.UPDATE_CONVERSATION: {
const message = action.payload as ConversationProps;
const isExistID = state.listConversation?.some(
(item) => item.id === message.id
Expand All @@ -52,5 +63,9 @@ export const chatReducer = (
...state,
listConversation: newListConversation,
};
}

default:
return state;
}
};
Loading