Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: consistent name for room #599

Merged
merged 8 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
24 changes: 22 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"@types/socket.io-client": "^3.0.0",
"autoprefixer": "^10.0.1",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.53.0",
Expand Down Expand Up @@ -81,4 +82,4 @@
"*.{js,jsx,ts,tsx}": "eslint --fix",
"*.--write": "prettier --write ."
}
}
}
34 changes: 17 additions & 17 deletions src/components/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,23 @@ const Chat = () => {
inputRef.current.value = '';
setEditing({ isediting: false, messageID: '' });
socket?.timeout(10000).emit(events.NEW_EVENT_TYPING, {
chatId: app.currentChatId,
room: app.currentroom,
isTyping: false,
});
};

const sortedMessages = useMemo(() => {
if (!app.currentChatId) {
if (!app.currentroom) {
return;
}
return Object.values(state[app.currentChatId]?.messages ?? {})?.sort(
return Object.values(state[app.currentroom]?.messages ?? {})?.sort(
(a, b) => {
const da = new Date(a.time),
db = new Date(b.time);
return da.getTime() - db.getTime();
}
);
}, [state, app.currentChatId]);
}, [state, app.currentroom]);

const doSend = async ({
senderId,
Expand Down Expand Up @@ -181,7 +181,7 @@ const Chat = () => {
e.preventDefault();

socket?.emit(events.NEW_EVENT_TYPING, {
chatId: app.currentChatId,
room: app.currentroom,
isTyping: false,
});
const d = new Date();
Expand All @@ -197,14 +197,14 @@ const Chat = () => {
const oldMessage = messageObject?.message;
const editedMessage = await editMessage({
id: editing.messageID,
chatId: app.currentChatId,
room: app.currentroom,
newMessage: message as string,
oldMessage,
});

updateMessage({
...editedMessage,
room: app.currentChatId,
room: app.currentroom,
isEdited: true,
});
} catch (e) {
Expand All @@ -215,7 +215,7 @@ const Chat = () => {
} else {
doSend({
senderId,
room: app.currentChatId,
room: app.currentroom,
message: message as string,
time: d.getTime(),
containsBadword: badwords.check(message as string),
Expand All @@ -234,7 +234,7 @@ const Chat = () => {
const handleTypingStatus = throttle(e => {
if (e.target.value.length > 0) {
socket?.timeout(5000).emit(events.NEW_EVENT_TYPING, {
chatId: app.currentChatId,
room: app.currentroom,
isTyping: true,
});
}
Expand Down Expand Up @@ -292,18 +292,18 @@ const Chat = () => {

const deleteMessageHandler = ({
id,
chatId,
room,
}: {
id: string;
chatId: string;
room: string;
}) => {
removeMessage(id, chatId);
removeMessage(id, room);
};

const editMessageHandler = (messageEdited: MessageType) => {
updateMessage({
...messageEdited,
room: app.currentChatId,
room: app.currentroom,
isEdited: true,
});
};
Expand All @@ -314,12 +314,12 @@ const Chat = () => {

const readMessageHandler = ({
messageId,
chatId,
room,
}: {
messageId: string;
chatId: string;
room: string;
}) => {
receiveMessage(messageId, chatId);
receiveMessage(messageId, room);
};

// This is used to recive message form other user.
Expand All @@ -336,7 +336,7 @@ const Chat = () => {
socket?.off(events.NEW_EVENT_READ_MESSAGE, readMessageHandler);
socket?.off(events.NEW_EVENT_SEND_FAILED, limitMessageHandler);
};
}, [app.currentChatId, socket]);
}, [app.currentroom, socket]);

// this is used to send the notification for inactive chat to the respective user
// get the last message sent
Expand Down
2 changes: 1 addition & 1 deletion src/components/Chat/DropDownOption.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const DropDownOptions = ({
try {
const messageDeleted = await deleteMessage({
id,
chatId: gottenMessage.room as string,
room: gottenMessage.room as string,
});

console.log(messageDeleted);
Expand Down
14 changes: 7 additions & 7 deletions src/components/Chat/MessageSeen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ const MessageSeen = ({ isRead, isSender }: MessageSeenProps) => {
const { seenMessage } = useChatUtils(socket);

const sortedMessages = useMemo(() => {
if (!app.currentChatId) {
if (!app.currentroom) {
return;
}
return Object.values(state[app.currentChatId]?.messages ?? {})?.sort(
return Object.values(state[app.currentroom]?.messages ?? {})?.sort(
(a, b) => {
const da = new Date(a.time),
db = new Date(b.time);

return da.getTime() - db.getTime();
}
);
}, [state, app.currentChatId]);
}, [state, app.currentroom]);

useEffect(() => {
// Initialize Intersection Observer
Expand All @@ -42,19 +42,19 @@ const MessageSeen = ({ isRead, isSender }: MessageSeenProps) => {
?.getAttribute('id')
?.split('-')[1] as string;

if (!app.currentChatId) {
if (!app.currentroom) {
return;
}

try {
seenMessage({
messageId,
chatId: app.currentChatId,
room: app.currentroom,
});
} catch (e) {
return;
}
receiveMessage(messageId, app.currentChatId);
receiveMessage(messageId, app.currentroom);
}
});
},
Expand All @@ -81,7 +81,7 @@ const MessageSeen = ({ isRead, isSender }: MessageSeenProps) => {
// Clean up the observer
observer.disconnect();
};
}, [sortedMessages, isTabVisible, app.currentChatId, isSender]);
}, [sortedMessages, isTabVisible, app.currentroom, isSender]);

return isSender && <p className='text-sm'>{isRead ? 'Seen' : 'Not Seen'}</p>;
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const NavBar = () => {
const fullscreenPages = ['/founduser'];

const hideNavbar = useMemo(
() => fullscreenPages.includes(location) && app.currentChatId,
() => fullscreenPages.includes(location) && app.currentroom,
[location, app]
);

Expand Down
4 changes: 2 additions & 2 deletions src/components/PageWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ const PageWrapper = ({ children }: ProviderType) => {
}, [isLoggedIn, router]);

useEffect(() => {
const onRestoreChat = ({ chats, currentChatId }: any) => {
const onRestoreChat = ({ chats, currentroom }: any) => {
Object.values(chats).forEach((chat: any) => {
createChat(chat.id, chat.userIds, chat.messages, chat.createdAt);
});
endSearch(currentChatId);
endSearch(currentroom);
};

socket?.on(events.NEW_EVENT_CHAT_RESTORE, onRestoreChat);
Expand Down
4 changes: 2 additions & 2 deletions src/context/AppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ export const AppProvider = ({ children }: ProviderType) => {
});
}

function endSearch(currentChatId: string | null = null): undefined {
function endSearch(currentroom: string | null = null): undefined {
dispatch({
type: 'STOP_SEARCHING',
payload: { currentChatId },
payload: { currentroom },
});
}

Expand Down
20 changes: 10 additions & 10 deletions src/context/ChatContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,39 +83,39 @@ export const ChatProvider = ({ children }: ProviderType) => {
});
},
createChat: (
chatId: string,
room: string,
userIds: [string, string],
messages: MessageIdType = {},
createdAt?: Date
): any => {
try {
dispatch({
type: 'CREATE_CHAT',
payload: { chatId, userIds, messages, createdAt },
payload: { room, userIds, messages, createdAt },
});
console.log('context', { chatId, userIds, messages, createdAt });
return { chatId, userIds };
console.log('context', { room, userIds, messages, createdAt });
return { room, userIds };
} catch (error) {
console.error('Error creating chat:', error);
return { error };
}
},
removeMessage: (id: string, chatId: string) => {
removeMessage: (id: string, room: string) => {
dispatch({
type: 'REMOVE_MESSAGE',
payload: { id, room: chatId },
payload: { id, room },
});
},
receiveMessage: (id: string, chatId: string) => {
receiveMessage: (id: string, room: string) => {
dispatch({
type: 'RECEIVE_MESSAGE',
payload: { id, room: chatId },
payload: { id, room },
});
},
closeChat: (chatId: string) => {
closeChat: (room: string) => {
dispatch({
type: 'CLOSE_CHAT',
payload: { chatId },
payload: { room },
});
},
closeAllChats: () => {
Expand Down
8 changes: 4 additions & 4 deletions src/lib/chatHelper.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { AppType, ChatIdType, MessageType } from '@/types/types';
import { AppType, RoomType, MessageType } from '@/types/types';
import { RefObject } from 'react';

export default (state: ChatIdType, app: AppType) => {
export default (state: RoomType, app: AppType) => {
const getMessage = (id: string): MessageType | null => {
if (app.currentChatId === null) {
if (app.currentroom === null) {
return null;
}

const chatContent = state[app.currentChatId];
const chatContent = state[app.currentroom];

if (!chatContent) {
return null;
Expand Down
Loading
Loading