| 1 | import React, { useState, useEffect } from 'react'
|
|---|
| 2 | import { Sparkles, Check, X, Clock, ChevronDown, ChevronUp } from 'lucide-react'
|
|---|
| 3 | import { useStoryStore } from '../../store/storyStore'
|
|---|
| 4 | import { useUIStore } from '../../store/uiStore'
|
|---|
| 5 | import { AISuggestion, SuggestionType } from '../../types'
|
|---|
| 6 | import { Button } from '../ui/Button'
|
|---|
| 7 |
|
|---|
| 8 | interface AISuggestionPanelProps {
|
|---|
| 9 | chapterId: number
|
|---|
| 10 | storyId?: number
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | const typeColors: Record<SuggestionType, string> = {
|
|---|
| 14 | grammar: 'bg-blue-500/20 text-blue-300 border-blue-500/30',
|
|---|
| 15 | style: 'bg-violet-500/20 text-violet-300 border-violet-500/30',
|
|---|
| 16 | plot: 'bg-amber-500/20 text-amber-300 border-amber-500/30',
|
|---|
| 17 | character: 'bg-emerald-500/20 text-emerald-300 border-emerald-500/30',
|
|---|
| 18 | pacing: 'bg-pink-500/20 text-pink-300 border-pink-500/30',
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | const SuggestionCard: React.FC<{
|
|---|
| 22 | suggestion: AISuggestion
|
|---|
| 23 | onAccept: () => void
|
|---|
| 24 | onReject: () => void
|
|---|
| 25 | }> = ({ suggestion, onAccept, onReject }) => {
|
|---|
| 26 | const [expanded, setExpanded] = useState(false)
|
|---|
| 27 |
|
|---|
| 28 | return (
|
|---|
| 29 | <div className="bg-slate-800 border border-slate-700 rounded-xl p-4">
|
|---|
| 30 | <div className="flex items-start justify-between gap-3 mb-3">
|
|---|
| 31 | <div className="flex items-center gap-2">
|
|---|
| 32 | <span className={`px-2 py-0.5 text-xs font-medium rounded-full border ${typeColors[suggestion.suggestion_type]}`}>
|
|---|
| 33 | {suggestion.suggestion_type}
|
|---|
| 34 | </span>
|
|---|
| 35 | {suggestion.accepted === true && (
|
|---|
| 36 | <span className="flex items-center gap-1 text-xs text-emerald-400">
|
|---|
| 37 | <Check size={12} /> Accepted
|
|---|
| 38 | </span>
|
|---|
| 39 | )}
|
|---|
| 40 | {suggestion.accepted === false && (
|
|---|
| 41 | <span className="flex items-center gap-1 text-xs text-rose-400">
|
|---|
| 42 | <X size={12} /> Rejected
|
|---|
| 43 | </span>
|
|---|
| 44 | )}
|
|---|
| 45 | {suggestion.accepted === null && (
|
|---|
| 46 | <span className="flex items-center gap-1 text-xs text-amber-400">
|
|---|
| 47 | <Clock size={12} /> Pending
|
|---|
| 48 | </span>
|
|---|
| 49 | )}
|
|---|
| 50 | </div>
|
|---|
| 51 | <button
|
|---|
| 52 | onClick={() => setExpanded(!expanded)}
|
|---|
| 53 | className="text-slate-500 hover:text-slate-300 transition-colors"
|
|---|
| 54 | >
|
|---|
| 55 | {expanded ? <ChevronUp size={16} /> : <ChevronDown size={16} />}
|
|---|
| 56 | </button>
|
|---|
| 57 | </div>
|
|---|
| 58 |
|
|---|
| 59 | <div className="space-y-3">
|
|---|
| 60 | <div>
|
|---|
| 61 | <p className="text-xs text-slate-500 mb-1">Original:</p>
|
|---|
| 62 | <p className="text-sm text-slate-400 bg-slate-900 rounded-lg p-3 italic border border-slate-700">
|
|---|
| 63 | "{suggestion.original_text}"
|
|---|
| 64 | </p>
|
|---|
| 65 | </div>
|
|---|
| 66 | <div>
|
|---|
| 67 | <p className="text-xs text-slate-500 mb-1">Suggested:</p>
|
|---|
| 68 | <p className="text-sm text-emerald-300 bg-emerald-500/5 rounded-lg p-3 border border-emerald-500/20">
|
|---|
| 69 | "{suggestion.suggested_text}"
|
|---|
| 70 | </p>
|
|---|
| 71 | </div>
|
|---|
| 72 |
|
|---|
| 73 | {expanded && (
|
|---|
| 74 | <div>
|
|---|
| 75 | <p className="text-xs text-slate-500 mb-1">Explanation:</p>
|
|---|
| 76 | <p className="text-sm text-slate-400">{suggestion.explanation}</p>
|
|---|
| 77 | {suggestion.applied_at && (
|
|---|
| 78 | <p className="text-xs text-slate-600 mt-2">
|
|---|
| 79 | Applied: {new Date(suggestion.applied_at).toLocaleString()}
|
|---|
| 80 | </p>
|
|---|
| 81 | )}
|
|---|
| 82 | </div>
|
|---|
| 83 | )}
|
|---|
| 84 |
|
|---|
| 85 | {suggestion.accepted === null && (
|
|---|
| 86 | <div className="flex gap-2 pt-1">
|
|---|
| 87 | <Button size="sm" onClick={onAccept} className="flex-1">
|
|---|
| 88 | <Check size={14} />
|
|---|
| 89 | Accept
|
|---|
| 90 | </Button>
|
|---|
| 91 | <Button size="sm" variant="danger" onClick={onReject} className="flex-1">
|
|---|
| 92 | <X size={14} />
|
|---|
| 93 | Reject
|
|---|
| 94 | </Button>
|
|---|
| 95 | </div>
|
|---|
| 96 | )}
|
|---|
| 97 | </div>
|
|---|
| 98 | </div>
|
|---|
| 99 | )
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | export const AISuggestionPanel: React.FC<AISuggestionPanelProps> = ({ chapterId, storyId }) => {
|
|---|
| 103 | const { aiSuggestions, acceptSuggestion, rejectSuggestion, fetchSuggestions } = useStoryStore()
|
|---|
| 104 | const { addToast } = useUIStore()
|
|---|
| 105 | const [tab, setTab] = useState<'pending' | 'accepted' | 'rejected'>('pending')
|
|---|
| 106 | const [loading, setLoading] = useState(false)
|
|---|
| 107 |
|
|---|
| 108 | useEffect(() => {
|
|---|
| 109 | setLoading(true)
|
|---|
| 110 | fetchSuggestions().finally(() => setLoading(false))
|
|---|
| 111 | }, [chapterId])
|
|---|
| 112 |
|
|---|
| 113 | // Filter by storyId if available (backend), else fall back to chapterId (mock)
|
|---|
| 114 | const chapterSuggestions = aiSuggestions.filter(s =>
|
|---|
| 115 | storyId ? (s as any).story_id === storyId : s.chapter_id === chapterId
|
|---|
| 116 | )
|
|---|
| 117 | const pending = chapterSuggestions.filter(s => s.accepted === null)
|
|---|
| 118 | const accepted = chapterSuggestions.filter(s => s.accepted === true)
|
|---|
| 119 | const rejected = chapterSuggestions.filter(s => s.accepted === false)
|
|---|
| 120 |
|
|---|
| 121 | const currentList = tab === 'pending' ? pending : tab === 'accepted' ? accepted : rejected
|
|---|
| 122 |
|
|---|
| 123 | return (
|
|---|
| 124 | <div className="bg-slate-900 border border-slate-700 rounded-2xl p-6">
|
|---|
| 125 | <div className="flex items-center gap-2 mb-4">
|
|---|
| 126 | <Sparkles size={18} className="text-violet-400" />
|
|---|
| 127 | <h3 className="text-white font-semibold">AI Writing Suggestions</h3>
|
|---|
| 128 | <span className="ml-auto text-xs text-slate-500">{chapterSuggestions.length} total</span>
|
|---|
| 129 | </div>
|
|---|
| 130 |
|
|---|
| 131 | {/* Tabs */}
|
|---|
| 132 | <div className="flex gap-1 p-1 bg-slate-800 rounded-xl mb-4">
|
|---|
| 133 | {([
|
|---|
| 134 | { key: 'pending', label: 'Pending', count: pending.length },
|
|---|
| 135 | { key: 'accepted', label: 'Accepted', count: accepted.length },
|
|---|
| 136 | { key: 'rejected', label: 'Rejected', count: rejected.length },
|
|---|
| 137 | ] as const).map(t => (
|
|---|
| 138 | <button
|
|---|
| 139 | key={t.key}
|
|---|
| 140 | onClick={() => setTab(t.key)}
|
|---|
| 141 | className={`flex-1 flex items-center justify-center gap-1.5 py-1.5 rounded-lg text-sm font-medium transition-colors ${
|
|---|
| 142 | tab === t.key
|
|---|
| 143 | ? 'bg-indigo-600 text-white'
|
|---|
| 144 | : 'text-slate-400 hover:text-white'
|
|---|
| 145 | }`}
|
|---|
| 146 | >
|
|---|
| 147 | {t.label}
|
|---|
| 148 | <span className={`text-xs px-1.5 py-0.5 rounded-full ${
|
|---|
| 149 | tab === t.key ? 'bg-indigo-500' : 'bg-slate-700'
|
|---|
| 150 | }`}>
|
|---|
| 151 | {t.count}
|
|---|
| 152 | </span>
|
|---|
| 153 | </button>
|
|---|
| 154 | ))}
|
|---|
| 155 | </div>
|
|---|
| 156 |
|
|---|
| 157 | {/* Suggestions */}
|
|---|
| 158 | <div className="space-y-3">
|
|---|
| 159 | {loading ? (
|
|---|
| 160 | <div className="text-center py-8 text-slate-500 text-sm">
|
|---|
| 161 | <Sparkles size={32} className="mx-auto mb-2 opacity-30 animate-pulse" />
|
|---|
| 162 | Loading suggestions...
|
|---|
| 163 | </div>
|
|---|
| 164 | ) : currentList.length === 0 ? (
|
|---|
| 165 | <div className="text-center py-8 text-slate-500 text-sm">
|
|---|
| 166 | <Sparkles size={32} className="mx-auto mb-2 opacity-30" />
|
|---|
| 167 | No {tab} suggestions
|
|---|
| 168 | </div>
|
|---|
| 169 | ) : (
|
|---|
| 170 | currentList.map(s => (
|
|---|
| 171 | <SuggestionCard
|
|---|
| 172 | key={s.suggestion_id}
|
|---|
| 173 | suggestion={s}
|
|---|
| 174 | onAccept={async () => { await acceptSuggestion(s.suggestion_id); addToast('Suggestion accepted!') }}
|
|---|
| 175 | onReject={async () => { await rejectSuggestion(s.suggestion_id); addToast('Suggestion rejected', 'info') }}
|
|---|
| 176 | />
|
|---|
| 177 | ))
|
|---|
| 178 | )}
|
|---|
| 179 | </div>
|
|---|
| 180 | </div>
|
|---|
| 181 | )
|
|---|
| 182 | }
|
|---|