Prmbr/src/app/studio/[id]/page.tsx
2025-07-30 00:25:54 +08:00

505 lines
20 KiB
TypeScript

'use client'
import { useEffect, useState } from 'react'
import { useTranslations } from 'next-intl'
import { useAuth } from '@/hooks/useAuth'
import { useRouter } from 'next/navigation'
import { Header } from '@/components/layout/Header'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Textarea } from '@/components/ui/textarea'
import { LoadingSpinner } from '@/components/ui/loading-spinner'
import {
Play,
Save,
Copy,
Settings,
Plus,
Search,
Filter,
MoreHorizontal,
Zap,
History,
ArrowLeft,
FileText,
Clock
} from 'lucide-react'
interface PromptPageProps {
params: Promise<{ id: string }>
}
interface PromptData {
id: string
name: string
description: string | null
content: string
tags: string[]
createdAt: string
updatedAt: string
lastUsed?: string | null
currentVersion?: number
usage?: number
}
export default function PromptPage({ params }: PromptPageProps) {
const [promptId, setPromptId] = useState<string>('')
useEffect(() => {
params.then(p => setPromptId(p.id))
}, [params])
const { user, loading } = useAuth()
const router = useRouter()
const t = useTranslations('studio')
const tCommon = useTranslations('common')
const [prompt, setPrompt] = useState<PromptData | null>(null)
const [promptContent, setPromptContent] = useState('')
const [promptTitle, setPromptTitle] = useState('')
const [testResult, setTestResult] = useState('')
const [isRunning, setIsRunning] = useState(false)
const [isSaving, setIsSaving] = useState(false)
const [isLoading, setIsLoading] = useState(true)
const fetchPrompt = async () => {
if (!user || !promptId) return
try {
setIsLoading(true)
const response = await fetch(`/api/prompts/${promptId}?userId=${user.id}`)
if (response.ok) {
const data = await response.json()
setPrompt(data)
setPromptContent(data.content)
setPromptTitle(data.name)
} else {
router.push('/studio')
}
} catch (error) {
console.error('Error fetching prompt:', error)
router.push('/studio')
} finally {
setIsLoading(false)
}
}
useEffect(() => {
if (!loading && !user) {
router.push('/signin')
} else if (user && promptId) {
fetchPrompt()
}
}, [user, loading, router, promptId, fetchPrompt])
const handleRunTest = async () => {
if (!promptContent.trim() || !user || !promptId) return
setIsRunning(true)
setTestResult('')
try {
const response = await fetch(`/api/prompts/${promptId}/test`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
content: promptContent,
userId: user.id
})
})
if (response.ok) {
const data = await response.json()
setTestResult(data.result.output || data.result.error || 'No output received')
} else {
setTestResult('Error: Failed to run prompt test. Please try again.')
}
} catch (error) {
console.error('Error running test:', error)
setTestResult('Error: Network error occurred. Please try again.')
} finally {
setIsRunning(false)
}
}
const handleSavePrompt = async () => {
if (!user || !promptId) return
setIsSaving(true)
try {
const response = await fetch(`/api/prompts/${promptId}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: promptTitle,
content: promptContent,
userId: user.id
})
})
if (response.ok) {
const updatedPrompt = await response.json()
setPrompt(updatedPrompt)
// Show success message
}
} catch (error) {
console.error('Failed to save prompt:', error)
} finally {
setIsSaving(false)
}
}
const copyToClipboard = (text: string) => {
navigator.clipboard.writeText(text)
// Show success message
}
if (loading || isLoading) {
return (
<div className="min-h-screen flex items-center justify-center">
<div className="text-center">
<LoadingSpinner size="lg" />
<p className="mt-4 text-muted-foreground">{t('loadingStudio')}</p>
</div>
</div>
)
}
if (!user || !prompt) {
return null
}
return (
<div className="min-h-screen bg-background">
<Header />
{/* Top Navigation */}
<div className="border-b bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60">
<div className="max-w-7xl mx-auto px-4 py-4">
<div className="flex items-center justify-between">
<div className="flex items-center space-x-4">
<Button
variant="ghost"
size="sm"
onClick={() => router.push('/studio')}
className="flex items-center space-x-2"
>
<ArrowLeft className="h-4 w-4" />
<span>{t('backToList')}</span>
</Button>
<div className="h-6 w-px bg-border" />
<div className="flex items-center space-x-3">
<div className="flex items-center space-x-2">
<FileText className="h-5 w-5 text-muted-foreground" />
<div>
<h1 className="font-semibold text-foreground">{prompt.name}</h1>
<p className="text-xs text-muted-foreground">Version {prompt.currentVersion || 1} {prompt.usage || 0} uses</p>
</div>
</div>
</div>
</div>
<div className="flex items-center space-x-2">
<Button variant="outline" size="sm" className="flex items-center space-x-1">
<History className="h-4 w-4" />
<span className="hidden sm:inline">{t('versionHistory')}</span>
</Button>
<Button variant="outline" size="sm" className="flex items-center space-x-1">
<Copy className="h-4 w-4" />
<span className="hidden sm:inline">Duplicate</span>
</Button>
<Button variant="outline" size="sm" className="flex items-center space-x-1">
<Settings className="h-4 w-4" />
<span className="hidden sm:inline">Settings</span>
</Button>
</div>
</div>
</div>
</div>
<div className="max-w-7xl mx-auto p-4">
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
{/* Left Sidebar - Prompt List */}
<div className="lg:col-span-1">
<div className="bg-card rounded-lg border border-border overflow-hidden">
<div className="p-4 border-b border-border">
<div className="flex items-center justify-between mb-3">
<h2 className="font-semibold text-foreground">{t('myPrompts')}</h2>
<Button size="sm" variant="outline" className="h-7 w-7 p-0">
<Plus className="h-3 w-3" />
</Button>
</div>
{/* Search */}
<div className="relative">
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-muted-foreground" />
<Input
placeholder={t('searchPrompts')}
className="pl-9 h-8"
/>
</div>
</div>
{/* Filters */}
<div className="p-4 border-b border-border">
<div className="flex items-center justify-between text-sm">
<Button variant="ghost" size="sm" className="h-7 px-2 text-xs">
<Filter className="h-3 w-3 mr-1" />
{t('filter')}
</Button>
<span className="text-muted-foreground">3 prompts</span>
</div>
</div>
{/* Prompt List */}
<div className="max-h-96 overflow-y-auto">
{/* Active Prompt */}
<div className="p-3 border-b border-border bg-muted/50">
<div className="flex items-start space-x-3">
<div className="w-2 h-2 rounded-full bg-primary mt-2 flex-shrink-0"></div>
<div className="flex-1 min-w-0">
<h3 className="font-medium text-foreground text-sm truncate">{prompt.name}</h3>
<p className="text-xs text-muted-foreground mt-1 line-clamp-2">{prompt.description}</p>
<div className="flex items-center mt-2 space-x-2">
{prompt.tags.slice(0, 1).map((tag) => (
<span key={tag} className="inline-flex items-center px-1.5 py-0.5 text-xs font-medium bg-primary/10 text-primary rounded">
{tag}
</span>
))}
{prompt.tags.length > 1 && (
<span className="text-xs text-muted-foreground">+{prompt.tags.length - 1}</span>
)}
</div>
</div>
<Button variant="ghost" size="sm" className="h-6 w-6 p-0 opacity-0 group-hover:opacity-100">
<MoreHorizontal className="h-3 w-3" />
</Button>
</div>
</div>
{/* Other Prompts */}
<div className="p-3 border-b border-border hover:bg-muted/30 cursor-pointer group">
<div className="flex items-start space-x-3">
<div className="w-2 h-2 rounded-full bg-muted-foreground/30 mt-2 flex-shrink-0"></div>
<div className="flex-1 min-w-0">
<h3 className="font-medium text-foreground text-sm truncate">Code Review Assistant</h3>
<p className="text-xs text-muted-foreground mt-1 line-clamp-2">Help review code for best practices and potential issues</p>
<div className="flex items-center mt-2 space-x-2">
<span className="inline-flex items-center px-1.5 py-0.5 text-xs font-medium bg-muted text-muted-foreground rounded">
development
</span>
</div>
</div>
<Button variant="ghost" size="sm" className="h-6 w-6 p-0 opacity-0 group-hover:opacity-100">
<MoreHorizontal className="h-3 w-3" />
</Button>
</div>
</div>
<div className="p-3 hover:bg-muted/30 cursor-pointer group">
<div className="flex items-start space-x-3">
<div className="w-2 h-2 rounded-full bg-muted-foreground/30 mt-2 flex-shrink-0"></div>
<div className="flex-1 min-w-0">
<h3 className="font-medium text-foreground text-sm truncate">Content Summarizer</h3>
<p className="text-xs text-muted-foreground mt-1 line-clamp-2">Summarize long articles into key points</p>
<div className="flex items-center mt-2 space-x-2">
<span className="inline-flex items-center px-1.5 py-0.5 text-xs font-medium bg-muted text-muted-foreground rounded">
content
</span>
</div>
</div>
<Button variant="ghost" size="sm" className="h-6 w-6 p-0 opacity-0 group-hover:opacity-100">
<MoreHorizontal className="h-3 w-3" />
</Button>
</div>
</div>
</div>
</div>
{/* Prompt Info */}
<div className="mt-6 bg-card rounded-lg border border-border p-4">
<h3 className="font-semibold text-foreground mb-3">Prompt Info</h3>
<div className="space-y-3 text-sm">
<div className="flex items-center justify-between">
<span className="text-muted-foreground">Created</span>
<span className="text-foreground">{new Date(prompt.createdAt).toLocaleDateString()}</span>
</div>
<div className="flex items-center justify-between">
<span className="text-muted-foreground">Updated</span>
<span className="text-foreground">{new Date(prompt.updatedAt).toLocaleDateString()}</span>
</div>
<div className="flex items-center justify-between">
<span className="text-muted-foreground">Last used</span>
<span className="text-foreground">{prompt.lastUsed ? new Date(prompt.lastUsed).toLocaleDateString() : 'Never'}</span>
</div>
<div className="flex items-center justify-between">
<span className="text-muted-foreground">Usage count</span>
<span className="text-foreground">{prompt.usage || 0}</span>
</div>
<div className="pt-2 border-t border-border">
<div className="flex flex-wrap gap-1">
{prompt.tags.map((tag: string) => (
<span
key={tag}
className="inline-flex items-center px-2 py-1 text-xs font-medium bg-muted text-muted-foreground rounded-full"
>
{tag}
</span>
))}
</div>
</div>
</div>
</div>
</div>
{/* Main Content */}
<div className="lg:col-span-2 space-y-6">
{/* Action Bar */}
<div className="flex items-center justify-between">
<div className="flex items-center space-x-2">
<Button
onClick={handleSavePrompt}
disabled={isSaving}
className="flex items-center space-x-2"
>
{isSaving ? (
<LoadingSpinner size="sm" className="mr-2" />
) : (
<Save className="h-4 w-4 mr-2" />
)}
{tCommon('save')}
</Button>
<Button
variant="outline"
onClick={handleRunTest}
disabled={isRunning || !promptContent.trim()}
className="flex items-center space-x-2"
>
{isRunning ? (
<LoadingSpinner size="sm" className="mr-2" />
) : (
<Play className="h-4 w-4 mr-2" />
)}
{t('runTest')}
</Button>
<Button
variant="outline"
onClick={() => copyToClipboard(promptContent)}
className="flex items-center space-x-2"
>
<Copy className="h-4 w-4 mr-2" />
Copy
</Button>
</div>
<div className="flex items-center space-x-2 text-sm text-muted-foreground">
<Clock className="h-4 w-4" />
<span>Auto-save enabled</span>
</div>
</div>
{/* Editor */}
<div className="grid grid-cols-1 xl:grid-cols-2 gap-6">
{/* Prompt Editor */}
<div className="bg-card rounded-lg border border-border">
<div className="p-4 border-b border-border">
<div className="flex items-center justify-between">
<h3 className="font-semibold text-foreground">{t('promptEditor')}</h3>
<div className="flex items-center space-x-1 text-xs text-muted-foreground">
<span>{promptContent.length} characters</span>
</div>
</div>
</div>
<div className="p-4">
<div className="space-y-4">
<div>
<Label htmlFor="promptTitle" className="text-sm font-medium">
{t('promptName')}
</Label>
<Input
id="promptTitle"
value={promptTitle}
onChange={(e) => setPromptTitle(e.target.value)}
className="mt-1"
/>
</div>
<div>
<Label htmlFor="promptContent" className="text-sm font-medium">
{t('promptContent')}
</Label>
<Textarea
id="promptContent"
value={promptContent}
onChange={(e) => setPromptContent(e.target.value)}
className="mt-1 min-h-[400px] font-mono text-sm resize-none"
placeholder="Write your prompt here..."
/>
</div>
</div>
</div>
</div>
{/* Test Results */}
<div className="bg-card rounded-lg border border-border">
<div className="p-4 border-b border-border">
<div className="flex items-center justify-between">
<h3 className="font-semibold text-foreground">{t('testResults')}</h3>
{testResult && (
<Button
variant="outline"
size="sm"
onClick={() => copyToClipboard(testResult)}
className="h-7"
>
<Copy className="h-3 w-3 mr-1" />
Copy
</Button>
)}
</div>
</div>
<div className="p-4">
<div className="bg-muted rounded-lg min-h-[400px] p-4">
{isRunning ? (
<div className="flex items-center justify-center h-full">
<div className="text-center">
<LoadingSpinner size="lg" />
<p className="mt-4 text-muted-foreground">Running prompt test...</p>
<p className="text-xs text-muted-foreground mt-2">This may take a few seconds</p>
</div>
</div>
) : testResult ? (
<div className="space-y-4">
<pre className="text-sm text-foreground whitespace-pre-wrap leading-relaxed">
{testResult}
</pre>
</div>
) : (
<div className="flex items-center justify-center h-full text-center">
<div>
<Zap className="h-12 w-12 text-muted-foreground mx-auto mb-4" />
<p className="text-muted-foreground mb-2">
{t('clickRunTestToSee')}
</p>
<p className="text-xs text-muted-foreground">
Test your prompt with AI models to see the output
</p>
</div>
</div>
)}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
)
}