File size: 9,725 Bytes
e058d03
 
 
8b65a0f
 
 
 
 
 
 
 
 
 
 
 
34d6b36
8b65a0f
75db55d
e058d03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75db55d
e058d03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75db55d
 
e058d03
 
 
 
 
 
 
 
 
 
 
 
 
75db55d
 
e058d03
 
 
 
 
 
4567d57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e058d03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b65a0f
 
 
a77d40e
8b65a0f
 
 
 
34d6b36
8b65a0f
 
fe844e6
 
 
0b95fd8
fe844e6
 
 
 
 
 
 
34d6b36
fe844e6
 
75db55d
 
7bdfdfb
75db55d
 
 
 
 
0b95fd8
75db55d
 
d502def
75db55d
 
 
 
 
 
 
 
 
 
fe844e6
 
 
 
 
 
 
 
 
 
 
 
34d6b36
 
 
 
 
 
 
 
fe844e6
 
 
 
 
 
 
8b65a0f
 
e058d03
fe844e6
8b65a0f
34d6b36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e058d03
 
fe844e6
e058d03
fe844e6
 
8b65a0f
 
fe844e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b65a0f
e058d03
8b65a0f
 
e058d03
8b65a0f
 
 
 
34d6b36
 
8b65a0f
 
 
 
0b95fd8
8b65a0f
 
 
 
e058d03
 
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
'use client';

import { useState, useEffect } from 'react';
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogFooter,
} from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import { ScrollArea } from '@/components/ui/scroll-area';
import { CheckboxField } from '@/components/ui/checkbox-field';
import { Loader2 } from 'lucide-react';
import { Select, SelectTrigger, SelectContent, SelectItem, SelectValue } from '@/components/ui/select';

interface ArticleGenerationModalProps {
  isOpen: boolean;
  onClose: () => void;
  onGenerate: (params: ArticleGenerationParams) => void;
  isGenerating: boolean;
}

export interface ArticleGenerationParams {
  publisher: string[];
  grade: string[];
  unit: string[];
  textbookVocab: string;
  additionalVocab: string;
  grammar: string[];
  topic: string[];
  inputArticle: string;
  cefr: string; // single-select: A1..C2
}

// Options based on the Python entry_form.py
const PUBLISHER_OPTIONS = [
  '康軒', '南一', '翰林'
];

const GRADE_OPTIONS = [
  '七年級上學期', '七年級下學期',
  '八年級上學期', '八年級下學期',
  '九年級上學期', '九年級下學期'
];

const UNIT_OPTIONS = [
  '第一課', '第二課', '第三課', '第四課', '第五課', '第六課'
];

const GRAMMAR_OPTIONS = [
  '名詞', '動詞', '形容詞', '副詞', '介系詞', '連接詞', '助詞'
];

const TOPIC_OPTIONS = [
  '家庭', '學校', '運動', '食物', '動物', '旅遊', '科技', '環境', '友誼', '夢想'
];

