File size: 7,515 Bytes
17c377a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 { useCallback, useState } from "react";
import { Upload, FileAudio, FileVideo, X, AlertCircle } from "lucide-react";
import { Card } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Textarea } from "@/components/ui/textarea";
import { Label } from "@/components/ui/label";
import { Progress } from "@/components/ui/progress";
import { Badge } from "@/components/ui/badge";

interface FileUploadProps {
  onUpload: (file: File, context: string) => void;
  isUploading: boolean;
  uploadProgress: number;
}

const MAX_FILE_SIZE = 25 * 1024 * 1024; // 25MB
const ACCEPTED_TYPES = ["audio/mpeg", "audio/mp3", "video/mp4"];

export function FileUpload({ onUpload, isUploading, uploadProgress }: FileUploadProps) {
  const [file, setFile] = useState<File | null>(null);
  const [context, setContext] = useState("");
  const [dragOver, setDragOver] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const wordCount = context.trim().split(/\s+/).filter(Boolean).length;
  const isContextValid = wordCount <= 100;

  const validateFile = useCallback((file: File): string | null => {
    if (file.size > MAX_FILE_SIZE) {
      return `File is too large. Maximum size is 25MB. Your file is ${(file.size / 1024 / 1024).toFixed(1)}MB.`;
    }

    const isValidType = ACCEPTED_TYPES.includes(file.type) ||
      file.name.endsWith('.mp3') ||
      file.name.endsWith('.mp4');

    if (!isValidType) {
      return "Invalid file type. Please upload an MP3 or MP4 file.";
    }

    return null;
  }, []);

  const handleFileSelect = useCallback((selectedFile: File) => {
    const validationError = validateFile(selectedFile);
    if (validationError) {
      setError(validationError);
      setFile(null);
      return;
    }
    setError(null);
    setFile(selectedFile);
  }, [validateFile]);

  const handleDrop = useCallback((e: React.DragEvent) => {
    e.preventDefault();
    setDragOver(false);

    const droppedFile = e.dataTransfer.files[0];
    if (droppedFile) {
      handleFileSelect(droppedFile);
    }
  }, [handleFileSelect]);

  const handleDragOver = useCallback((e: React.DragEvent) => {
    e.preventDefault();
    setDragOver(true);
  }, []);

  const handleDragLeave = useCallback((e: React.DragEvent) => {
    e.preventDefault();
    setDragOver(false);
  }, []);

  const handleInputChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
    const selectedFile = e.target.files?.[0];
    if (selectedFile) {
      handleFileSelect(selectedFile);
    }
  }, [handleFileSelect]);

  const handleSubmit = () => {
    if (file && isContextValid) {
      onUpload(file, context);
    }
  };

  const clearFile = () => {
    setFile(null);
    setError(null);
  };

  const getFileIcon = () => {
    if (!file) return null;
    if (file.type.startsWith("video") || file.name.endsWith(".mp4")) {
      return <FileVideo className="h-8 w-8 text-primary" />;
    }
    return <FileAudio className="h-8 w-8 text-primary" />;
  };

  return (
    <div className="space-y-6">
      <Card
        className={`relative border-2 border-dashed transition-colors duration-200 ${dragOver
          ? "border-primary bg-accent/50"
          : error
            ? "border-destructive/50"
            : "border-muted-foreground/25 hover:border-primary/50"
          }`}
        onDrop={handleDrop}
        onDragOver={handleDragOver}
        onDragLeave={handleDragLeave}
      >
        <input
          type="file"
          accept=".mp3,.mp4,audio/mpeg,video/mp4"
          onChange={handleInputChange}
          className="absolute inset-0 w-full h-full opacity-0 cursor-pointer"
          disabled={isUploading}
          data-testid="input-file-upload"
        />

        <div className="p-8 text-center">
          {file ? (
            <div className="space-y-4">
              <div className="flex items-center justify-center gap-3">
                {getFileIcon()}
                <div className="text-left">
                  <p className="font-medium truncate max-w-xs" data-testid="text-file-name">
                    {file.name}
                  </p>
                  <p className="text-sm text-muted-foreground">
                    {(file.size / 1024 / 1024).toFixed(2)} MB
                  </p>
                </div>
                <Button
                  size="icon"
                  variant="ghost"
                  onClick={(e) => {
                    e.stopPropagation();
                    clearFile();
                  }}
                  disabled={isUploading}
                  data-testid="button-clear-file"
                >
                  <X className="h-4 w-4" />
                </Button>
              </div>

              {isUploading && (
                <div className="space-y-2">
                  <Progress value={uploadProgress} className="h-2" />
                  <p className="text-sm text-muted-foreground">
                    Uploading... {uploadProgress}%
                  </p>
                </div>
              )}
            </div>
          ) : (
            <div className="space-y-4">
              <div className="mx-auto w-16 h-16 rounded-full bg-accent flex items-center justify-center">
                <Upload className="h-8 w-8 text-accent-foreground" />
              </div>
              <div>
                <p className="font-medium text-lg">
                  Drop your audio or video file here
                </p>
                <p className="text-sm text-muted-foreground mt-1">
                  or click to browse
                </p>
              </div>
              <div className="flex items-center justify-center gap-2">
                <Badge variant="secondary">MP3</Badge>
                <Badge variant="secondary">MP4</Badge>
                <span className="text-sm text-muted-foreground">up to 25MB</span>
              </div>
            </div>
          )}
        </div>
      </Card>

      {error && (
        <div className="flex items-center gap-2 p-3 rounded-md bg-destructive/10 text-destructive" data-testid="text-upload-error">
          <AlertCircle className="h-4 w-4 flex-shrink-0" />
          <p className="text-sm">{error}</p>
        </div>
      )}

      <div className="space-y-3">
        <div className="flex items-center justify-between">
          <Label htmlFor="context">Context (optional)</Label>
          <span className={`text-xs ${!isContextValid ? "text-destructive" : "text-muted-foreground"}`}>
            {wordCount}/100 words
          </span>
        </div>
        <Textarea
          id="context"
          placeholder="Provide context about the content (e.g., 'A tech podcast about machine learning', 'Interview with John Smith about climate change'). This helps identify subtitle errors more accurately."
          value={context}
          onChange={(e) => setContext(e.target.value)}
          rows={3}
          disabled={isUploading}
          className="resize-none"
          data-testid="input-context"
        />
        {!isContextValid && (
          <p className="text-xs text-destructive">
            Context must be 100 words or less.
          </p>
        )}
      </div>

      <Button
        onClick={handleSubmit}
        disabled={!file || isUploading || !isContextValid}
        className="w-full"
        data-testid="button-start-subtitle-generation"
      >
        {isUploading ? "Uploading..." : "Start Subtitle Generation"}
      </Button>
    </div>
  );
}