From 74a423600fca071ae31c00a828eade0e7037f3b2 Mon Sep 17 00:00:00 2001 From: hskwon Date: Wed, 22 Oct 2025 20:21:50 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20Dashboard=20=EC=97=AD=ED=95=A0=20?= =?UTF-8?q?=EC=A0=84=ED=99=98=20=EC=8B=9C=20=EC=BB=A8=ED=85=90=EC=B8=A0=20?= =?UTF-8?q?=EC=A6=89=EC=8B=9C=20=EB=B0=98=EC=98=81=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - userRole을 정적 변수에서 useState로 변경하여 reactive state로 전환 - useEffect로 storage 및 roleChanged 이벤트 리스너 추가 - localStorage 변경 감지 시 자동 리렌더링 구현 - SystemAdmin, CEO, ProductionManager, Worker 역할별 대시보드 동적 렌더링 --- src/components/business/Dashboard.tsx | 32 ++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/components/business/Dashboard.tsx b/src/components/business/Dashboard.tsx index 19c5d46..8846153 100644 --- a/src/components/business/Dashboard.tsx +++ b/src/components/business/Dashboard.tsx @@ -1,4 +1,4 @@ -import { useState } from "react"; +import { useState, useEffect } from "react"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; @@ -60,10 +60,32 @@ import { Checkbox } from "@/components/ui/checkbox"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; export function Dashboard() { - // Get user role from localStorage - const userDataStr = localStorage.getItem("user"); - const userData = userDataStr ? JSON.parse(userDataStr) : null; - const userRole = userData?.role || "CEO"; // Default to CEO if not found + // Get user role from localStorage and make it reactive + const [userRole, setUserRole] = useState(() => { + const userDataStr = localStorage.getItem("user"); + const userData = userDataStr ? JSON.parse(userDataStr) : null; + return userData?.role || "CEO"; + }); + + // Listen for storage changes to update role dynamically + useEffect(() => { + const handleStorageChange = () => { + const userDataStr = localStorage.getItem("user"); + const userData = userDataStr ? JSON.parse(userDataStr) : null; + const newRole = userData?.role || "CEO"; + setUserRole(newRole); + }; + + // Listen to custom storage event + window.addEventListener('storage', handleStorageChange); + // Listen to custom role change event + window.addEventListener('roleChanged', handleStorageChange); + + return () => { + window.removeEventListener('storage', handleStorageChange); + window.removeEventListener('roleChanged', handleStorageChange); + }; + }, []); // 시스템관리자인 경우 SystemAdminDashboard 렌더링 if (userRole === "SystemAdmin") {