Skip to content

Commit

Permalink
feat: display online status
Browse files Browse the repository at this point in the history
  • Loading branch information
meowtec committed Jun 12, 2023
1 parent 9c2d395 commit 2d95c46
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
21 changes: 20 additions & 1 deletion packages/web/src/components/avatar/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// red dot
&__badge {
position: absolute;
bottom: 0;
top: 0;
right: 0;
min-width: 18px;
height: 18px;
Expand All @@ -22,5 +22,24 @@
border-radius: 50%;
background-color: #ff0000;
color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

&__status {
position: absolute;
bottom: 0px;
right: 0px;
width: 10px;
height: 10px;
border-radius: 10px;
color: #fff;

&.__online {
background: #00dd00;
}

&.__offline {
background: #ff0000;
}
}
}
8 changes: 7 additions & 1 deletion packages/web/src/components/avatar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import './index.scss';
interface AvatarProps {
id: string;
badge?: number;
online?: boolean;
className?: string;
}

Expand All @@ -16,7 +17,9 @@ function getAvatarColors(id: string) {
return new Array(3 + size).fill(0).map(() => `#${Math.floor(rand() * 0xffffff).toString(16).padStart(6, '0')}`);
}

export default function Avatar({ id, badge, className }: AvatarProps) {
export default function Avatar({
id, badge, online, className,
}: AvatarProps) {
const colors = useMemo(() => getAvatarColors(id), [id]);

return (
Expand All @@ -27,6 +30,9 @@ export default function Avatar({ id, badge, className }: AvatarProps) {
colors={colors}
size={60}
/>
{online != null ? (
<span className={clsx('avatar__status', online ? '__online' : '__offline')} />
) : null}
{badge ? (
<span className="avatar__badge">{badge}</span>
) : null}
Expand Down
4 changes: 4 additions & 0 deletions packages/web/src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import { createSelectors } from './selectors';
const useAppStoreBase = create<AppState>()(persist(() => initialState, {
name: 'chat-storage',
version: 0,
partialize: (state) => ({
...state,
chatUserId: null,
}),
}));

export type UseAppStoreExtended = typeof useAppStoreBase & {
Expand Down
7 changes: 5 additions & 2 deletions packages/web/src/views/chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import InputBox from './input-box';
import './index.scss';

interface ChatProps {
userInfo: User | null,
userInfo: User | null;
online: boolean;
channel: ChatChannel | null;
onSubmit: (content: File | string) => void;
}
Expand All @@ -20,14 +21,15 @@ const userPlaceholder: User = {
};

function Chat({
channel, userInfo, onSubmit,
channel, userInfo, online, onSubmit,
}: ChatProps) {
return (
<div className="chat">
<div className="chat__header">
<UserItem
className="chat__user"
isMe={false}
online={online}
user={userInfo ?? userPlaceholder}
/>
</div>
Expand Down Expand Up @@ -67,6 +69,7 @@ export default function ChatConnected() {
<Chat
userInfo={channelInfo.userInfo ?? null}
channel={channelInfo.channel ?? null}
online={channelInfo.isOnline}
onSubmit={handleSubmit}
/>
) : null}
Expand Down
3 changes: 3 additions & 0 deletions packages/web/src/views/user-item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ interface UserItemProps {
isMe: boolean;
user: User;
unreadCount?: number;
online?: boolean;
className?: string;
}

export default function UserItem({
isMe,
user,
unreadCount,
online,
className,
}: UserItemProps) {
return (
Expand All @@ -24,6 +26,7 @@ export default function UserItem({
<Avatar
id={user.id}
badge={unreadCount}
online={online}
/>
<div className="user-item__name">{user.user_name}</div>
</div>
Expand Down

0 comments on commit 2d95c46

Please sign in to comment.