All files / components/records ExportDialog.tsx

94.28% Statements 33/35
80% Branches 16/20
66.66% Functions 4/6
94.11% Lines 32/34

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                                                    124x 124x   124x 2x 2x 2x   2x     124x 1x 1x 1x 1x 1x 1x   1x 1x           1x 1x 1x     1x     1x 1x 1x 1x 1x 1x 1x   1x       124x 124x   124x             1x                                                                                                                              
/**
 * Bulk export dialog
 * Exports current filtered or all records as a JSON file
 */
 
import { useState } from 'react';
import { AppDialog } from '@/components/common/AppDialog';
import { Button } from '@/components/ui/button';
import { Download } from 'lucide-react';
import { getRecords } from '@/services/records.service';
 
interface ExportDialogProps {
	open: boolean;
	onOpenChange: (open: boolean) => void;
	collection: string;
	filterExpression: string;
	total: number;
}
 
export default function ExportDialog({
	open,
	onOpenChange,
	collection,
	filterExpression,
	total,
}: ExportDialogProps) {
	const [exportAll, setExportAll] = useState(false);
	const [isExporting, setIsExporting] = useState(false);
 
	const handleClose = (open: boolean) => {
		Eif (!open) {
			setExportAll(false);
			setIsExporting(false);
		}
		onOpenChange(open);
	};
 
	const handleExport = async () => {
		setIsExporting(true);
		try {
			const allRecords = [];
			let skip = 0;
			const pageSize = 100; // API max is 100 (le=100 in backend)
			const filterToUse = exportAll ? '' : filterExpression;
 
			while (true) {
				const res = await getRecords({
					collection,
					skip,
					limit: pageSize,
					...(filterToUse ? { filter: filterToUse } : {}),
				});
				allRecords.push(...res.items);
				skip += res.items.length;
				Eif (skip >= res.total) break;
			}
 
			const blob = new Blob([JSON.stringify(allRecords, null, 2)], {
				type: 'application/json',
			});
			const url = URL.createObjectURL(blob);
			const a = document.createElement('a');
			a.href = url;
			a.download = `${collection}-export-${new Date().toISOString().slice(0, 10)}.json`;
			a.click();
			URL.revokeObjectURL(url);
			handleClose(false);
		} finally {
			setIsExporting(false);
		}
	};
 
	const hasFilter = Boolean(filterExpression);
	const exportCount = exportAll || !hasFilter ? total : total;
 
	return (
		<AppDialog
			open={open}
			onOpenChange={handleClose}
			title="Export Records"
			footer={
				<>
					<Button variant="outline" onClick={() => handleClose(false)} disabled={isExporting}>
						Cancel
					</Button>
					<Button onClick={handleExport} disabled={isExporting} className="gap-2">
						<Download className="h-4 w-4" />
						{isExporting ? 'Exporting…' : `Export ${exportCount} Record${exportCount === 1 ? '' : 's'}`}
					</Button>
				</>
			}
		>
			<div className="space-y-4">
				<p className="text-sm text-muted-foreground">
					Export records from <strong>{collection}</strong> as a JSON file.
				</p>
 
				{hasFilter ? (
					<div className="space-y-2">
						<p className="text-sm font-medium">What to export:</p>
						<label className="flex items-center gap-3 cursor-pointer rounded-lg border p-3 hover:bg-muted/50 transition-colors">
							<input
								type="radio"
								name="export-scope"
								checked={!exportAll}
								onChange={() => setExportAll(false)}
								className="accent-primary"
							/>
							<div>
								<p className="text-sm font-medium">Filtered records ({total})</p>
								<p className="text-xs text-muted-foreground font-mono truncate max-w-xs">
									{filterExpression}
								</p>
							</div>
						</label>
						<label className="flex items-center gap-3 cursor-pointer rounded-lg border p-3 hover:bg-muted/50 transition-colors">
							<input
								type="radio"
								name="export-scope"
								checked={exportAll}
								onChange={() => setExportAll(true)}
								className="accent-primary"
							/>
							<div>
								<p className="text-sm font-medium">All records</p>
								<p className="text-xs text-muted-foreground">
									Ignore the current filter and export everything
								</p>
							</div>
						</label>
					</div>
				) : (
					<p className="text-sm">
						All <strong>{total}</strong> record{total === 1 ? '' : 's'} will be exported.
					</p>
				)}
 
				<p className="text-xs text-muted-foreground">
					The file will be saved as{' '}
					<span className="font-mono">{collection}-export-{new Date().toISOString().slice(0, 10)}.json</span>
				</p>
			</div>
		</AppDialog>
	);
}