Skip to content

Commit 98f20fa

Browse files
committed
feat: unread chat count badge
1 parent ce39533 commit 98f20fa

4 files changed

Lines changed: 34 additions & 1 deletion

File tree

โ€Žsrc/common/components/Navigator/index.tsxโ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import { Link, useLocation } from 'react-router';
22
import * as s from './style.css';
33
import { NAVIGATION_MENU_LIST } from '@/pages/routes';
4+
import { useUnreadChatStore } from '@/common/store/UnreadChatStore';
45

56
// TODO: ์•„์ด์ฝ˜์—๋„ Transition Animation ์ถ”๊ฐ€
67
const Navigator = () => {
78
const { pathname } = useLocation();
9+
const unreadChatCount = useUnreadChatStore(state => state.unreadChatCount);
810

911
return (
1012
<nav className={s.Container}>
1113
{NAVIGATION_MENU_LIST.map(({ label, path, selectedClassName, unSelectedClassName }) => {
1214
const selected = pathname === path;
15+
const viewUnreadChatCount = label === '์ฑ„ํŒ…' && unreadChatCount > 0;
1316
return (
1417
<Link key={path} className={s.Menu({ selected })} to={path}>
1518
<span className={selected ? selectedClassName : unSelectedClassName} />
1619
<p>{label}</p>
20+
{viewUnreadChatCount && <div className={s.UnreadChatCount}>{unreadChatCount}</div>}
1721
</Link>
1822
);
1923
})}

โ€Žsrc/common/components/Navigator/style.css.tsโ€Ž

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const Menu = cva({
3131
lineHeight: 1.2,
3232
letterSpacing: '-0.0175rem',
3333
cursor: 'pointer',
34+
position: 'relative',
3435
'& p': {
3536
lineClamp: 1,
3637
whiteSpace: 'nowrap',
@@ -61,3 +62,17 @@ export const Menu = cva({
6162
selected: false,
6263
},
6364
});
65+
66+
export const UnreadChatCount = css({
67+
position: 'absolute',
68+
bgColor: 'main',
69+
color: '100',
70+
p: '0.0625rem 0.375rem',
71+
rounded: 'full',
72+
right: '0.25rem',
73+
top: '-0.25rem',
74+
textAlign: 'center',
75+
fontSize: '0.75rem',
76+
fontWeight: 400,
77+
lineHeight: '1.4',
78+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { create } from 'zustand';
2+
3+
interface UnreadChatStore {
4+
unreadChatCount: number;
5+
setUnreadChatCount: (unreadChatCount: number) => void;
6+
}
7+
8+
export const useUnreadChatStore = create<UnreadChatStore>()(set => ({
9+
unreadChatCount: 0,
10+
setUnreadChatCount: unreadChatCount => set({ unreadChatCount }),
11+
}));

โ€Žsrc/common/utils/SocketProvider/index.tsxโ€Ž

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@ import { useEffect } from 'react';
22
import { connectSocket, subChatListSocket } from '@/common/utils/wsClient';
33
import { useQueryClient } from '@tanstack/react-query';
44
import { QUERY_KEYS } from '@/libs/queryKeys';
5+
import { useUnreadChatStore } from '@/common/store/UnreadChatStore';
56

67
interface Props {
78
children: React.ReactNode;
89
}
910

1011
const SocketProvider = ({ children }: Props) => {
1112
const queryClient = useQueryClient();
13+
const setUnreadChatCount = useUnreadChatStore(state => state.setUnreadChatCount);
1214

1315
useEffect(() => {
1416
let unsubscribe: (() => void) | undefined;
1517

1618
connectSocket()
1719
.then(() => {
1820
console.log('์•ฑ ์‹œ์ž‘ ์‹œ WebSocket ์—ฐ๊ฒฐ ์™„๋ฃŒ');
19-
unsubscribe = subChatListSocket(() => {
21+
unsubscribe = subChatListSocket(data => {
2022
queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.CHAT_LIST] });
23+
setUnreadChatCount(data.message.unreadChatCount);
2124
});
2225
})
2326
.catch(err => {

0 commit comments

Comments
ย (0)