export function ArticleGenerationModal({
  isOpen,
  onClose,
  onGenerate,
  isGenerating
}: ArticleGenerationModalProps) {
  const [formData, setFormData] = useState<ArticleGenerationParams>({
    publisher: [],
    grade: [],
    unit: [],
    textbookVocab: '',
    additionalVocab: '',
    grammar: [],
    topic: [],
    inputArticle: '',
    cefr: ''
  });

  // Reset form when modal opens
  useEffect(() => {
    if (isOpen) {
      setFormData({
        publisher: [],
        grade: [],
        unit: [],
        textbookVocab: '',
        additionalVocab: '',
        grammar: [],
        topic: [],
        inputArticle: '',
        cefr: ''
      });
    }
  }, [isOpen]);

  // Update textbook vocabulary when selections change
  useEffect(() => {
    let cancelled = false;
    const fetchVocab = async () => {
      if (formData.grade.length > 0 && formData.unit.length > 0 && formData.publisher.length > 0) {
        try {
          const res = await fetch('/api/vocab', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
              publisher: formData.publisher,
              grade: formData.grade,
              unit: formData.unit,
            }),
          });
          if (!res.ok) throw new Error('Failed to fetch vocab');
          const data: { vocab: string[]; text: string } = await res.json();
          if (!cancelled) {
            setFormData(prev => ({ ...prev, textbookVocab: data.text }));
          }
        } catch (e) {
          if (!cancelled) {
            setFormData(prev => ({ ...prev, textbookVocab: '' }));
          }
        }
      } else {
        if (!cancelled) {
          setFormData(prev => ({ ...prev, textbookVocab: '' }));
        }
      }
    };
    fetchVocab();
    return () => { cancelled = true; };
  }, [formData.grade, formData.unit, formData.publisher]);

  const handleCheckboxChange = (field: keyof ArticleGenerationParams, value: string) => {
    setFormData(prev => {
      const currentArray = prev[field] as string[];
      const newArray = currentArray.includes(value)
        ? currentArray.filter(item => item !== value)
        : [...currentArray, value];
      return { ...prev, [field]: newArray };
    });
  };

  const handleTextChange = (field: keyof ArticleGenerationParams, value: string) => {
    setFormData(prev => ({ ...prev, [field]: value }));
  };

  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault();
    onGenerate(formData);
  };

  return (
    <Dialog open={isOpen} onOpenChange={onClose}>
      <DialogContent className="max-w-4xl max-h-[90vh]">
        <DialogHeader>
          <DialogTitle>📝 Generate / Rewrite Article</DialogTitle>
          <DialogDescription>
            Configure the parameters for article generation
          </DialogDescription>
        </DialogHeader>

        <ScrollArea className="h-[calc(90vh-200px)] pr-4">
          <form onSubmit={handleSubmit} className="space-y-6">
            {/* 初始文章 */}
            <div>
              <Label htmlFor="input-article" className="mb-2">
                文章 ( 如需改寫文章可輸入在此,無則留空 )
              </Label>
              <Textarea
                id="input-article"
                value={formData.inputArticle}
                onChange={(e) => handleTextChange('inputArticle', e.target.value)}
                placeholder="Enter an initial article or topic to base the generation on..."
                className="min-h-[120px]"
              />
            </div>

            {/* CEFR level */}
            <div className="grid grid-cols-1 gap-2">
              <Label htmlFor="cefr" className="mb-2">CEFR Level ( 只會影響到文章,不會影響到題目 )</Label>
              <Select
                value={formData.cefr}
                onValueChange={(v) => setFormData(prev => ({ ...prev, cefr: v }))}
              >
                <SelectTrigger id="cefr" className="w-full">
                  <SelectValue placeholder="Select CEFR level (Below A1 - C2)" />
                </SelectTrigger>
                <SelectContent>
                  <SelectItem value="Below A1">Below A1</SelectItem>
                  <SelectItem value="A1">A1</SelectItem>
                  <SelectItem value="A2">A2</SelectItem>
                  <SelectItem value="B1">B1</SelectItem>
                  <SelectItem value="B2">B2</SelectItem>
                  <SelectItem value="C1">C1</SelectItem>
                  <SelectItem value="C2">C2</SelectItem>
                </SelectContent>
              </Select>
            </div>

            {/* 出版社 */}
            <CheckboxField
              label="出版社 (Publisher)"
              fieldName="publisher"
              value={formData.publisher}
              onChange={(value: string) => handleCheckboxChange('publisher', value)}
              options={PUBLISHER_OPTIONS}
              columns={1}
            />

            {/* 學生年級 + 課程範圍 */}
            <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
              <CheckboxField
                label="學生年級 (Grade Level)"
                fieldName="grade"
                value={formData.grade}
                onChange={(value: string) => handleCheckboxChange('grade', value)}
                options={GRADE_OPTIONS}
                columns={1}
              />
              <CheckboxField
                label="課程範圍 (Unit Range)"
                fieldName="unit"
                value={formData.unit}
                onChange={(value: string) => handleCheckboxChange('unit', value)}
                options={UNIT_OPTIONS}
                columns={1}
              />
            </div>

            {/* 文法範圍 + 主題範圍 */}
            <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
              <CheckboxField
                label="文法範圍 (Grammar Focus)"
                fieldName="grammar"
                value={formData.grammar}
                onChange={(value: string) => handleCheckboxChange('grammar', value)}
                options={GRAMMAR_OPTIONS}
                columns={1}
              />
              <CheckboxField
                label="主題範圍 (Topic)"
                fieldName="topic"
                value={formData.topic}
                onChange={(value: string) => handleCheckboxChange('topic', value)}
                options={TOPIC_OPTIONS}
                columns={1}
              />
            </div>

            {/* 單字列表 */}
            <div>
              <Label htmlFor="textbook-vocab" className="mb-2">
                課本單字列表 (Textbook Vocabulary)
              </Label>
              <Textarea
                id="textbook-vocab"
                value={formData.textbookVocab}
                readOnly
                placeholder="Select units to see vocabulary"
                className="bg-muted"
              />
            </div>

            {/* 額外單字列表 */}
            <div>
              <Label htmlFor="additional-vocab" className="mb-2">
                額外單字列表 (Additional Vocabulary)
              </Label>
              <Textarea
                id="additional-vocab"
                value={formData.additionalVocab}
                onChange={(e) => handleTextChange('additionalVocab', e.target.value)}
                placeholder="Enter additional vocabulary words, separated by commas"
              />
            </div>
          </form>
        </ScrollArea>

        <DialogFooter>
          <Button type="button" variant="outline" onClick={onClose}>
            Cancel
          </Button>
          <Button
            type="submit"
            onClick={handleSubmit}
            disabled={isGenerating}
          >
            {isGenerating && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
            {isGenerating ? '生成中...' : '生成 / 改寫文章'}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}