Skip to content

Commit

Permalink
fix: 새로고침해도 workspace 권한 반영되게 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
hyonun321 committed Dec 2, 2024
1 parent ab47668 commit fb19818
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { WorkspaceListItem } from "@noctaCrdt/Interfaces"; // 이전에 만든
import { useSocketStore } from "@src/stores/useSocketStore";
import { useToastStore } from "@src/stores/useToastStore";
import { useUserInfo } from "@src/stores/useUserStore";
import { useWorkspaceStore, WorkspaceRole } from "@src/stores/useWorkspaceStore";
import { useWorkspaceStore } from "@src/stores/useWorkspaceStore";
import {
itemContainer,
itemContent,
Expand Down Expand Up @@ -34,7 +34,7 @@ export const WorkspaceSelectItem = ({
const handleClick = () => {
if (!isActive) {
switchWorkspace(userId, id);
setCurrentRole(role as WorkspaceRole);
setCurrentRole(role);
addToast(`워크스페이스(${name})에 접속하였습니다.`);
}
};
Expand Down
14 changes: 13 additions & 1 deletion client/src/stores/useSocketStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "@noctaCrdt/Interfaces";
import { io, Socket } from "socket.io-client";
import { create } from "zustand";
import { useWorkspaceStore } from "./useWorkspaceStore";

class BatchProcessor {
private batch: any[] = [];
Expand Down Expand Up @@ -136,7 +137,7 @@ export const useSocketStore = create<SocketStore>((set, get) => ({

socket.on("workspace", (workspace: WorkSpaceSerializedProps) => {
const { setWorkspace } = get();
setWorkspace(workspace); // 수정된 부분
setWorkspace(workspace);
});

socket.on("workspace/connections", (connections: Record<string, number>) => {
Expand All @@ -153,12 +154,23 @@ export const useSocketStore = create<SocketStore>((set, get) => ({

socket.on("workspace/list", (workspaces: WorkspaceListItem[]) => {
set({ availableWorkspaces: workspaces });
const { availableWorkspaces } = get();
console.log(availableWorkspaces);
const { workspace } = get();
const currentWorkspace = availableWorkspaces.find((ws) => ws.id === workspace!.id);
if (currentWorkspace) {
useWorkspaceStore.getState().setCurrentRole(currentWorkspace.role);
}
});

socket.on("error", (error: Error) => {
console.error("Socket error:", error);
});

socket.on("workspace/role", (data: { role: "owner" | "editor" }) => {
useWorkspaceStore.getState().setCurrentRole(data.role);
});

socket.connect();
},

Expand Down
5 changes: 2 additions & 3 deletions client/src/stores/useWorkspaceStore.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { create } from "zustand";

// 워크스페이스 권한 타입 정의
export type WorkspaceRole = "owner" | "editor";

interface WorkspaceStore {
// 현재 선택된 워크스페이스의 권한
currentRole: WorkspaceRole | null;
currentRole: string | null;
// 권한 설정 함수
setCurrentRole: (role: WorkspaceRole | null) => void;
setCurrentRole: (role: string | null) => void;
}

export const useWorkspaceStore = create<WorkspaceStore>((set) => ({
Expand Down

0 comments on commit fb19818

Please sign in to comment.