File size: 9,366 Bytes
d47b053 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | /**
* 工作空间 - 合并 历史记录 / 提示词管理 / 用量统计
*/
import { useState, type ReactNode } from 'react';
import { HistoryPanel } from './HistoryPanel';
import { usePrompts } from '../hooks/usePrompts';
import { PromptSidebar } from './PromptSidebar';
import { UsageDashboardContent } from './UsageDashboard';
import type { RoleType, SharedModuleType } from '../types/api';
import { useI18n } from '../i18n';
import { useModalTransition } from '../hooks/useModalTransition';
type WorkspaceModule = 'history' | 'prompts' | 'usage';
interface WorkspaceProps {
isOpen: boolean;
onClose: () => void;
initialModule?: WorkspaceModule;
onReusePrompt?: (prompt: string) => void;
}
export function Workspace({ isOpen, onClose, initialModule = 'history', onReusePrompt }: WorkspaceProps) {
const { t } = useI18n();
const { shouldRender, isExiting } = useModalTransition(isOpen, 400);
const [activeModule, setActiveModule] = useState<WorkspaceModule>(initialModule);
const {
isLoading: promptsLoading,
selection,
setSelection,
getCurrentContent,
setCurrentContent,
restoreCurrent,
hasOverride,
} = usePrompts();
if (!shouldRender) return null;
const breadcrumbMap: Record<WorkspaceModule, string> = {
history: t('workspace.breadcrumb.history'),
prompts: t('workspace.breadcrumb.prompts'),
usage: t('workspace.breadcrumb.usage'),
};
const railItems: { id: WorkspaceModule; label: string; icon: ReactNode }[] = [
{
id: 'history',
label: t('workspace.rail.history'),
icon: (
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
),
},
{
id: 'prompts',
label: t('workspace.rail.prompts'),
icon: (
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M7 8h10M7 12h4m1 8l-4-4H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-3l-4 4z" />
</svg>
),
},
{
id: 'usage',
label: t('workspace.rail.usage'),
icon: (
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 19h16M6 16V9m6 7V5m6 11v-4" />
</svg>
),
},
];
const getPromptTitle = () => {
const roleLabels: Record<RoleType, string> = {
problemFraming: t('prompts.role.problemFraming'),
conceptDesigner: t('prompts.role.conceptDesigner'),
codeGeneration: t('prompts.role.codeGeneration'),
codeRetry: t('prompts.role.codeRetry'),
codeEdit: t('prompts.role.codeEdit'),
};
const sharedLabels: Record<SharedModuleType, string> = {
apiIndex: t('prompts.shared.apiIndex'),
specification: t('prompts.shared.specification'),
};
if (selection.kind === 'role') {
const roleLabel = roleLabels[selection.role];
return selection.promptType === 'system'
? t('prompts.role.systemTitle', { role: roleLabel })
: t('prompts.role.userTitle', { role: roleLabel });
}
return sharedLabels[selection.module];
};
const getPromptDescription = () => {
if (selection.kind === 'role') {
return selection.promptType === 'system'
? t('prompts.role.systemDescription')
: t('prompts.role.userDescription');
}
return selection.module === 'apiIndex'
? t('prompts.shared.apiIndexDescription')
: t('prompts.shared.specificationDescription');
};
const promptContent = getCurrentContent();
const isModified = hasOverride();
return (
<div
className={`fixed inset-0 z-[120] flex flex-col bg-bg-primary transition-all duration-500 ${
isExiting ? 'opacity-0 scale-[1.02] blur-xl' : 'opacity-100 scale-100 blur-0 animate-studio-entrance'
}`}
>
{/* 顶栏 */}
<div className="h-16 bg-bg-secondary/50 border-b border-border/5 flex items-center justify-between px-6">
<div className="flex items-center gap-4">
<button
onClick={onClose}
className="p-2.5 text-text-secondary/50 hover:text-text-primary hover:bg-bg-primary/50 rounded-2xl transition-all"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 19l-7-7 7-7" />
</svg>
</button>
<div className="flex items-center gap-3">
<div className="w-1.5 h-1.5 rounded-full bg-accent-rgb/40" />
<span className="text-lg font-medium text-text-primary/90 tracking-tight">{t('workspace.title')}</span>
</div>
</div>
<div className="flex items-center gap-6">
{activeModule === 'prompts' && isModified && (
<div className="flex items-center gap-3 animate-fade-in">
<span className="text-[10px] uppercase tracking-widest text-accent-rgb/60">{t('prompts.modified')}</span>
<button
onClick={restoreCurrent}
className="px-4 py-1.5 text-[10px] uppercase tracking-widest bg-accent-rgb/5 text-accent-rgb/70 hover:bg-accent-rgb/10 hover:text-accent-rgb rounded-full transition-all"
>
{t('prompts.restore')}
</button>
</div>
)}
<div className="text-[10px] text-text-secondary/30 uppercase tracking-[0.25em] font-light">
Workspace / <span className="text-text-secondary/60 font-medium">{breadcrumbMap[activeModule]}</span>
</div>
</div>
</div>
{/* 核心交互区 */}
<div className="flex-1 flex overflow-hidden">
{/* 左侧 Rail 导航 */}
<aside className="w-20 bg-bg-secondary/20 border-r border-border/5 flex flex-col items-center py-8 gap-4">
{railItems.map(item => (
<button
key={item.id}
onClick={() => setActiveModule(item.id)}
className={`w-12 h-12 flex items-center justify-center rounded-2xl transition-all duration-300 ${
activeModule === item.id
? 'bg-bg-tertiary text-text-primary shadow-sm'
: 'text-text-secondary/40 hover:text-text-primary hover:bg-bg-secondary/50'
}`}
title={item.label}
>
{item.icon}
</button>
))}
</aside>
{/* 模块展示区 */}
<div className="flex-1 flex overflow-hidden">
{activeModule === 'history' && (
<main className="flex-1 p-10 overflow-y-auto bg-bg-primary/30 animate-fade-in">
<HistoryPanel isActive={activeModule === 'history'} onReusePrompt={onReusePrompt} />
</main>
)}
{activeModule === 'prompts' && (
<div className="flex-1 flex overflow-hidden animate-fade-in">
<PromptSidebar selection={selection} onSelect={setSelection} />
<div className="flex-1 flex flex-col overflow-hidden">
<div className="px-10 py-8 border-b border-border/5">
<h2 className="text-xl font-medium text-text-primary/90 tracking-tight">{getPromptTitle()}</h2>
<p className="text-sm text-text-secondary/50 mt-2 font-light">{getPromptDescription()}</p>
</div>
<div className="flex-1 p-8 overflow-hidden">
{promptsLoading ? (
<div className="h-full flex items-center justify-center text-text-secondary/30 text-sm tracking-widest uppercase">
{t('common.loading')}
</div>
) : (
<textarea
value={promptContent}
onChange={e => setCurrentContent(e.target.value)}
className="w-full h-full px-8 py-8 bg-bg-secondary/30 border border-border/5 rounded-[2rem] text-[15px] text-text-primary/80 font-mono leading-relaxed resize-none focus:outline-none focus:border-accent-rgb/20 focus:bg-bg-secondary/50 transition-all shadow-inner scrollbar-hide"
placeholder={t('prompts.placeholder')}
/>
)}
</div>
<div className="px-10 py-5 border-t border-border/5 flex items-center justify-between text-[10px] uppercase tracking-widest text-text-secondary/30 font-light">
<span>{t('prompts.characters', { count: promptContent.length })}</span>
<span className="flex items-center gap-2">
<div className="w-1 h-1 rounded-full bg-green-500/40 animate-pulse" />
{t('prompts.autosave')}
</span>
</div>
</div>
</div>
)}
{activeModule === 'usage' && (
<main className="flex-1 overflow-y-auto bg-bg-primary/30 animate-fade-in">
<UsageDashboardContent isActive={activeModule === 'usage'} />
</main>
)}
</div>
</div>
</div>
);
}
|