feat(ui): 集成 shadcn/ui 原语、Framer Motion 动画和 Sonner Toast

- 添加 shadcn/ui 风格原语组件 (Button, Input, Dialog, Select, Slider, Switch, Tooltip)
- 集成 Framer Motion 动画库,添加动画预设
- 集成 Sonner Toast 通知系统
- 改造 ChatMessage 添加淡入动画和复制按钮
- 改造 Sidebar 添加动画、空状态引导和骨架屏
- 改造 ConfigPanel 使用新原语组件
- 优化 Chat 页面空状态和连接状态指示器
- 添加 tailwindcss-animate 插件
This commit is contained in:
2025-12-12 17:20:41 +08:00
parent 67c6918b28
commit cbbe9c7af1
26 changed files with 2272 additions and 514 deletions
+66 -19
View File
@@ -2,8 +2,11 @@
* Chat Message Component
*/
import { User, Bot } from 'lucide-react';
import clsx from 'clsx';
import { User, Bot, Copy, Check } from 'lucide-react';
import { motion } from 'framer-motion';
import { useState } from 'react';
import { cn } from '../utils/cn';
import { fadeInUp, smoothTransition } from '../utils/animations';
import type { Message } from '../api/client.js';
interface ChatMessageProps {
@@ -12,16 +15,28 @@ interface ChatMessageProps {
export function ChatMessage({ message }: ChatMessageProps) {
const isUser = message.role === 'user';
const [copied, setCopied] = useState(false);
const handleCopy = async () => {
await navigator.clipboard.writeText(message.content);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
};
return (
<div
className={clsx(
'flex gap-4 p-4 rounded-lg',
isUser ? 'bg-gray-800' : 'bg-gray-850'
<motion.div
variants={fadeInUp}
initial="initial"
animate="animate"
exit="exit"
transition={smoothTransition}
className={cn(
'group flex gap-4 p-4 rounded-lg',
isUser ? 'bg-gray-800' : 'bg-gray-800/50'
)}
>
<div
className={clsx(
className={cn(
'w-8 h-8 rounded-full flex items-center justify-center flex-shrink-0',
isUser ? 'bg-primary-600' : 'bg-green-600'
)}
@@ -29,14 +44,23 @@ export function ChatMessage({ message }: ChatMessageProps) {
{isUser ? <User size={18} /> : <Bot size={18} />}
</div>
<div className="flex-1 min-w-0">
<div className="text-sm text-gray-400 mb-1">
{isUser ? 'You' : 'AI Assistant'}
<div className="flex items-center justify-between mb-1">
<span className="text-sm text-gray-400">
{isUser ? 'You' : 'AI Assistant'}
</span>
<button
onClick={handleCopy}
className="opacity-0 group-hover:opacity-100 p-1 rounded text-gray-500 hover:text-gray-300 hover:bg-gray-700 transition-all"
title="Copy message"
>
{copied ? <Check size={14} className="text-green-500" /> : <Copy size={14} />}
</button>
</div>
<div className="message-content whitespace-pre-wrap break-words">
{message.content}
</div>
</div>
</div>
</motion.div>
);
}
@@ -46,7 +70,12 @@ interface StreamingMessageProps {
export function StreamingMessage({ content }: StreamingMessageProps) {
return (
<div className="flex gap-4 p-4 rounded-lg bg-gray-850">
<motion.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
transition={smoothTransition}
className="flex gap-4 p-4 rounded-lg bg-gray-800/50"
>
<div className="w-8 h-8 rounded-full flex items-center justify-center flex-shrink-0 bg-green-600">
<Bot size={18} />
</div>
@@ -54,27 +83,45 @@ export function StreamingMessage({ content }: StreamingMessageProps) {
<div className="text-sm text-gray-400 mb-1">AI Assistant</div>
<div className="message-content whitespace-pre-wrap break-words">
{content}
<span className="inline-block w-2 h-4 bg-gray-400 animate-pulse ml-1" />
<motion.span
animate={{ opacity: [1, 0] }}
transition={{ duration: 0.8, repeat: Infinity, repeatType: 'reverse' }}
className="inline-block w-2 h-4 bg-primary-400 ml-1 rounded-sm"
/>
</div>
</div>
</div>
</motion.div>
);
}
export function TypingIndicator() {
return (
<div className="flex gap-4 p-4 rounded-lg bg-gray-850">
<motion.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
transition={smoothTransition}
className="flex gap-4 p-4 rounded-lg bg-gray-800/50"
>
<div className="w-8 h-8 rounded-full flex items-center justify-center flex-shrink-0 bg-green-600">
<Bot size={18} />
</div>
<div className="flex-1">
<div className="text-sm text-gray-400 mb-1">AI Assistant</div>
<div className="typing-indicator">
<span />
<span />
<span />
<div className="flex items-center gap-1 h-6">
{[0, 1, 2].map((i) => (
<motion.span
key={i}
animate={{ y: [0, -4, 0] }}
transition={{
duration: 0.6,
repeat: Infinity,
delay: i * 0.15,
}}
className="w-2 h-2 rounded-full bg-gray-400"
/>
))}
</div>
</div>
</div>
</motion.div>
);
}
+248 -242
View File
@@ -5,7 +5,16 @@
*/
import { useState, useEffect } from 'react';
import clsx from 'clsx';
import { X } from 'lucide-react';
import { motion, AnimatePresence } from 'framer-motion';
import { toast } from 'sonner';
import { cn } from '../utils/cn';
import { modalOverlay, modalContent, smoothTransition } from '../utils/animations';
import { Button } from '../primitives/Button';
import { Input } from '../primitives/Input';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../primitives/Select';
import { Slider } from '../primitives/Slider';
import { Skeleton } from './Skeleton';
import { getConfig, updateConfig, type ServerConfig } from '../api/client.js';
interface ConfigPanelProps {
@@ -22,12 +31,17 @@ const AVAILABLE_MODELS = [
{ id: 'claude-3-haiku-20240307', name: 'Claude 3 Haiku' },
];
// Temperature 语义标签
function getTemperatureLabel(value: number): string {
if (value <= 0.3) return 'Precise';
if (value <= 0.7) return 'Balanced';
return 'Creative';
}
export function ConfigPanel({ onClose, responsive = false }: ConfigPanelProps) {
const [config, setConfig] = useState<ServerConfig | null>(null);
const [loading, setLoading] = useState(true);
const [saving, setSaving] = useState(false);
const [error, setError] = useState<string | null>(null);
const [success, setSuccess] = useState(false);
// 表单状态
const [formData, setFormData] = useState({
@@ -50,7 +64,7 @@ export function ConfigPanel({ onClose, responsive = false }: ConfigPanelProps) {
workdir: response.data.workdir,
});
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load config');
toast.error(err instanceof Error ? err.message : 'Failed to load config');
} finally {
setLoading(false);
}
@@ -61,16 +75,13 @@ export function ConfigPanel({ onClose, responsive = false }: ConfigPanelProps) {
// 保存配置
const handleSave = async () => {
setSaving(true);
setError(null);
setSuccess(false);
try {
const response = await updateConfig(formData);
setConfig(response.data);
setSuccess(true);
setTimeout(() => setSuccess(false), 2000);
toast.success('Settings saved');
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to save config');
toast.error(err instanceof Error ? err.message : 'Failed to save config');
} finally {
setSaving(false);
}
@@ -85,243 +96,238 @@ export function ConfigPanel({ onClose, responsive = false }: ConfigPanelProps) {
temperature: config.temperature,
workdir: config.workdir,
});
toast.info('Settings reset');
}
};
if (loading) {
return (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
<div className="bg-gray-800 rounded-lg p-6">
<div className="text-gray-400">Loading configuration...</div>
// Loading 骨架屏
const LoadingSkeleton = () => (
<div className="space-y-6 p-6">
{[1, 2, 3, 4].map((i) => (
<div key={i} className="space-y-2">
<Skeleton className="h-4 w-24" />
<Skeleton className="h-10 w-full" />
</div>
</div>
);
}
return (
<div
className={clsx(
'fixed inset-0 bg-black/50 flex z-50',
responsive ? 'items-end md:items-center justify-center' : 'items-center justify-center'
)}
>
{/* 响应式模式:移动端从底部滑出的全屏面板;桌面端:居中弹窗 */}
{/* 非响应式:固定居中弹窗 */}
<div
className={clsx(
'bg-gray-800 max-h-[90vh] overflow-auto',
responsive
? 'w-full md:w-full md:max-w-lg md:mx-4 rounded-t-2xl md:rounded-lg'
: 'rounded-lg w-full max-w-lg mx-4'
)}
>
{/* Header */}
<div
className={clsx(
'sticky top-0 flex items-center justify-between border-b border-gray-700 bg-gray-800 z-10',
responsive ? 'px-4 md:px-6 py-4' : 'px-6 py-4'
)}
>
{/* 响应式模式下移动端拖动指示器 */}
{responsive && (
<div className="absolute top-2 left-1/2 -translate-x-1/2 w-10 h-1 bg-gray-600 rounded-full md:hidden" />
)}
<h2 className={clsx('text-lg font-semibold', responsive && 'mt-2 md:mt-0')}>
Configuration
</h2>
<button
onClick={onClose}
className={clsx(
'hover:bg-gray-700 rounded transition-colors',
responsive
? 'p-2 active:bg-gray-600 rounded-lg min-w-[44px] min-h-[44px] flex items-center justify-center'
: 'p-1'
)}
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</button>
</div>
{/* Content */}
<div className={clsx('space-y-6', responsive ? 'p-4 md:p-6' : 'p-6')}>
{/* Error message */}
{error && (
<div className="p-3 bg-red-900/50 border border-red-700 rounded-lg text-red-300 text-sm">
{error}
</div>
)}
{/* Success message */}
{success && (
<div className="p-3 bg-green-900/50 border border-green-700 rounded-lg text-green-300 text-sm">
Configuration saved successfully!
</div>
)}
{/* Model */}
<div>
<label className="block text-sm font-medium text-gray-300 mb-2">Model</label>
<select
value={formData.model}
onChange={(e) => setFormData({ ...formData, model: e.target.value })}
className={clsx(
'w-full bg-gray-900 border border-gray-700 rounded-lg text-white focus:outline-none focus:border-blue-500',
responsive ? 'px-3 py-3 md:py-2 text-base md:text-sm' : 'px-3 py-2'
)}
>
{AVAILABLE_MODELS.map((model) => (
<option key={model.id} value={model.id}>
{model.name}
</option>
))}
</select>
<p className="mt-1 text-xs text-gray-500">
Select the AI model to use for conversations
</p>
</div>
{/* Max Tokens */}
<div>
<label className="block text-sm font-medium text-gray-300 mb-2">
Max Tokens: {formData.maxTokens.toLocaleString()}
</label>
<input
type="range"
min="1024"
max="32768"
step="1024"
value={formData.maxTokens}
onChange={(e) => setFormData({ ...formData, maxTokens: parseInt(e.target.value) })}
className={clsx(
'w-full h-2 bg-gray-700 rounded-lg appearance-none cursor-pointer',
responsive && 'touch-pan-x'
)}
/>
<div className="flex justify-between text-xs text-gray-500 mt-1">
<span>1K</span>
<span>8K</span>
<span>16K</span>
<span>32K</span>
</div>
<p className="mt-1 text-xs text-gray-500">
Maximum number of tokens in the response
</p>
</div>
{/* Temperature */}
<div>
<label className="block text-sm font-medium text-gray-300 mb-2">
Temperature: {formData.temperature.toFixed(2)}
</label>
<input
type="range"
min="0"
max="1"
step="0.05"
value={formData.temperature}
onChange={(e) => setFormData({ ...formData, temperature: parseFloat(e.target.value) })}
className={clsx(
'w-full h-2 bg-gray-700 rounded-lg appearance-none cursor-pointer',
responsive && 'touch-pan-x'
)}
/>
<div className="flex justify-between text-xs text-gray-500 mt-1">
<span>{responsive ? 'Precise' : 'Precise (0)'}</span>
<span>{responsive ? 'Balanced' : 'Balanced (0.5)'}</span>
<span>{responsive ? 'Creative' : 'Creative (1)'}</span>
</div>
<p className="mt-1 text-xs text-gray-500">Controls randomness in responses</p>
</div>
{/* Working Directory */}
<div>
<label className="block text-sm font-medium text-gray-300 mb-2">Working Directory</label>
<input
type="text"
value={formData.workdir}
onChange={(e) => setFormData({ ...formData, workdir: e.target.value })}
className={clsx(
'w-full bg-gray-900 border border-gray-700 rounded-lg text-white focus:outline-none focus:border-blue-500 font-mono',
responsive ? 'px-3 py-3 md:py-2 text-base md:text-sm' : 'px-3 py-2 text-sm'
)}
placeholder="/path/to/project"
/>
<p className="mt-1 text-xs text-gray-500">Root directory for file operations</p>
</div>
{/* Server Info (Read-only) */}
{config && (
<div className="pt-4 border-t border-gray-700">
<h3 className="text-sm font-medium text-gray-400 mb-3">Server Information</h3>
<div
className={clsx(
'text-sm',
responsive ? 'grid grid-cols-1 md:grid-cols-2 gap-3' : 'grid grid-cols-2 gap-4'
)}
>
<div className={responsive ? 'flex justify-between md:block' : ''}>
<span className="text-gray-500">Allowed Paths:</span>
<span className={clsx('text-gray-300', responsive ? 'md:ml-2' : 'ml-2')}>
{config.allowedPaths.length || 'All'}
</span>
</div>
<div className={responsive ? 'flex justify-between md:block' : ''}>
<span className="text-gray-500">Denied Paths:</span>
<span className={clsx('text-gray-300', responsive ? 'md:ml-2' : 'ml-2')}>
{config.deniedPaths.length || 'None'}
</span>
</div>
</div>
</div>
)}
</div>
{/* Footer */}
<div
className={clsx(
'sticky bottom-0 border-t border-gray-700 bg-gray-800',
responsive
? 'flex flex-col-reverse md:flex-row items-stretch md:items-center justify-end gap-2 md:gap-3 p-4 md:px-6 md:py-4 safe-area-pb'
: 'flex items-center justify-end gap-3 px-6 py-4 bg-gray-800/50'
)}
>
<button
onClick={handleReset}
className={clsx(
'text-sm text-gray-300 hover:text-white transition-colors',
responsive ? 'px-4 py-3 md:py-2 active:bg-gray-700 rounded-lg' : 'px-4 py-2'
)}
>
Reset
</button>
<button
onClick={onClose}
className={clsx(
'text-sm bg-gray-700 hover:bg-gray-600 rounded-lg transition-colors',
responsive ? 'px-4 py-3 md:py-2 active:bg-gray-500' : 'px-4 py-2'
)}
>
Cancel
</button>
<button
onClick={handleSave}
disabled={saving}
className={clsx(
'text-sm bg-blue-600 hover:bg-blue-500 rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed',
responsive ? 'px-4 py-3 md:py-2 active:bg-blue-700 font-medium' : 'px-4 py-2'
)}
>
{saving ? 'Saving...' : 'Save Changes'}
</button>
</div>
</div>
))}
</div>
);
return (
<AnimatePresence>
<motion.div
variants={modalOverlay}
initial="initial"
animate="animate"
exit="exit"
transition={{ duration: 0.2 }}
className={cn(
'fixed inset-0 bg-black/50 flex z-50',
responsive ? 'items-end md:items-center justify-center' : 'items-center justify-center'
)}
onClick={onClose}
>
<motion.div
variants={modalContent}
initial="initial"
animate="animate"
exit="exit"
transition={smoothTransition}
onClick={(e) => e.stopPropagation()}
className={cn(
'bg-gray-800 max-h-[90vh] overflow-auto',
responsive
? 'w-full md:w-full md:max-w-lg md:mx-4 rounded-t-2xl md:rounded-lg'
: 'rounded-lg w-full max-w-lg mx-4'
)}
>
{/* Header */}
<div
className={cn(
'sticky top-0 flex items-center justify-between border-b border-gray-700 bg-gray-800 z-10',
responsive ? 'px-4 md:px-6 py-4' : 'px-6 py-4'
)}
>
{responsive && (
<div className="absolute top-2 left-1/2 -translate-x-1/2 w-10 h-1 bg-gray-600 rounded-full md:hidden" />
)}
<h2 className={cn('text-lg font-semibold', responsive && 'mt-2 md:mt-0')}>
Settings
</h2>
<Button
variant="ghost"
size="icon"
onClick={onClose}
className={cn(
responsive && 'min-w-[44px] min-h-[44px]'
)}
>
<X size={20} />
</Button>
</div>
{/* Content */}
{loading ? (
<LoadingSkeleton />
) : (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ delay: 0.1 }}
className={cn('space-y-6', responsive ? 'p-4 md:p-6' : 'p-6')}
>
{/* Model */}
<div className="space-y-2">
<label className="block text-sm font-medium text-gray-300">Model</label>
<Select
value={formData.model}
onValueChange={(value) => setFormData({ ...formData, model: value })}
>
<SelectTrigger>
<SelectValue placeholder="Select a model" />
</SelectTrigger>
<SelectContent>
{AVAILABLE_MODELS.map((model) => (
<SelectItem key={model.id} value={model.id}>
{model.name}
</SelectItem>
))}
</SelectContent>
</Select>
<p className="text-xs text-gray-500">
Select the AI model to use for conversations
</p>
</div>
{/* Max Tokens */}
<div className="space-y-3">
<div className="flex items-center justify-between">
<label className="text-sm font-medium text-gray-300">Max Tokens</label>
<span className="text-sm text-primary-400 font-medium">
{formData.maxTokens.toLocaleString()}
</span>
</div>
<Slider
min={1024}
max={32768}
step={1024}
value={[formData.maxTokens]}
onValueChange={(value) => setFormData({ ...formData, maxTokens: value[0] })}
/>
<div className="flex justify-between text-xs text-gray-500">
<span>1K</span>
<span>8K</span>
<span>16K</span>
<span>32K</span>
</div>
<p className="text-xs text-gray-500">
Maximum number of tokens in the response
</p>
</div>
{/* Temperature */}
<div className="space-y-3">
<div className="flex items-center justify-between">
<label className="text-sm font-medium text-gray-300">Temperature</label>
<div className="flex items-center gap-2">
<span className="text-xs text-gray-400">{getTemperatureLabel(formData.temperature)}</span>
<span className="text-sm text-primary-400 font-medium">
{formData.temperature.toFixed(2)}
</span>
</div>
</div>
<Slider
min={0}
max={1}
step={0.05}
value={[formData.temperature]}
onValueChange={(value) => setFormData({ ...formData, temperature: value[0] })}
/>
<div className="flex justify-between text-xs text-gray-500">
<span>Precise</span>
<span>Balanced</span>
<span>Creative</span>
</div>
<p className="text-xs text-gray-500">Controls randomness in responses</p>
</div>
{/* Working Directory */}
<div className="space-y-2">
<label className="block text-sm font-medium text-gray-300">Working Directory</label>
<Input
value={formData.workdir}
onChange={(e) => setFormData({ ...formData, workdir: e.target.value })}
className="font-mono"
placeholder="/path/to/project"
/>
<p className="text-xs text-gray-500">Root directory for file operations</p>
</div>
{/* Server Info */}
{config && (
<motion.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
transition={{ delay: 0.2 }}
className="pt-4 border-t border-gray-700"
>
<h3 className="text-sm font-medium text-gray-400 mb-3">Server Information</h3>
<div
className={cn(
'text-sm',
responsive ? 'grid grid-cols-1 md:grid-cols-2 gap-3' : 'grid grid-cols-2 gap-4'
)}
>
<div className={responsive ? 'flex justify-between md:block' : ''}>
<span className="text-gray-500">Allowed Paths:</span>
<span className={cn('text-gray-300', responsive ? 'md:ml-2' : 'ml-2')}>
{config.allowedPaths.length || 'All'}
</span>
</div>
<div className={responsive ? 'flex justify-between md:block' : ''}>
<span className="text-gray-500">Denied Paths:</span>
<span className={cn('text-gray-300', responsive ? 'md:ml-2' : 'ml-2')}>
{config.deniedPaths.length || 'None'}
</span>
</div>
</div>
</motion.div>
)}
</motion.div>
)}
{/* Footer */}
<div
className={cn(
'sticky bottom-0 border-t border-gray-700 bg-gray-800',
responsive
? 'flex flex-col-reverse md:flex-row items-stretch md:items-center justify-end gap-2 md:gap-3 p-4 md:px-6 md:py-4 safe-area-pb'
: 'flex items-center justify-end gap-3 px-6 py-4 bg-gray-800/50'
)}
>
<Button
variant="ghost"
onClick={handleReset}
className={cn(responsive && 'justify-center')}
>
Reset
</Button>
<Button
variant="secondary"
onClick={onClose}
className={cn(responsive && 'justify-center')}
>
Cancel
</Button>
<Button
onClick={handleSave}
disabled={saving || loading}
className={cn(responsive && 'justify-center')}
>
{saving ? 'Saving...' : 'Save Changes'}
</Button>
</div>
</motion.div>
</motion.div>
</AnimatePresence>
);
}
+173 -143
View File
@@ -5,9 +5,13 @@
*/
import { useState, useEffect } from 'react';
import { Plus, MessageSquare, Trash2, RefreshCw, Menu, X } from 'lucide-react';
import clsx from 'clsx';
import { Plus, MessageSquare, Trash2, Menu, X, MessageCircle } from 'lucide-react';
import { motion, AnimatePresence } from 'framer-motion';
import { toast } from 'sonner';
import { cn } from '../utils/cn';
import { fadeInUp, smoothTransition } from '../utils/animations';
import { listSessions, createSession, deleteSession, type Session } from '../api/client.js';
import { SessionSkeleton } from './Skeleton';
interface SidebarProps {
currentSessionId: string | null;
@@ -34,6 +38,7 @@ export function Sidebar({
setSessions(data);
} catch (error) {
console.error('Failed to load sessions:', error);
toast.error('Failed to load sessions');
} finally {
setIsLoading(false);
}
@@ -44,9 +49,11 @@ export function Sidebar({
const { data } = await createSession();
setSessions((prev) => [data, ...prev]);
onCreateSession(data);
if (responsive) setIsOpen(false); // 响应式模式下创建后关闭侧边栏
toast.success('Session created');
if (responsive) setIsOpen(false);
} catch (error) {
console.error('Failed to create session:', error);
toast.error('Failed to create session');
}
};
@@ -55,6 +62,7 @@ export function Sidebar({
try {
await deleteSession(id);
setSessions((prev) => prev.filter((s) => s.id !== id));
toast.success('Session deleted');
if (currentSessionId === id) {
const remaining = sessions.filter((s) => s.id !== id);
if (remaining.length > 0) {
@@ -63,117 +71,97 @@ export function Sidebar({
}
} catch (error) {
console.error('Failed to delete session:', error);
toast.error('Failed to delete session');
}
};
const handleSelectSession = (id: string) => {
onSelectSession(id);
if (responsive) setIsOpen(false); // 响应式模式下选择后关闭侧边栏
if (responsive) setIsOpen(false);
};
useEffect(() => {
loadSessions();
}, []);
// 点击遮罩层关闭
const handleOverlayClick = () => {
setIsOpen(false);
};
// 非响应式模式:简单的固定侧边栏
if (!responsive) {
return (
<div className="w-64 bg-gray-800 border-r border-gray-700 flex flex-col">
{/* Header */}
<div className="p-4 border-b border-gray-700">
<button
onClick={handleCreate}
className="w-full flex items-center justify-center gap-2 px-4 py-2 bg-primary-600 hover:bg-primary-700 rounded-lg transition-colors"
>
<Plus size={18} />
<span>New Chat</span>
</button>
</div>
{/* Session List */}
<div className="flex-1 overflow-y-auto">
{isLoading ? (
<div className="p-4 text-center text-gray-500">
<RefreshCw className="animate-spin inline-block" size={20} />
</div>
) : sessions.length === 0 ? (
<div className="p-4 text-center text-gray-500">No conversations yet</div>
) : (
<div className="p-2 space-y-1">
{sessions.map((session) => (
<div
key={session.id}
onClick={() => onSelectSession(session.id)}
className={clsx(
'flex items-center gap-2 p-3 rounded-lg cursor-pointer group',
'hover:bg-gray-700 transition-colors',
currentSessionId === session.id && 'bg-gray-700'
)}
>
<MessageSquare size={18} className="text-gray-400 flex-shrink-0" />
<div className="flex-1 min-w-0">
<div className="text-sm truncate">
{session.name || `Chat ${session.id.slice(0, 8)}`}
</div>
<div className="text-xs text-gray-500">{session.messageCount} messages</div>
</div>
<button
onClick={(e) => handleDelete(session.id, e)}
className="opacity-0 group-hover:opacity-100 p-1 hover:bg-gray-600 rounded transition-all"
>
<Trash2 size={14} className="text-gray-400" />
</button>
</div>
))}
</div>
)}
</div>
{/* Footer */}
<div className="p-4 border-t border-gray-700 text-center text-xs text-gray-500">
AI Assistant v1.0
</div>
// 空状态组件
const EmptyState = () => (
<motion.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
transition={smoothTransition}
className="p-6 text-center"
>
<div className="w-12 h-12 mx-auto mb-4 rounded-full bg-gray-700/50 flex items-center justify-center">
<MessageCircle size={24} className="text-gray-500" />
</div>
);
}
// 响应式模式:移动端抽屉 + 桌面端固定
return (
<>
{/* 移动端菜单按钮 */}
<button
onClick={() => setIsOpen(true)}
className="fixed top-3 left-3 z-40 p-2 rounded-lg bg-gray-800 text-gray-300 hover:bg-gray-700 transition-colors md:hidden"
aria-label="Open menu"
<p className="text-gray-400 mb-4">No conversations yet</p>
<motion.button
whileHover={{ scale: 1.02 }}
whileTap={{ scale: 0.98 }}
onClick={handleCreate}
className="text-primary-400 text-sm hover:text-primary-300 transition-colors"
>
<Menu size={20} />
</button>
Create your first chat
</motion.button>
</motion.div>
);
{/* 遮罩层 - 仅移动端 */}
{isOpen && (
<div
className="fixed inset-0 bg-black/50 z-40 md:hidden"
onClick={handleOverlayClick}
/>
// 会话列表项
const SessionItem = ({ session }: { session: Session }) => (
<motion.div
layout
variants={fadeInUp}
initial="initial"
animate="animate"
exit="exit"
transition={smoothTransition}
onClick={() => handleSelectSession(session.id)}
className={cn(
'flex items-center gap-2 p-3 rounded-lg cursor-pointer group',
'hover:bg-gray-700 transition-colors',
'active:bg-gray-600',
currentSessionId === session.id && 'bg-gray-700'
)}
{/* 侧边栏 */}
<div
className={clsx(
'fixed md:static inset-y-0 left-0 z-50',
'w-64 bg-gray-800 border-r border-gray-700 flex flex-col',
'transform transition-transform duration-300 ease-in-out',
'md:transform-none',
isOpen ? 'translate-x-0' : '-translate-x-full md:translate-x-0'
)}
>
<MessageSquare size={18} className="text-gray-400 flex-shrink-0" />
<div className="flex-1 min-w-0">
<div className="text-sm truncate">
{session.name || `Chat ${session.id.slice(0, 8)}`}
</div>
<div className="text-xs text-gray-500">{session.messageCount} messages</div>
</div>
<motion.button
initial={{ opacity: 0 }}
whileHover={{ scale: 1.1 }}
onClick={(e) => handleDelete(session.id, e)}
className="opacity-0 group-hover:opacity-100 p-1 hover:bg-gray-600 rounded transition-opacity"
aria-label="Delete session"
>
{/* Header */}
<div className="p-4 border-b border-gray-700">
<Trash2 size={14} className="text-gray-400" />
</motion.button>
</motion.div>
);
// 加载状态
const LoadingState = () => (
<div className="p-2 space-y-2">
{[1, 2, 3].map((i) => (
<SessionSkeleton key={i} />
))}
</div>
);
// 侧边栏内容
const SidebarContent = () => (
<>
{/* Header */}
<div className="p-4 border-b border-gray-700">
{responsive && (
<div className="flex items-center justify-between mb-3 md:hidden">
<span className="text-lg font-semibold">Sessions</span>
<button
@@ -184,60 +172,102 @@ export function Sidebar({
<X size={20} />
</button>
</div>
<button
onClick={handleCreate}
className="w-full flex items-center justify-center gap-2 px-4 py-2 bg-primary-600 hover:bg-primary-700 rounded-lg transition-colors"
>
<Plus size={18} />
<span>New Chat</span>
</button>
</div>
)}
<motion.button
whileHover={{ scale: 1.02 }}
whileTap={{ scale: 0.98 }}
onClick={handleCreate}
className="w-full flex items-center justify-center gap-2 px-4 py-2 bg-primary-600 hover:bg-primary-700 rounded-lg transition-colors"
>
<Plus size={18} />
<span>New Chat</span>
</motion.button>
</div>
{/* Session List */}
<div className="flex-1 overflow-y-auto">
{isLoading ? (
<div className="p-4 text-center text-gray-500">
<RefreshCw className="animate-spin inline-block" size={20} />
</div>
) : sessions.length === 0 ? (
<div className="p-4 text-center text-gray-500">No conversations yet</div>
) : (
<div className="p-2 space-y-1">
{/* Session List */}
<div className="flex-1 overflow-y-auto">
{isLoading ? (
<LoadingState />
) : sessions.length === 0 ? (
<EmptyState />
) : (
<div className="p-2 space-y-1">
<AnimatePresence mode="popLayout">
{sessions.map((session) => (
<div
key={session.id}
onClick={() => handleSelectSession(session.id)}
className={clsx(
'flex items-center gap-2 p-3 rounded-lg cursor-pointer group',
'hover:bg-gray-700 transition-colors',
'active:bg-gray-600', // 触摸反馈
currentSessionId === session.id && 'bg-gray-700'
)}
>
<MessageSquare size={18} className="text-gray-400 flex-shrink-0" />
<div className="flex-1 min-w-0">
<div className="text-sm truncate">
{session.name || `Chat ${session.id.slice(0, 8)}`}
</div>
<div className="text-xs text-gray-500">{session.messageCount} messages</div>
</div>
<button
onClick={(e) => handleDelete(session.id, e)}
className="opacity-0 group-hover:opacity-100 p-1 hover:bg-gray-600 rounded transition-all md:opacity-0"
aria-label="Delete session"
>
<Trash2 size={14} className="text-gray-400" />
</button>
</div>
<SessionItem key={session.id} session={session} />
))}
</div>
)}
</div>
</AnimatePresence>
</div>
)}
</div>
{/* Footer */}
<div className="p-4 border-t border-gray-700 text-center text-xs text-gray-500">
AI Assistant v1.0
{/* Footer */}
<div className="p-4 border-t border-gray-700 text-center text-xs text-gray-500">
AI Assistant v1.0
</div>
</>
);
// 非响应式模式
if (!responsive) {
return (
<div className="w-64 bg-gray-800 border-r border-gray-700 flex flex-col">
<SidebarContent />
</div>
);
}
// 响应式模式
return (
<>
{/* 移动端菜单按钮 */}
<motion.button
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
onClick={() => setIsOpen(true)}
className="fixed top-3 left-3 z-40 p-2 rounded-lg bg-gray-800 text-gray-300 hover:bg-gray-700 transition-colors md:hidden"
aria-label="Open menu"
>
<Menu size={20} />
</motion.button>
{/* 遮罩层 */}
<AnimatePresence>
{isOpen && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.2 }}
className="fixed inset-0 bg-black/50 z-40 md:hidden"
onClick={handleOverlayClick}
/>
)}
</AnimatePresence>
{/* 侧边栏 */}
<motion.div
initial={false}
animate={{ x: isOpen ? 0 : '-100%' }}
transition={{ type: 'spring', stiffness: 300, damping: 30 }}
className={cn(
'fixed md:static inset-y-0 left-0 z-50',
'w-64 bg-gray-800 border-r border-gray-700 flex flex-col',
'md:transform-none md:translate-x-0'
)}
style={{ transform: 'none' }}
>
<div className="hidden md:flex flex-col h-full">
<SidebarContent />
</div>
<div className="flex md:hidden flex-col h-full">
<SidebarContent />
</div>
</motion.div>
{/* 桌面端固定侧边栏 */}
<div className="hidden md:flex w-64 bg-gray-800 border-r border-gray-700 flex-col">
<SidebarContent />
</div>
</>
);
+44
View File
@@ -0,0 +1,44 @@
import { cn } from '../utils/cn';
interface SkeletonProps extends React.HTMLAttributes<HTMLDivElement> {}
export function Skeleton({ className, ...props }: SkeletonProps) {
return (
<div
className={cn('animate-pulse rounded-md bg-gray-700', className)}
{...props}
/>
);
}
export function MessageSkeleton() {
return (
<div className="flex gap-3 animate-pulse">
<div className="w-8 h-8 rounded-full bg-gray-700" />
<div className="flex-1 space-y-2">
<div className="h-4 bg-gray-700 rounded w-1/4" />
<div className="h-4 bg-gray-700 rounded w-3/4" />
<div className="h-4 bg-gray-700 rounded w-1/2" />
</div>
</div>
);
}
export function SessionSkeleton() {
return (
<div className="px-2 py-2 animate-pulse">
<div className="h-10 bg-gray-700 rounded-lg" />
</div>
);
}
export function FileSkeleton() {
return (
<div className="px-3 py-2 animate-pulse">
<div className="flex items-center gap-2">
<div className="w-4 h-4 bg-gray-700 rounded" />
<div className="h-4 bg-gray-700 rounded flex-1" />
</div>
</div>
);
}
+17
View File
@@ -0,0 +1,17 @@
import { Toaster as SonnerToaster } from 'sonner';
export function Toaster() {
return (
<SonnerToaster
position="top-right"
toastOptions={{
style: {
background: '#1f2937',
border: '1px solid #374151',
color: '#f3f4f6',
},
}}
richColors
/>
);
}