# All files / src/components/ui video-upload-field.tsx

0% Statements 0/68
0% Branches 0/43
0% Functions 0/8
0% Lines 0/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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

     'use client';

    import React, { useState, useRef, useCallback } from 'react';
    import { Input } from './input';
    import { Button } from './button';
    import { Progress } from './progress';
    import { Upload, Link, X, FileVideo } from 'lucide-react';
    import { useTranslation } from 'react-i18next';
    import { cn } from '@/lib/utils';

    const SUPPORTED_VIDEO_FORMATS = [
        'video/mp4',
        'video/x-matroska', // mkv
        'video/quicktime', // mov
        'video/x-ms-wmv', // wmv
        'video/x-msvideo', // avi
        'video/webm',
        'video/mpeg',
        'video/x-flv', // flv
        'video/x-vob', // vob (may not be recognized)
    ];

    const ACCEPTED_EXTENSIONS = '.mp4,.mkv,.mov,.wmv,.avi,.webm,.mpeg,.flv,.vob,.m4v,.ts';

    interface VideoUploadFieldProps {
        value: string;
        onChange: (value: string) => void;
        onUpload?: (file: File) => Promise<string>;
        disabled?: boolean;
        placeholder?: string;
        className?: string;
        error?: string;
        /** Content ID or Episode ID for upload endpoint */
        uploadId?: number;
        /** Type of content: 'content' for movies, 'episode' for series episodes */
        uploadType?: 'content' | 'episode';
    }

    export function VideoUploadField({
        value,
        onChange,
        onUpload,
        disabled = false,
        placeholder,
        className,
        error,
        uploadId,
        uploadType = 'content'}: VideoUploadFieldProps) {
        const { t } = useTranslation();
        const [isUploading, setIsUploading] = useState(false);
        const [uploadProgress, setUploadProgress] = useState(0);
        const [selectedFile, setSelectedFile] = useState<File | null>(null);
        const fileInputRef = useRef<HTMLInputElement>(null);

        const handleFileSelect = useCallback(async (event: React.ChangeEvent<HTMLInputElement>) => {
            const file = event.target.files?.[0];
            if (file) {
                // Validate file type
                const ext = file.name.split('.').pop()?.toLowerCase() || '';
                const validExtensions = ['mp4', 'mkv', 'mov', 'wmv', 'avi', 'webm', 'mpeg', 'flv', 'vob', 'm4v', 'ts'];

                if (!validExtensions.includes(ext)) {
                    alert(t('createContent.unsupportedFormat', {
                        format: ext,
                        supported: validExtensions.join(', ')
                    }));
                    return;
                }

                setSelectedFile(file);

                // If we have an upload handler and ID, upload immediately
                if (onUpload && uploadId) {
                    handleUpload(file);
                } else if (onUpload) {
                    // No uploadId yet - call onUpload to get the pending marker
                    try {
                        const result = await onUpload(file);
                        onChange(result);
                    } catch (error) {
                        console.error('Error setting pending upload:', error);
                    }
                }
            }
        }, [onUpload, uploadId, onChange, t]);

        const handleUpload = async (file: File) => {
            if (!uploadId) {
                // Store file for later upload after content creation
                setSelectedFile(file);
                // Show local file name in input
                onChange(`[Pending Upload] ${file.name}`);
                return;
            }

            if (!onUpload) return;

            setIsUploading(true);
            setUploadProgress(0);

            try {
                // Simulate progress (actual progress would come from XMLHttpRequest)
                const progressInterval = setInterval(() => {
                    setUploadProgress(prev => {
                        if (prev >= 90) {
                            clearInterval(progressInterval);
                            return prev;
                        }
                        return prev + 10;
                    });
                }, 200);

                const resultPath = await onUpload(file);

                clearInterval(progressInterval);
                setUploadProgress(100);

                onChange(resultPath);
                setSelectedFile(null);
            } catch (error) {
                console.error('Upload error:', error);
                alert(t('createContent.uploadFailed'));
            } finally {
                setIsUploading(false);
                setUploadProgress(0);
            }
        };

        const handleBrowseClick = () => {
            fileInputRef.current?.click();
        };

        const handleClearFile = () => {
            setSelectedFile(null);
            if (value.startsWith('[Pending Upload]')) {
                onChange('');
            }
            if (fileInputRef.current) {
                fileInputRef.current.value = '';
            }
        };

        const isPendingUpload = value.startsWith('[Pending Upload]');

        return (
            <div className={cn('space-y-2', className)}>
                {isPendingUpload ? (
                    // File selected - show file info card
                    <div className="flex gap-2">
                        <div className="flex-1 flex items-center gap-3 p-3 rounded-lg border border-blue-300 bg-blue-50 dark:bg-blue-900/20 dark:border-blue-700">
                            <FileVideo className="h-5 w-5 text-blue-500 flex-shrink-0" />
                            <div className="flex-1 min-w-0">
                                <p className="text-sm font-medium text-blue-700 dark:text-blue-300 truncate">
                                    {selectedFile?.name || t('createContent.pendingUpload')}
                                </p>
                                <p className="text-xs text-blue-500 dark:text-blue-400">
                                    {selectedFile ? `${(selectedFile.size / (1024 * 1024)).toFixed(1)} MB` : t('createContent.uploadVideo')}
                                </p>
                            </div>
                            <button
                                type="button"
                                onClick={handleClearFile}
                                className="p-1 rounded hover:bg-blue-200 dark:hover:bg-blue-800 text-blue-500"
                            >
                                <X className="h-4 w-4" />
                            </button>
                        </div>
                        <Button
                            type="button"
                            variant="outline"
                            onClick={handleBrowseClick}
                            disabled={disabled || isUploading}
                            className="whitespace-nowrap"
                        >
                            <Upload className="h-4 w-4 mr-2" />
                            {t('common.change')}
                        </Button>
                    </div>
                ) : (
                    // No file - show URL input + upload button
                    <div className="flex gap-2">
                        <div className="relative flex-1">
                            <Input
                                value={value}
                                onChange={(e) => onChange(e.target.value)}
                                placeholder={placeholder || t('createContent.videoUrlPlaceholder')}
                                disabled={disabled || isUploading}
                                className="pr-10"
                            />
                            {value && !isUploading && (
                                <button
                                    type="button"
                                    onClick={handleClearFile}
                                    className="absolute inset-y-0 right-2 flex items-center text-gray-400 hover:text-gray-600"
                                >
                                    <X className="h-4 w-4" />
                                </button>
                            )}
                        </div>

                        <Button
                            type="button"
                            variant="outline"
                            onClick={handleBrowseClick}
                            disabled={disabled || isUploading}
                            className="whitespace-nowrap"
                        >
                            <Upload className="h-4 w-4 mr-2" />
                            {t('createContent.uploadVideo')}
                        </Button>
                    </div>
                )}

                <input
                    ref={fileInputRef}
                    type="file"
                    accept={ACCEPTED_EXTENSIONS}
                    onChange={handleFileSelect}
                    className="hidden"
                />

                {isUploading && (
                    <div className="space-y-1">
                        <Progress value={uploadProgress} className="h-2" />
                        <p className="text-xs text-muted-foreground">
                            {t('createContent.uploading')} {uploadProgress}%
                        </p>
                    </div>
                )}

                {isPendingUpload && !isUploading && (
                    <p className="text-xs text-blue-600 dark:text-blue-400 flex items-center gap-1">
                        <span className="w-2 h-2 rounded-full bg-blue-500 animate-pulse" />
                        {t('createContent.pendingUpload')}
                    </p>
                )}

                <p className="text-xs text-muted-foreground">
                    {t('createContent.supportedFormats')}
                </p>

                {error && (
                    <p className="text-sm text-red-600">{error}</p>
                )}
            </div>
        );
    }

    export default VideoUploadField;