11<script setup lang="ts">
2- import { ref , onMounted , defineProps } from ' vue' // Added defineProps
2+ import { ref , onMounted , defineProps , computed } from ' vue' // Added defineProps
33import { useRouter } from ' vue-router'
44import { useI18n } from ' vue-i18n'
55import { getEnv } from ' @/utils/env' // Import getEnv
@@ -30,14 +30,16 @@ import {
3030} from ' @/components/ui/dropdown-menu'
3131import { Avatar , AvatarFallback , AvatarImage } from ' @/components/ui/avatar'
3232import { TeamService , type Team } from ' @/services/teamService'
33+ import { UserService , type User } from ' @/services/userService'
3334import {
3435 Server ,
3536 Settings ,
3637 Key ,
3738 ChevronDown ,
38- User ,
39+ User as UserIcon ,
3940 LogOut ,
40- Users
41+ Users ,
42+ FileSliders
4143} from ' lucide-vue-next'
4244
4345// Define props, including variant
@@ -51,10 +53,16 @@ const router = useRouter()
5153const { t } = useI18n ()
5254
5355// User data
56+ const currentUser = ref <User | null >(null )
5457const userEmail = ref (' ' )
5558const userName = ref (' ' )
5659const userLoading = ref (true )
5760
61+ // Role checking
62+ const isGlobalAdmin = computed (() => {
63+ return currentUser .value ?.role_id === ' global_admin'
64+ })
65+
5866// Teams data
5967const teams = ref <Team []>([])
6068const selectedTeam = ref <Team | null >(null )
@@ -80,21 +88,24 @@ const navigationItems = [
8088 },
8189]
8290
83- // Fetch user data logic (remains the same)
91+ // Fetch user data logic using UserService
8492const fetchUserData = async () => {
8593 try {
86- const apiUrl = getEnv (' VITE_DEPLOYSTACK_BACKEND_URL' ) // Use getEnv with the correct key
87- if (! apiUrl ) {
88- throw new Error (' API URL not configured. Make sure VITE_DEPLOYSTACK_BACKEND_URL is set.' )
94+ const user = await UserService .getCurrentUser ()
95+ if (user ) {
96+ currentUser .value = user
97+ userEmail .value = user .email
98+ userName .value = user .username || ' '
99+ } else {
100+ // User not logged in, redirect to login
101+ router .push (' /login' )
89102 }
90- const response = await fetch (` ${apiUrl }/api/users/me ` , { method: ' GET' , headers: { ' Content-Type' : ' application/json' }, credentials: ' include' })
91- if (! response .ok ) {
92- if (response .status === 401 ) { router .push (' /login' ); return }
93- throw new Error (` Failed to fetch user data: ${response .status } ` )
94- }
95- const data = await response .json ()
96- if (data .success && data .data ) { userEmail .value = data .data .email ; userName .value = data .data .username ; }
97- } catch (error ) { console .error (' Error fetching user data:' , error ) } finally { userLoading .value = false }
103+ } catch (error ) {
104+ console .error (' Error fetching user data:' , error )
105+ currentUser .value = null
106+ } finally {
107+ userLoading .value = false
108+ }
98109}
99110
100111// Fetch teams logic (remains the same)
@@ -197,6 +208,26 @@ onMounted(() => {
197208 </SidebarMenu >
198209 </SidebarGroupContent >
199210 </SidebarGroup >
211+
212+ <!-- Admin Area section - only visible to global_admin -->
213+ <SidebarGroup v-if =" isGlobalAdmin" >
214+ <SidebarGroupLabel >{{ t('sidebar.adminArea.title') }}</SidebarGroupLabel >
215+ <SidebarGroupContent >
216+ <SidebarMenu >
217+ <SidebarMenuItem >
218+ <SidebarMenuButton
219+ @click =" navigateTo('/global-settings')"
220+ :is-active =" router.currentRoute.value.path === '/global-settings'"
221+ class =" w-full justify-start"
222+ :aria-current =" router.currentRoute.value.path === '/global-settings' ? 'page' : undefined"
223+ >
224+ <FileSliders class =" mr-2 h-4 w-4 shrink-0" />
225+ <span >{{ t('sidebar.adminArea.globalSettings') }}</span >
226+ </SidebarMenuButton >
227+ </SidebarMenuItem >
228+ </SidebarMenu >
229+ </SidebarGroupContent >
230+ </SidebarGroup >
200231 </SidebarContent >
201232
202233 <SidebarFooter >
@@ -224,7 +255,7 @@ onMounted(() => {
224255 align =" start"
225256 >
226257 <DropdownMenuItem @click =" goToAccount" class =" gap-2" >
227- <User class =" size-4" />
258+ <UserIcon class =" size-4" />
228259 {{ t('sidebar.user.account') }}
229260 </DropdownMenuItem >
230261 <DropdownMenuSeparator />
0 commit comments