"use client"; import { useState, useEffect, useMemo } from "react"; import { Search, Building2, Mail, MessageSquare, Users, User } from "lucide-react"; import { Card, CardContent } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Badge } from "@/components/ui/badge"; import { fetchEmployees, fetchDepartments } from "@/lib/supabase/employees-actions"; import { createOrFindConversation } from "@/lib/supabase/comms-actions"; import type { EmployeeWithRelations, Department } from "@/lib/supabase/employees-types"; import PageSkeleton from "./PageSkeleton"; const AVATAR_COLORS = ["bg-teal-500", "bg-blue-500", "bg-purple-500", "bg-orange-500", "bg-pink-500"]; function getInitials(firstName: string, lastName: string): string { return `${firstName[0]}${lastName[0]}`.toUpperCase(); } export default function DirectoryPage() { const [employees, setEmployees] = useState([]); const [departments, setDepartments] = useState([]); const [loading, setLoading] = useState(true); const [searchQuery, setSearchQuery] = useState(""); const [selectedDept, setSelectedDept] = useState("all"); useEffect(() => { let cancelled = false; Promise.all([fetchEmployees(), fetchDepartments()]) .then(([emps, depts]) => { if (!cancelled) { // Only show active employees in directory setEmployees(emps.filter(e => e.status === 'active')); setDepartments(depts); } }) .catch((err) => { console.error("Failed to load directory:", err); }) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; }; }, []); const filteredEmployees = useMemo(() => { return employees.filter((emp) => { const matchesSearch = `${emp.first_name} ${emp.last_name}`.toLowerCase().includes(searchQuery.toLowerCase()) || emp.job_title?.toLowerCase().includes(searchQuery.toLowerCase()); const matchesDept = selectedDept === "all" || emp.department_id === selectedDept; return matchesSearch && matchesDept; }); }, [employees, searchQuery, selectedDept]); const handleMessage = async (userId: string) => { if (!userId) return; try { // In a real app, this would trigger navigation to Comms with this user // For now, we just find/create the conversation await createOrFindConversation(userId); // We'd need to tell AppShell to switch to Comms, but for now we just log console.log("Messaging user:", userId); // If AppShell was accessible via context, we'd call a navigate function here } catch (err) { console.error("Failed to start conversation:", err); } }; if (loading) return ; return (

Directory

Connect with your teammates across the organisation

setSearchQuery(e.target.value)} />
{filteredEmployees.length === 0 ? (

No teammates found

Try adjusting your search or filters

) : (
{filteredEmployees.map((emp, i) => { const initials = getInitials(emp.first_name, emp.last_name); const colorClass = AVATAR_COLORS[i % AVATAR_COLORS.length]; return (
{initials}

{emp.first_name} {emp.last_name}

{emp.job_title || "Team Member"}

{emp.department && ( {emp.department.name} )}
{emp.email} {emp.user_id && ( )}
); })}
)}
); }