-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #210 from boostcampwm-2024/feature-fe-#206
유저를 관리하기 위한 웹소켓 Room 추가
- Loading branch information
Showing
10 changed files
with
200 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { User } from "@/store/useUserStore"; | ||
import { cn } from "@/lib/utils"; | ||
|
||
interface ActiveUserProps { | ||
users: User[]; | ||
className?: string; | ||
} | ||
|
||
export default function ActiveUser({ users, className }: ActiveUserProps) { | ||
return ( | ||
<div className={cn("flex gap-2", className)}> | ||
{users.map((user) => ( | ||
<div | ||
style={{ backgroundColor: user.color }} | ||
className={cn("group relative h-5 w-5 rounded-full")} | ||
key={user.clientId} | ||
> | ||
<div | ||
style={{ backgroundColor: user.color }} | ||
className="absolute left-2 z-10 hidden px-2 text-sm group-hover:flex" | ||
> | ||
{user.clientId} | ||
</div> | ||
</div> | ||
))} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useEffect } from "react"; | ||
|
||
import usePageStore from "@/store/usePageStore"; | ||
import useUserStore, { User } from "@/store/useUserStore"; | ||
|
||
export const useSyncedUsers = () => { | ||
const { currentPage } = usePageStore(); | ||
const { provider, currentUser, setCurrentUser, setUsers } = useUserStore(); | ||
|
||
const updateUsersFromAwareness = () => { | ||
const values = Array.from( | ||
provider.awareness.getStates().values(), | ||
) as User[]; | ||
setUsers(values); | ||
}; | ||
|
||
const getLocalStorageUser = (): User | null => { | ||
const userData = localStorage.getItem("currentUser"); | ||
return userData ? JSON.parse(userData) : null; | ||
}; | ||
|
||
useEffect(() => { | ||
if (currentPage === null) return; | ||
|
||
const updatedUser: User = { | ||
...currentUser, | ||
currentPageId: currentPage.toString(), | ||
}; | ||
|
||
setCurrentUser(updatedUser); | ||
provider.awareness.setLocalState(updatedUser); | ||
}, [currentPage]); | ||
|
||
useEffect(() => { | ||
const localStorageUser = getLocalStorageUser(); | ||
|
||
if (!localStorageUser) { | ||
localStorage.setItem("currentUser", JSON.stringify(currentUser)); | ||
} else { | ||
setCurrentUser(localStorageUser); | ||
provider.awareness.setLocalState(localStorageUser); | ||
} | ||
|
||
updateUsersFromAwareness(); | ||
|
||
provider.awareness.on("change", updateUsersFromAwareness); | ||
|
||
return () => { | ||
provider.awareness.off("change", updateUsersFromAwareness); | ||
}; | ||
}, []); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { create } from "zustand"; | ||
import * as Y from "yjs"; | ||
import { SocketIOProvider } from "y-socket.io"; | ||
|
||
import { getRandomColor, getRandomHexString } from "@/lib/utils"; | ||
|
||
export interface User { | ||
clientId: string; | ||
color: string; | ||
currentPageId: string | null; | ||
} | ||
|
||
interface UserStore { | ||
provider: SocketIOProvider; | ||
users: User[]; | ||
currentUser: User; | ||
setUsers: (users: User[]) => void; | ||
setCurrentUser: (user: User) => void; | ||
} | ||
|
||
const useUserStore = create<UserStore>((set) => ({ | ||
provider: new SocketIOProvider( | ||
import.meta.env.VITE_WS_URL, | ||
`users`, | ||
new Y.Doc(), | ||
{ | ||
autoConnect: true, | ||
disableBc: false, | ||
}, | ||
{ | ||
reconnectionDelayMax: 10000, | ||
timeout: 5000, | ||
transports: ["websocket", "polling"], | ||
}, | ||
), | ||
users: [], | ||
currentUser: { | ||
clientId: getRandomHexString(10), | ||
color: getRandomColor(), | ||
currentPageId: null, | ||
}, | ||
setUsers: (users: User[]) => set({ users }), | ||
setCurrentUser: (user: User) => set({ currentUser: user }), | ||
})); | ||
|
||
export default useUserStore; |