'use client'; import { useState } from 'react'; import PasswordEntryForm from './PasswordEntryForm'; import UserForm from './UserForm'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from './ui/dialog'; import { Avatar, AvatarFallback, AvatarImage } from './ui/avatar'; import { Crown, Pencil, Plus, User as UserIcon, UserRoundPen } from 'lucide-react'; import { Input } from './ui/input'; import { Button } from './ui/button'; import { useAtom } from 'jotai'; import { usersAtom } from '@/lib/atoms'; import { signIn } from '@/app/actions/user'; import { createUser } from '@/app/actions/data'; import { useTranslations } from 'next-intl'; import { toast } from '@/hooks/use-toast'; import { Description } from '@radix-ui/react-dialog'; import { SafeUser, User } from '@/lib/types'; import { cn } from '@/lib/utils'; import { useHelpers } from '@/lib/client-helpers'; function UserCard({ user, onSelect, onEdit, showEdit, isCurrentUser }: { user: User, onSelect: () => void, onEdit: () => void, showEdit: boolean, isCurrentUser: boolean }) { return (
{showEdit && ( )}
); } function AddUserButton({ onClick }: { onClick: () => void }) { const t = useTranslations('UserSelectModal'); return ( ); } function UserSelectionView({ users, currentUser, onUserSelect, onEditUser, onCreateUser, }: { users: User[], currentUser?: SafeUser, onUserSelect: (userId: string) => void, onEditUser: (userId: string) => void, onCreateUser: () => void, }) { return (
{users .filter(user => user.id !== currentUser?.id) .map((user) => ( onUserSelect(user.id)} onEdit={() => onEditUser(user.id)} showEdit={!!currentUser?.isAdmin} isCurrentUser={false} /> ))} {currentUser?.isAdmin && }
); } export default function UserSelectModal({ onClose }: { onClose: () => void }) { const t = useTranslations('UserSelectModal'); const [selectedUser, setSelectedUser] = useState(); const [isCreating, setIsCreating] = useState(false); const [isEditing, setIsEditing] = useState(false); const [error, setError] = useState(''); const [usersData] = useAtom(usersAtom); const users = usersData.users; const { currentUser } = useHelpers(); const handleUserSelect = (userId: string) => { setSelectedUser(userId); setError(''); }; const handleEditUser = (userId: string) => { setSelectedUser(userId); setIsEditing(true); }; const handleCreateUser = () => { setIsCreating(true); }; const handleFormSuccess = () => { setSelectedUser(undefined); setIsCreating(false); setIsEditing(false); onClose(); }; const handleFormCancel = () => { setSelectedUser(undefined); setIsCreating(false); setIsEditing(false); setError(''); }; return ( {isCreating ? t('createNewUserTitle') : t('selectUserTitle')}
{!selectedUser && !isCreating && !isEditing ? ( ) : isCreating || isEditing ? ( ) : ( u.id === selectedUser)!} onCancel={() => setSelectedUser(undefined)} onSubmit={async (password) => { try { setError(''); const user = users.find(u => u.id === selectedUser); if (!user) throw new Error("User not found"); await signIn(user.username, password); setError(''); onClose(); toast({ title: t('signInSuccessTitle'), description: t('signInSuccessDescription', { username: user.username }), variant: "default" }); setTimeout(() => window.location.reload(), 300); } catch (err) { setError(t('errorInvalidPassword')); throw err; } }} error={error} /> )}
); }