import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { User } from '@/types'; import { useUserData } from '@/hooks/useUserData'; import { useAuth } from '@/contexts/AuthContext'; import AddUserForm from '@/components/AddUserForm'; import EditUserForm from '@/components/EditUserForm'; import UserCard from '@/components/UserCard'; const UsersPage: React.FC = () => { const { t } = useTranslation(); const { auth } = useAuth(); const currentUser = auth.user; const { users, loading: usersLoading, error: userError, setError: setUserError, deleteUser, triggerRefresh } = useUserData(); const [editingUser, setEditingUser] = useState(null); const [showAddForm, setShowAddForm] = useState(false); // Check if current user is admin if (!currentUser?.isAdmin) { return (

{t('users.adminRequired')}

); } const handleEditClick = (user: User) => { setEditingUser(user); }; const handleEditComplete = () => { setEditingUser(null); triggerRefresh(); // Refresh the users list after editing }; const handleDeleteUser = async (username: string) => { const result = await deleteUser(username); if (!result?.success) { setUserError(result?.message || t('users.deleteError')); } }; const handleAddUser = () => { setShowAddForm(true); }; const handleAddComplete = () => { setShowAddForm(false); triggerRefresh(); // Refresh the users list after adding }; return (

{t('pages.users.title')}

{userError && (

{userError}

)} {usersLoading ? (

{t('app.loading')}

) : users.length === 0 ? (

{t('users.noUsers')}

) : (
{users.map((user) => ( ))}
)} {showAddForm && ( )} {editingUser && ( setEditingUser(null)} /> )}
); }; export default UsersPage;