diff --git a/Components/Search/OpenChatPicture.tsx b/Components/Search/OpenChatPicture.tsx index 1c6d336..4d2d9be 100644 --- a/Components/Search/OpenChatPicture.tsx +++ b/Components/Search/OpenChatPicture.tsx @@ -1,6 +1,7 @@ import React from 'react'; import { User } from '@/app/search/search.type'; import Image from 'next/image'; +import { convertPictureURL } from '@/hooks/Common/users'; const OpenChatPicture = ({ openChatUsers }: { openChatUsers: User[] }) => { let userCount = 0; @@ -15,11 +16,13 @@ const OpenChatPicture = ({ openChatUsers }: { openChatUsers: User[] }) => { return null; // 사진이 4개 이상인 경우 렌더링을 하지 않음 } + const picture = convertPictureURL(user.picture); + return (
  • user picture { return ( @@ -26,22 +28,40 @@ const FriendProfiles = ({ allUsers }: { allUsers: User[] | undefined }) => { ); }; -export const FriendProfile = ({ user }: { user: User }) => { - const picture = - user.picture.trim().split('.')[0] === 'https://avatars' - ? user.picture - : '/icon_cat.svg'; +export const FriendProfile = ({ user }: { user: User | UserHasOnline }) => { + const picture = convertPictureURL(user.picture); + const isUserOnline = 'isOnline' in user; return (
    - {user.name} +
    +
    + {user.name} +
    + {isUserOnline && ( + + online + + )} +

    {user.name}

    diff --git a/Components/Users/FriendProfilesCheckOnline.tsx b/Components/Users/FriendProfilesCheckOnline.tsx new file mode 100644 index 0000000..e1324c9 --- /dev/null +++ b/Components/Users/FriendProfilesCheckOnline.tsx @@ -0,0 +1,71 @@ +'use client'; + +import React, { useEffect, useState } from 'react'; +import io from 'socket.io-client'; +import { getCookie } from '../Login/Cookie'; +import Link from 'next/link'; +import { FriendProfile } from './FriendProfiles'; +import { User } from '@/types'; +import { UserHasOnline } from '@/app/users/users.type'; + +export const FriendProfilesCheckOnline = ({ + allUsersExceptMe, +}: { + allUsersExceptMe: User[] | undefined; +}) => { + const [userNameConnected, setUserNameConnected] = useState([]); + const accessToken = getCookie('accessToken'); + + const users: UserHasOnline[] = + allUsersExceptMe?.map((user) => { + return { ...user, isOnline: userNameConnected.includes(user.id) }; + }) || []; + + useEffect(() => { + const socket = io(`https://fastcampus-chat.net/server`, { + extraHeaders: { + Authorization: `Bearer ${accessToken}`, + serverId: process.env.NEXT_PUBLIC_SERVER_ID as string, + }, + }); + const socketInitializer = async () => { + socket.on('connect', () => { + console.log('connected', socket); + socket.emit('users-server'); + }); + + socket.on('users-server-to-client', (obj) => { + setUserNameConnected(obj.users); + console.log('users-server-to-client', obj); + }); + }; + + socketInitializer(); + + return () => { + console.log('disconnect user'); + socket.disconnect(); + }; + }, [accessToken]); + + return ( +
    + {users?.map((user) => { + return ( +
  • + + + +
  • + ); + })} + + ); +}; + +export default FriendProfilesCheckOnline; diff --git a/Components/Users/MyProfile.tsx b/Components/Users/MyProfile.tsx index 0c594f7..f96c077 100644 --- a/Components/Users/MyProfile.tsx +++ b/Components/Users/MyProfile.tsx @@ -4,9 +4,10 @@ import Image from 'next/image'; import React from 'react'; import { User } from '@/types'; import Link from 'next/link'; +import { convertPictureURL } from '@/hooks/Common/users'; const MyProfile = ({ user }: { user: User }) => { - const picture = user.picture || '/icon_cat.svg'; + const picture = convertPictureURL(user.picture); return (
    @@ -17,13 +18,19 @@ const MyProfile = ({ user }: { user: User }) => { }} >
    -
    +
    {user.name}
    diff --git a/Components/Users/ProfileModal.tsx b/Components/Users/ProfileModal.tsx index 45072de..5a7292e 100644 --- a/Components/Users/ProfileModal.tsx +++ b/Components/Users/ProfileModal.tsx @@ -9,6 +9,7 @@ import Image from 'next/image'; import { useSearchParams, useRouter } from 'next/navigation'; import React from 'react'; import { getCookie } from '../Login/Cookie'; +import { convertPictureURL } from '@/hooks/Common/users'; const ProfileModal = ({ user, @@ -20,7 +21,7 @@ const ProfileModal = ({ const accessToken = getCookie('accessToken'); const router = useRouter(); const searchParams = useSearchParams(); - const isMyProfile = searchParams.get('isMyProfile') === 'true'; + const isMyProfile = searchParams?.get('isMyProfile') === 'true'; const chattingParticipateHandler = async () => { if (existPrivateChat) { @@ -55,13 +56,21 @@ const ProfileModal = ({
    - {user.name} +
    + {user.name} +

    {user.name}

    diff --git a/Components/Users/ShowSearchedFriend.tsx b/Components/Users/ShowSearchedFriend.tsx index e625256..49cc394 100644 --- a/Components/Users/ShowSearchedFriend.tsx +++ b/Components/Users/ShowSearchedFriend.tsx @@ -1,3 +1,4 @@ +import { convertPictureURL } from '@/hooks/Common/users'; import { User } from '@/types'; import Image from 'next/image'; import Link from 'next/link'; @@ -15,10 +16,7 @@ const ShowSearchedFriend = ({ return (
    {searchedUserUpToFour?.map((user) => { - const picture = - user.picture.trim().split('.')[0] === 'https://avatars' - ? user.picture - : '/icon_cat.svg'; + const picture = convertPictureURL(user.picture); return (
    { const cookieStore = cookies(); @@ -23,7 +23,7 @@ const Users = async () => { 친구{allUsersExceptMe?.length}명
    - +