mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Change logged out view to still have bottom user menu, and move settings into it
This commit is contained in:
1 parent
60fa8d2239
commit
69c38b489a
5 files changed
+254
-31
No files matched your search
@@ -1,7 +1,6 @@
|
||||
import React, { useEffect } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { Plus, Folder, GitBranch, Settings, Loader2, ChevronDown, Star } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Plus, Folder, GitBranch, Loader2, ChevronDown, Star } from 'lucide-react';
|
||||
import { Skeleton } from '@/components/ui/skeleton';
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';
|
||||
import { LoginArea } from '@/components/auth/LoginArea';
|
||||
@@ -116,16 +115,6 @@ export function ProjectSidebar({
|
||||
Shakespeare
|
||||
</h1>
|
||||
</button>
|
||||
<div className="flex items-center gap-1">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-8 w-8 p-0 hover:bg-primary/10 -mr-2"
|
||||
onClick={() => navigate('/settings')}
|
||||
>
|
||||
<Settings className="h-4 w-4 text-primary/60 hover:text-primary" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { AccountSwitcher } from './AccountSwitcher';
|
||||
import { useLoggedInAccounts } from '@/hooks/useLoggedInAccounts';
|
||||
|
||||
// Mock the hooks
|
||||
vi.mock('@/hooks/useLoggedInAccounts');
|
||||
vi.mock('react-router-dom', () => ({
|
||||
useNavigate: () => vi.fn()
|
||||
}));
|
||||
|
||||
// Mock the RelaySelector component
|
||||
vi.mock('@/components/RelaySelector', () => ({
|
||||
RelaySelector: ({ className }: { className?: string }) => (
|
||||
<div data-testid="relay-selector" className={className}>Relay Selector</div>
|
||||
)
|
||||
}));
|
||||
|
||||
const mockUseLoggedInAccounts = vi.mocked(useLoggedInAccounts);
|
||||
|
||||
describe('AccountSwitcher', () => {
|
||||
it('renders account switcher with dropdown trigger', () => {
|
||||
mockUseLoggedInAccounts.mockReturnValue({
|
||||
authors: [{
|
||||
id: 'user1',
|
||||
pubkey: 'pubkey1',
|
||||
metadata: { name: 'Test User' }
|
||||
}],
|
||||
currentUser: {
|
||||
id: 'user1',
|
||||
pubkey: 'pubkey1',
|
||||
metadata: { name: 'Test User' }
|
||||
},
|
||||
otherUsers: [],
|
||||
setLogin: vi.fn(),
|
||||
removeLogin: vi.fn()
|
||||
});
|
||||
|
||||
render(<AccountSwitcher onAddAccountClick={vi.fn()} />);
|
||||
|
||||
// Should show the user's name
|
||||
expect(screen.getByText('Test User')).toBeInTheDocument();
|
||||
|
||||
// Should show dropdown trigger button
|
||||
expect(screen.getByRole('button')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('returns null when no current user', () => {
|
||||
mockUseLoggedInAccounts.mockReturnValue({
|
||||
authors: [],
|
||||
currentUser: undefined,
|
||||
otherUsers: [],
|
||||
setLogin: vi.fn(),
|
||||
removeLogin: vi.fn()
|
||||
});
|
||||
|
||||
const { container } = render(<AccountSwitcher onAddAccountClick={vi.fn()} />);
|
||||
|
||||
expect(container.firstChild).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -1,7 +1,7 @@
|
||||
// NOTE: This file is stable and usually should not be modified.
|
||||
// It is important that all functionality in this file is preserved, and should only be modified if explicitly requested.
|
||||
|
||||
import { ChevronDown, LogOut, UserIcon, UserPlus } from 'lucide-react';
|
||||
import { ChevronDown, LogOut, UserIcon, UserPlus, Settings } from 'lucide-react';
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
@@ -13,6 +13,7 @@ import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar.tsx'
|
||||
import { RelaySelector } from '@/components/RelaySelector';
|
||||
import { useLoggedInAccounts, type Account } from '@/hooks/useLoggedInAccounts';
|
||||
import { genUserName } from '@/lib/genUserName';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
|
||||
interface AccountSwitcherProps {
|
||||
onAddAccountClick: () => void;
|
||||
@@ -20,6 +21,7 @@ interface AccountSwitcherProps {
|
||||
|
||||
export function AccountSwitcher({ onAddAccountClick }: AccountSwitcherProps) {
|
||||
const { currentUser, otherUsers, setLogin, removeLogin } = useLoggedInAccounts();
|
||||
const navigate = useNavigate();
|
||||
|
||||
if (!currentUser) return null;
|
||||
|
||||
@@ -63,6 +65,13 @@ export function AccountSwitcher({ onAddAccountClick }: AccountSwitcherProps) {
|
||||
</DropdownMenuItem>
|
||||
))}
|
||||
<DropdownMenuSeparator />
|
||||
<DropdownMenuItem
|
||||
onClick={() => navigate('/settings')}
|
||||
className='flex items-center gap-2 cursor-pointer p-2 rounded-md'
|
||||
>
|
||||
<Settings className='w-4 h-4' />
|
||||
<span>Settings</span>
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem
|
||||
onClick={onAddAccountClick}
|
||||
className='flex items-center gap-2 cursor-pointer p-2 rounded-md'
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { LoginArea } from './LoginArea';
|
||||
import { useLoggedInAccounts } from '@/hooks/useLoggedInAccounts';
|
||||
|
||||
// Mock the hooks
|
||||
vi.mock('@/hooks/useLoggedInAccounts');
|
||||
vi.mock('@/hooks/useAppContext', () => ({
|
||||
useAppContext: () => ({
|
||||
config: { relayUrl: 'wss://relay.damus.io' },
|
||||
updateConfig: vi.fn(),
|
||||
presetRelays: [
|
||||
{ name: 'Damus', url: 'wss://relay.damus.io' }
|
||||
]
|
||||
})
|
||||
}));
|
||||
|
||||
// Mock the components
|
||||
vi.mock('./AccountSwitcher', () => ({
|
||||
AccountSwitcher: ({ onAddAccountClick }: { onAddAccountClick: () => void }) => (
|
||||
<div data-testid="account-switcher">
|
||||
<button onClick={onAddAccountClick}>Add Account</button>
|
||||
</div>
|
||||
)
|
||||
}));
|
||||
|
||||
// Mock react-router-dom
|
||||
vi.mock('react-router-dom', () => ({
|
||||
useNavigate: () => vi.fn()
|
||||
}));
|
||||
|
||||
vi.mock('@/components/RelaySelector', () => ({
|
||||
RelaySelector: ({ className }: { className?: string }) => (
|
||||
<div data-testid="relay-selector" className={className}>Relay Selector</div>
|
||||
)
|
||||
}));
|
||||
|
||||
vi.mock('./LoginDialog', () => ({
|
||||
default: ({ isOpen }: { isOpen: boolean; onClose: () => void }) =>
|
||||
isOpen ? <div data-testid="login-dialog">Login Dialog</div> : null
|
||||
}));
|
||||
|
||||
vi.mock('./SignupDialog', () => ({
|
||||
default: ({ isOpen }: { isOpen: boolean; onClose: () => void }) =>
|
||||
isOpen ? <div data-testid="signup-dialog">Signup Dialog</div> : null
|
||||
}));
|
||||
|
||||
const mockUseLoggedInAccounts = vi.mocked(useLoggedInAccounts);
|
||||
|
||||
describe('LoginArea', () => {
|
||||
it('shows AccountSwitcher when user is logged in', () => {
|
||||
mockUseLoggedInAccounts.mockReturnValue({
|
||||
authors: [{
|
||||
id: 'user1',
|
||||
pubkey: 'pubkey1',
|
||||
metadata: { name: 'Test User' }
|
||||
}],
|
||||
currentUser: {
|
||||
id: 'user1',
|
||||
pubkey: 'pubkey1',
|
||||
metadata: { name: 'Test User' }
|
||||
},
|
||||
otherUsers: [],
|
||||
setLogin: vi.fn(),
|
||||
removeLogin: vi.fn()
|
||||
});
|
||||
|
||||
render(<LoginArea />);
|
||||
|
||||
expect(screen.getByTestId('account-switcher')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows anonymous user menu when user is not logged in', () => {
|
||||
mockUseLoggedInAccounts.mockReturnValue({
|
||||
authors: [],
|
||||
currentUser: undefined,
|
||||
otherUsers: [],
|
||||
setLogin: vi.fn(),
|
||||
removeLogin: vi.fn()
|
||||
});
|
||||
|
||||
render(<LoginArea />);
|
||||
|
||||
// Should show anonymous user display
|
||||
expect(screen.getByText('Anonymous')).toBeInTheDocument();
|
||||
|
||||
// Should show the dropdown trigger button
|
||||
expect(screen.getByRole('button')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('maintains consistent layout structure whether logged in or not', () => {
|
||||
// Test logged out state
|
||||
mockUseLoggedInAccounts.mockReturnValue({
|
||||
authors: [],
|
||||
currentUser: undefined,
|
||||
otherUsers: [],
|
||||
setLogin: vi.fn(),
|
||||
removeLogin: vi.fn()
|
||||
});
|
||||
|
||||
const { rerender } = render(<LoginArea />);
|
||||
|
||||
const loggedOutContainer = screen.getByRole('button');
|
||||
expect(loggedOutContainer).toHaveClass('flex', 'items-center', 'gap-3', 'p-3', 'rounded-full');
|
||||
|
||||
// Test logged in state
|
||||
mockUseLoggedInAccounts.mockReturnValue({
|
||||
authors: [{
|
||||
id: 'user1',
|
||||
pubkey: 'pubkey1',
|
||||
metadata: { name: 'Test User' }
|
||||
}],
|
||||
currentUser: {
|
||||
id: 'user1',
|
||||
pubkey: 'pubkey1',
|
||||
metadata: { name: 'Test User' }
|
||||
},
|
||||
otherUsers: [],
|
||||
setLogin: vi.fn(),
|
||||
removeLogin: vi.fn()
|
||||
});
|
||||
|
||||
rerender(<LoginArea />);
|
||||
|
||||
// Both states should maintain similar structure
|
||||
expect(screen.getByTestId('account-switcher')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -2,13 +2,22 @@
|
||||
// It is important that all functionality in this file is preserved, and should only be modified if explicitly requested.
|
||||
|
||||
import { useState } from 'react';
|
||||
import { User, UserPlus } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button.tsx';
|
||||
import { User, UserPlus, ChevronDown, Settings } from 'lucide-react';
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuSeparator,
|
||||
DropdownMenuTrigger,
|
||||
} from '@/components/ui/dropdown-menu.tsx';
|
||||
import { Avatar, AvatarFallback } from '@/components/ui/avatar.tsx';
|
||||
import { RelaySelector } from '@/components/RelaySelector';
|
||||
import LoginDialog from './LoginDialog';
|
||||
import SignupDialog from './SignupDialog';
|
||||
import { useLoggedInAccounts } from '@/hooks/useLoggedInAccounts';
|
||||
import { AccountSwitcher } from './AccountSwitcher';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
|
||||
export interface LoginAreaProps {
|
||||
className?: string;
|
||||
@@ -16,6 +25,7 @@ export interface LoginAreaProps {
|
||||
|
||||
export function LoginArea({ className }: LoginAreaProps) {
|
||||
const { currentUser } = useLoggedInAccounts();
|
||||
const navigate = useNavigate();
|
||||
const [loginDialogOpen, setLoginDialogOpen] = useState(false);
|
||||
const [signupDialogOpen, setSignupDialogOpen] = useState(false);
|
||||
|
||||
@@ -29,22 +39,48 @@ export function LoginArea({ className }: LoginAreaProps) {
|
||||
{currentUser ? (
|
||||
<AccountSwitcher onAddAccountClick={() => setLoginDialogOpen(true)} />
|
||||
) : (
|
||||
<div className="flex gap-3 justify-center">
|
||||
<Button
|
||||
onClick={() => setLoginDialogOpen(true)}
|
||||
className='flex items-center gap-2 px-4 py-2 rounded-full bg-primary text-primary-foreground w-full font-medium transition-all hover:bg-primary/90 animate-scale-in'
|
||||
>
|
||||
<User className='w-4 h-4' />
|
||||
<span className='truncate'>Log in</span>
|
||||
</Button><Button
|
||||
onClick={() => setSignupDialogOpen(true)}
|
||||
variant="outline"
|
||||
className="flex items-center gap-2 px-4 py-2 rounded-full font-medium transition-all"
|
||||
>
|
||||
<UserPlus className="w-4 h-4" />
|
||||
<span>Sign Up</span>
|
||||
</Button>
|
||||
</div>
|
||||
<DropdownMenu modal={false}>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<button className='flex items-center gap-3 p-3 rounded-full hover:bg-accent transition-all w-full text-foreground'>
|
||||
<Avatar className='w-10 h-10'>
|
||||
<AvatarFallback>
|
||||
<User className='w-5 h-5 text-muted-foreground' />
|
||||
</AvatarFallback>
|
||||
</Avatar>
|
||||
<div className='flex-1 text-left truncate'>
|
||||
<p className='font-medium text-sm truncate'>Anonymous</p>
|
||||
</div>
|
||||
<ChevronDown className='w-4 h-4 text-muted-foreground' />
|
||||
</button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent className='w-56 p-2 animate-scale-in'>
|
||||
<div className='font-medium text-sm px-2 py-1.5'>Switch Relay</div>
|
||||
<RelaySelector className="w-full" />
|
||||
<DropdownMenuSeparator />
|
||||
<DropdownMenuItem
|
||||
onClick={() => navigate('/settings')}
|
||||
className='flex items-center gap-2 cursor-pointer p-2 rounded-md'
|
||||
>
|
||||
<Settings className='w-4 h-4' />
|
||||
<span>Settings</span>
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuSeparator />
|
||||
<DropdownMenuItem
|
||||
onClick={() => setLoginDialogOpen(true)}
|
||||
className='flex items-center gap-2 cursor-pointer p-2 rounded-md'
|
||||
>
|
||||
<User className='w-4 h-4' />
|
||||
<span>Log in</span>
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem
|
||||
onClick={() => setSignupDialogOpen(true)}
|
||||
className='flex items-center gap-2 cursor-pointer p-2 rounded-md'
|
||||
>
|
||||
<UserPlus className='w-4 h-4' />
|
||||
<span>Sign up</span>
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
)}
|
||||
|
||||
<LoginDialog
|
||||
|
||||
Reference in new issue
Block a user