From 7a6cebe207bab4cd5b62cd367fafddace99bdac6 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Thu, 8 Jan 2026 03:42:45 +0000 Subject: [PATCH] Changes --- src/components/navigation/TopBar.tsx | 108 +++++++++++++++++---------- 1 file changed, 68 insertions(+), 40 deletions(-) diff --git a/src/components/navigation/TopBar.tsx b/src/components/navigation/TopBar.tsx index d5f7aac..ff69505 100644 --- a/src/components/navigation/TopBar.tsx +++ b/src/components/navigation/TopBar.tsx @@ -1,6 +1,6 @@ -import { useState } from "react"; +import { useState, useEffect } from "react"; import { useNavigate } from "react-router-dom"; -import { Menu, ChevronDown, LogOut, User, Shield, Building2 } from "lucide-react"; +import { Menu, ChevronDown, LogOut, User, Shield, Building2, Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { SidebarTrigger } from "@/components/ui/sidebar"; import { @@ -12,30 +12,46 @@ import { DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; - -// Mock user data - will be replaced with real auth context -const mockUser = { - name: "John Doe", - email: "john@example.com", - avatar: null, - initials: "JD", -}; - -// Mock organization data -const mockOrgs = [ - { id: "1", name: "Acme Corp", role: "admin" }, - { id: "2", name: "Beta Inc", role: "member" }, -]; +import { useAuth } from "@/contexts/AuthContext"; +import { api, Organization } from "@/lib/api"; export function TopBar() { const navigate = useNavigate(); - const [currentOrg, setCurrentOrg] = useState(mockOrgs[0]); + const { user, isAuthenticated } = useAuth(); + const [organizations, setOrganizations] = useState([]); + const [currentOrg, setCurrentOrg] = useState(null); + const [orgsLoading, setOrgsLoading] = useState(true); + + useEffect(() => { + async function fetchOrgs() { + if (!isAuthenticated) { + setOrgsLoading(false); + return; + } + + try { + const response = await api.users.organizations(); + setOrganizations(response.organizations); + if (response.organizations.length > 0 && !currentOrg) { + setCurrentOrg(response.organizations[0]); + } + } catch (error) { + console.error("Failed to fetch organizations:", error); + } finally { + setOrgsLoading(false); + } + } + fetchOrgs(); + }, [isAuthenticated]); const handleLogout = () => { - // Will be replaced with actual logout logic navigate("/login"); }; + const userInitials = user?.full_name + ? user.full_name.split(" ").map(n => n[0]).join("").toUpperCase().slice(0, 2) + : user?.email?.slice(0, 2).toUpperCase() || "U"; + return (
{/* Left side - Sidebar toggle */} @@ -54,7 +70,9 @@ export function TopBar() {
- {currentOrg.name} + + {orgsLoading ? "Loading..." : (currentOrg?.name || "No Organization")} + @@ -63,23 +81,33 @@ export function TopBar() { Switch Organization - {mockOrgs.map((org) => ( - setCurrentOrg(org)} - className="flex items-center justify-between" - > -
- - {org.name} -
- {org.role === "admin" && ( - - Admin - - )} -
- ))} + {orgsLoading ? ( +
+ +
+ ) : organizations.length === 0 ? ( +
+ No organizations +
+ ) : ( + organizations.map((org) => ( + setCurrentOrg(org)} + className="flex items-center justify-between" + > +
+ + {org.name} +
+ {org.role && ["owner", "admin"].includes(org.role) && ( + + {org.role} + + )} +
+ )) + )} @@ -88,9 +116,9 @@ export function TopBar() {