# All files / src/components/admin BulkImportContentDialog.tsx

0% Statements 0/62
0% Branches 0/22
0% Functions 0/12
0% Lines 0/59

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

     'use client';

    import { useRef, useState, type ChangeEvent } from 'react';
    import { useMutation } from '@tanstack/react-query';
    import { z } from 'zod';
    import { Button } from '@/components/ui/button';
    import { Label } from '@/components/ui/label';
    import { Textarea } from '@/components/ui/textarea';
    import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog';
    import { Alert, AlertDescription } from '@/components/ui/alert';
    import { AlertCircle, Upload, CheckCircle2 } from 'lucide-react';
    import { contentService } from '@/services';
    import { useTranslation } from 'react-i18next';
    import { extractErrorMessage } from '@/lib/error-message';
    import useLoadNamespace from '@/hooks/useLoadNamespace';

    interface Props {
        open: boolean;
        onOpenChange: (open: boolean) => void;
        onSuccess: () => void;
        /** Backend category_type: 'vod', 'series', 'kids', 'anime' */
        contentType: string;
        /** Human-readable label for this content type, e.g. "Movies", "Series", "Kids", "Anime" */
        categoryLabel: string;
    }

    export default function BulkImportContentDialog({ open, onOpenChange, onSuccess, contentType, categoryLabel }: Props) {
        useLoadNamespace('admin/bulkImportContent');
        const { t } = useTranslation(['admin/bulkImportContent', 'translation']);
        const [text, setText] = useState('');
        const [error, setError] = useState<string | null>(null);
        const [importResult, setImportResult] = useState<{ total: number; success: number; failed: number } | null>(null);
        const fileInputRef = useRef<HTMLInputElement | null>(null);

        const schema = z.object({
            text: z.string().min(1, t('bulkImportContent.validation.pasteM3uContent')),
        });

        const mutation = useMutation({
            mutationFn: async () => {
                setError(null);
                setImportResult(null);
                const parsed = schema.safeParse({ text });
                if (!parsed.success) {
                    const first = parsed.error.issues?.[0]?.message;
                    throw new Error(first || t('bulkImportContent.validation.invalidData'));
                }
                const res = await contentService.bulkImportContent({
                    text: parsed.data.text,
                    category_type: contentType,
                });
                if (!res.success) {
                    throw new Error(extractErrorMessage(res.error, t('bulkImportContent.validation.importFailed')));
                }
                const items = res.data || [];
                const successCount = items.filter(i => !i.error).length;
                const failedCount = items.filter(i => i.error).length;
                setImportResult({ total: items.length, success: successCount, failed: failedCount });
                return items;
            },
            onSuccess: () => {
                onSuccess();
            },
            onError: (e: unknown) => {
                const msg = e instanceof Error ? e.message : t('bulkImportContent.validation.unknownError');
                setError(msg);
            },
        });

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

        const handleFileChange = async (e: ChangeEvent<HTMLInputElement>) => {
            const file = e.target.files?.[0];
            if (!file) return;

            try {
                const fileText = await file.text();
                setError(null);
                setText(fileText);
            } catch {
                setError(t('bulkImportContent.validation.fileReadFailed'));
            } finally {
                e.target.value = '';
            }
        };

        const handleClose = (openState: boolean) => {
            if (!openState) {
                setText('');
                setError(null);
                setImportResult(null);
            }
            onOpenChange(openState);
        };

        return (
                <Dialog open={open} onOpenChange={handleClose}>
                <DialogContent className="max-w-2xl max-h-[85vh] overflow-y-auto">
                    <DialogHeader>
                        <DialogTitle>
                            {t('bulkImportContent.title', { category: categoryLabel })}
                        </DialogTitle>
                        <DialogDescription>
                            {t('bulkImportContent.description', { category: categoryLabel })}
                        </DialogDescription>
                    </DialogHeader>

                    {error && (
                        <Alert variant="destructive" className="mb-2">
                            <AlertCircle className="h-4 w-4" />
                            <AlertDescription>{error}</AlertDescription>
                        </Alert>
                    )}

                    {importResult && (
                        <Alert className="mb-2 border-green-500/50 bg-green-500/10">
                            <CheckCircle2 className="h-4 w-4 text-green-500" />
                            <AlertDescription className="text-green-700 dark:text-green-400">
                                {t(importResult.failed > 0 ? 'bulkImportContent.resultWithFailed' : 'bulkImportContent.result', {
                                    success: importResult.success,
                                    total: importResult.total,
                                    failed: importResult.failed,
                                })}
                            </AlertDescription>
                        </Alert>
                    )}

                    <div className="grid gap-4 py-2">
                        <div className="grid gap-2">
                            <div className="flex items-center justify-between gap-3">
                                <Label>{t('bulkImportContent.labels.playlist')}</Label>
                                <div className="flex items-center gap-2">
                                    <input
                                        ref={fileInputRef}
                                        type="file"
                                        accept=".m3u,.m3u8,text/plain"
                                        className="hidden"
                                        onChange={handleFileChange}
                                    />
                                    <Button type="button" variant="outline" size="sm" onClick={handleChooseFile}>
                                        {t('bulkImportContent.buttons.chooseFile')}
                                    </Button>
                                </div>
                            </div>
                            <Textarea
                                rows={12}
                                value={text}
                                onChange={(e) => setText(e.target.value)}
                                className="h-64 max-h-[60vh] overflow-y-auto resize-y font-mono text-xs"
                                placeholder={`#EXTM3U\n\n#EXTINF:-1 tvg-name="Title" tvg-logo="https://image.example.com/poster.jpg" group-title="${categoryLabel}",Title\nhttps://example.com/stream.m3u8\n\n#EXTINF:-1 tvg-name="Another Title" tvg-logo="https://image.example.com/poster2.jpg" group-title="${categoryLabel}",Another Title\nhttps://example.com/stream2.m3u8`}
                            />
                        </div>
                    </div>

                    <DialogFooter>
                        <Button variant="outline" onClick={() => handleClose(false)} disabled={mutation.isPending}>
                            {t('bulkImportContent.buttons.cancel')}
                        </Button>
                        <Button onClick={() => mutation.mutate()} disabled={mutation.isPending || !text.trim()}>
                            <Upload className="h-4 w-4 mr-2" />
                            {mutation.isPending
                                ? t('bulkImportContent.buttons.importing')
                                : t('bulkImportContent.buttons.import', { category: categoryLabel })}
                        </Button>
                    </DialogFooter>
                </DialogContent>
            </Dialog>
        );
    }