# All files / src/components/admin/app-versions AppVersionList.tsx

0% Statements 0/39
0% Branches 0/25
0% Functions 0/7
0% Lines 0/38

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

     import { useState } from 'react';
    import { AppVersion, appVersionService } from '@/services/appVersion';
    import { ResponsiveTable, ResponsiveTableColumn } from '@/components/ui/responsive-table';
    import { Badge } from '@/components/ui/badge';
    import { Button } from '@/components/ui/button';
    import { Download, Edit, Trash2, Smartphone, Tv, Loader2, Power, AlertTriangle } from 'lucide-react';
    import { toast } from 'sonner';
    import { ConfirmationDialog } from '@/components/ui/confirmation-dialog';
    import { formatBytes } from '@/utils/utils';
    import { format } from 'date-fns';
    import { useTranslation } from 'react-i18next';
    import { useLoadNamespace } from '@/hooks/useLoadNamespace';
    import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip';

    interface AppVersionListProps {
        versions: AppVersion[];
        isLoading: boolean;
        onRefresh: () => void;
        onEdit: (version: AppVersion) => void;
        onToggleActive?: (version: AppVersion) => void;
        onToggleMandatory?: (version: AppVersion) => void;
    }

    export function AppVersionList({
        versions,
        isLoading,
        onRefresh,
        onEdit,
        onToggleActive,
        onToggleMandatory
    }: AppVersionListProps) {
        useLoadNamespace('admin/appUpdates');
        const { t } = useTranslation('admin/appUpdates');

        const [deleteId, setDeleteId] = useState<number | null>(null);
        const [isDeleting, setIsDeleting] = useState(false);

        const handleDelete = async () => {
            if (!deleteId) return;
            setIsDeleting(true);
            try {
                const result = await appVersionService.deleteVersion(deleteId);
                if (result.success) {
                    toast.success(t('appUpdates.success.deleted'));
                    onRefresh();
                } else {
                    toast.error(result.error?.details || t('appUpdates.errors.deleteFailed'));
                }
            } catch (error) {
                toast.error(t('appUpdates.errors.deleteFailed'));
            } finally {
                setIsDeleting(false);
                setDeleteId(null);
            }
        };

        const columns: ResponsiveTableColumn<AppVersion>[] = [
            {
                key: 'app_type',
                label: t('appUpdates.table.platform'),
                render: (row: AppVersion) => (
                    <div className="flex items-center gap-2">
                        {row.app_type === 'phone' ? (
                            <div className="p-2 rounded-lg bg-blue-500/10">
                                <Smartphone className="h-5 w-5 text-blue-500" />
                            </div>
                        ) : (
                            <div className="p-2 rounded-lg bg-purple-500/10">
                                <Tv className="h-5 w-5 text-purple-500" />
                            </div>
                        )}
                        <div className="flex flex-col">
                            <span className="font-medium">
                                {row.app_type === 'phone'
                                    ? t('appUpdates.platforms.android_mobile')
                                    : t('appUpdates.platforms.android_tv')}
                            </span>
                            <span className="text-xs text-muted-foreground">
                                v{row.version} (#{row.version_code})
                            </span>
                        </div>
                    </div>
                )},
            {
                key: 'file_size',
                label: t('common.size', {}),
                render: (row: AppVersion) => (
                    <span className="text-sm font-medium">{formatBytes(row.file_size)}</span>
                )},
            {
                key: 'status',
                label: t('appUpdates.table.status'),
                render: (row: AppVersion) => (
                    <div className="flex flex-col gap-1">
                        <Badge
                            variant={row.is_active ? "default" : "secondary"}
                            className={row.is_active ? "bg-green-500 hover:bg-green-600" : ""}
                        >
                            <Power className="h-3 w-3 mr-1" />
                            {row.is_active ? t('appUpdates.status.active') : t('appUpdates.status.inactive')}
                        </Badge>
                        {row.force_update && (
                            <Badge variant="destructive" className="text-xs">
                                <AlertTriangle className="h-3 w-3 mr-1" />
                                {t('appUpdates.updateType.mandatory')}
                            </Badge>
                        )}
                    </div>
                )},
            {
                key: 'release_notes',
                label: t('appUpdates.form.releaseNotes'),
                render: (row: AppVersion) => (
                    <TooltipProvider>
                        <Tooltip>
                            <TooltipTrigger asChild>
                                <span className="text-sm text-muted-foreground truncate max-w-[200px] block cursor-help">
                                    {row.release_notes || '-'}
                                </span>
                            </TooltipTrigger>
                            {row.release_notes && (
                                <TooltipContent className="max-w-[300px]">
                                    <p className="whitespace-pre-wrap">{row.release_notes}</p>
                                </TooltipContent>
                            )}
                        </Tooltip>
                    </TooltipProvider>
                )},
            {
                key: 'created_at',
                label: t('appUpdates.table.releaseDate'),
                render: (row: AppVersion) => (
                    <span className="text-sm text-muted-foreground">
                        {format(new Date(row.created_at), 'MMM dd, yyyy')}
                    </span>
                )},
            {
                key: 'id',
                label: t('appUpdates.table.actions'),
                render: (row: AppVersion) => (
                    <div className="flex items-center gap-1">
                        <Button
                            variant="outline"
                            size="sm"
                            onClick={() => onEdit(row)}
                        >
                            <Edit className="h-3 w-3 mr-1" />
                            {t('appUpdates.actions.edit')}
                        </Button>
                        <Button
                            variant="outline"
                            size="sm"
                            className="text-red-500 border-red-500/20 hover:bg-red-500/10"
                            onClick={() => setDeleteId(row.id)}
                        >
                            <Trash2 className="h-3 w-3 mr-1" />
                            {t('appUpdates.actions.delete')}
                        </Button>
                        <Button
                            variant="ghost"
                            size="icon"
                            onClick={() => window.open(row.download_url, '_blank')}
                        >
                            <Download className="h-4 w-4" />
                        </Button>
                    </div>
                )},
        ];

        if (isLoading) {
            return (
                <div className="flex items-center justify-center py-12">
                    <Loader2 className="h-8 w-8 animate-spin text-blue-500" />
                </div>
            );
        }

        if (versions.length === 0) {
            return (
                <div className="flex flex-col items-center justify-center py-12 text-center">
                    <Download className="h-12 w-12 text-muted-foreground mb-4" />
                    <h3 className="text-lg font-semibold">{t('appUpdates.empty.title')}</h3>
                    <p className="text-sm text-muted-foreground max-w-md mt-2">
                        {t('appUpdates.empty.description')}
                    </p>
                </div>
            );
        }

        return (
            <>
                <ResponsiveTable
                    data={versions}
                    columns={columns}
                    keyExtractor={(item) => item.id}
                />

                <ConfirmationDialog
                    open={!!deleteId}
                    onOpenChange={(open) => !open && setDeleteId(null)}
                    onConfirm={handleDelete}
                    title={t('appUpdates.actions.delete')}
                    description={t('appUpdates.confirm.delete')}
                    confirmText={t('appUpdates.actions.delete')}
                    variant="danger"
                    isLoading={isDeleting}
                />
            </>
        );
    }