All files / src/components BackupModal.tsx

0% Statements 0/242
0% Branches 0/1
0% Functions 0/1
0% Lines 0/242

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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
import { useEffect, useState } from 'react';
import { Download, X, Settings, Bell, FileText, Plug, Printer, Palette, Wrench, Archive, Loader2, Key, AlertTriangle } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import { Card, CardContent } from './Card';
import { Button } from './Button';
import { Toggle } from './Toggle';
 
interface BackupCategory {
  id: string;
  labelKey: string;
  defaultLabel: string;
  icon: React.ReactNode;
  default: boolean;
  description: string;
}
 
const BACKUP_CATEGORIES: BackupCategory[] = [
  {
    id: 'settings',
    labelKey: 'backup.categories.settings',
    defaultLabel: 'App Settings',
    icon: <Settings className="w-4 h-4" />,
    default: true,
    description: 'Language, theme, update preferences',
  },
  {
    id: 'notifications',
    labelKey: 'backup.categories.notifications',
    defaultLabel: 'Notification Providers',
    icon: <Bell className="w-4 h-4" />,
    default: true,
    description: 'ntfy, Pushover, Discord, etc.',
  },
  {
    id: 'templates',
    labelKey: 'backup.categories.templates',
    defaultLabel: 'Notification Templates',
    icon: <FileText className="w-4 h-4" />,
    default: true,
    description: 'Custom message templates',
  },
  {
    id: 'smart_plugs',
    labelKey: 'backup.categories.smartPlugs',
    defaultLabel: 'Smart Plugs',
    icon: <Plug className="w-4 h-4" />,
    default: true,
    description: 'Tasmota plug configurations',
  },
  {
    id: 'printers',
    labelKey: 'backup.categories.printers',
    defaultLabel: 'Printers',
    icon: <Printer className="w-4 h-4" />,
    default: false,
    description: 'Printer info (access codes excluded)',
  },
  {
    id: 'filaments',
    labelKey: 'backup.categories.filaments',
    defaultLabel: 'Filament Inventory',
    icon: <Palette className="w-4 h-4" />,
    default: false,
    description: 'Filament types and costs',
  },
  {
    id: 'maintenance',
    labelKey: 'backup.categories.maintenance',
    defaultLabel: 'Maintenance Types',
    icon: <Wrench className="w-4 h-4" />,
    default: false,
    description: 'Custom maintenance schedules',
  },
  {
    id: 'archives',
    labelKey: 'backup.categories.archives',
    defaultLabel: 'Print Archives',
    icon: <Archive className="w-4 h-4" />,
    default: false,
    description: 'All print data + files (3MF, thumbnails, photos)',
  },
];
 
interface BackupModalProps {
  onClose: () => void;
  onExport: (categories: Record<string, boolean>) => Promise<void>;
}
 
export function BackupModal({ onClose, onExport }: BackupModalProps) {
  const { t } = useTranslation();
  const [selected, setSelected] = useState<Record<string, boolean>>(() => {
    const initial: Record<string, boolean> = {};
    BACKUP_CATEGORIES.forEach((cat) => {
      initial[cat.id] = cat.default;
    });
    return initial;
  });
  const [includeAccessCodes, setIncludeAccessCodes] = useState(false);
  const [isExporting, setIsExporting] = useState(false);
 
  // Close on Escape key
  useEffect(() => {
    const handleKeyDown = (e: KeyboardEvent) => {
      if (e.key === 'Escape') onClose();
    };
    window.addEventListener('keydown', handleKeyDown);
    return () => window.removeEventListener('keydown', handleKeyDown);
  }, [onClose]);
 
  const toggleCategory = (id: string) => {
    setSelected((prev) => ({ ...prev, [id]: !prev[id] }));
  };
 
  const selectAll = () => {
    const all: Record<string, boolean> = {};
    BACKUP_CATEGORIES.forEach((cat) => {
      all[cat.id] = true;
    });
    setSelected(all);
  };
 
  const selectNone = () => {
    const none: Record<string, boolean> = {};
    BACKUP_CATEGORIES.forEach((cat) => {
      none[cat.id] = false;
    });
    setSelected(none);
  };
 
  const selectedCount = Object.values(selected).filter(Boolean).length;
 
  const handleExport = async () => {
    setIsExporting(true);
    try {
      await onExport({ ...selected, access_codes: includeAccessCodes && selected.printers });
    } finally {
      setIsExporting(false);
    }
  };
 
  return (
    <div
      className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4"
      onClick={isExporting ? undefined : onClose}
    >
      <Card className="w-full max-w-lg" onClick={(e: React.MouseEvent) => e.stopPropagation()}>
        <CardContent className="p-0">
          {/* Header */}
          <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary">
            <div className="flex items-center gap-3">
              <div className="p-2 rounded-full bg-bambu-green/20 text-bambu-green">
                <Download className="w-5 h-5" />
              </div>
              <div>
                <h3 className="text-lg font-semibold text-white">
                  {t('backup.exportTitle', { defaultValue: 'Export Backup' })}
                </h3>
                <p className="text-sm text-bambu-gray">
                  {t('backup.selectCategories', { defaultValue: 'Select data to include' })}
                </p>
              </div>
            </div>
            <button
              onClick={onClose}
              className="p-2 hover:bg-bambu-dark-tertiary rounded-lg transition-colors"
            >
              <X className="w-5 h-5" />
            </button>
          </div>
 
          {/* Quick actions */}
          <div className="flex gap-2 px-4 pt-4">
            <button
              onClick={selectAll}
              disabled={isExporting}
              className="text-sm text-bambu-green hover:text-bambu-green/80 disabled:opacity-50 disabled:cursor-not-allowed"
            >
              {t('common.selectAll', { defaultValue: 'Select All' })}
            </button>
            <span className="text-bambu-gray">|</span>
            <button
              onClick={selectNone}
              disabled={isExporting}
              className="text-sm text-bambu-gray hover:text-white disabled:opacity-50 disabled:cursor-not-allowed"
            >
              {t('common.selectNone', { defaultValue: 'Select None' })}
            </button>
          </div>
 
          {/* Categories */}
          <div className={`p-4 space-y-2 max-h-[400px] overflow-y-auto ${isExporting ? 'opacity-50 pointer-events-none' : ''}`}>
            {BACKUP_CATEGORIES.map((category) => (
              <label
                key={category.id}
                className={`flex items-center gap-3 p-3 rounded-lg cursor-pointer transition-colors ${
                  selected[category.id]
                    ? 'bg-bambu-green/10 border border-bambu-green/30'
                    : 'bg-bambu-dark hover:bg-bambu-dark-tertiary border border-transparent'
                }`}
              >
                <input
                  type="checkbox"
                  checked={selected[category.id]}
                  onChange={() => toggleCategory(category.id)}
                  disabled={isExporting}
                  className="w-4 h-4 rounded border-bambu-gray bg-bambu-dark text-bambu-green focus:ring-bambu-green focus:ring-offset-0"
                />
                <div className={`${selected[category.id] ? 'text-bambu-green' : 'text-bambu-gray'}`}>
                  {category.icon}
                </div>
                <div className="flex-1">
                  <div className="text-white text-sm font-medium">
                    {t(category.labelKey, { defaultValue: category.defaultLabel })}
                  </div>
                  <div className="text-xs text-bambu-gray">{category.description}</div>
                </div>
              </label>
            ))}
          </div>
 
          {/* Archive warning */}
          {selected.archives && (
            <div className="mx-4 mb-2 p-3 rounded-lg bg-yellow-500/10 border border-yellow-500/30">
              <div className="flex items-start gap-2 text-sm">
                <Archive className="w-4 h-4 text-yellow-500 mt-0.5 flex-shrink-0" />
                <div className="text-yellow-200 dark:text-yellow-200 text-yellow-700">
                  <span className="font-medium">ZIP file will be created.</span>
                  <span className="text-yellow-600 dark:text-yellow-200/70"> Includes all 3MF files, thumbnails, timelapses, and photos. This may take a while and result in a large file.</span>
                </div>
              </div>
            </div>
          )}
 
          {/* Access codes option - only shown when printers are selected */}
          {selected.printers && (
            <div className="mx-4 mb-2 p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary">
              <div className="flex items-center justify-between">
                <div className="flex items-start gap-2">
                  <Key className="w-4 h-4 text-orange-500 dark:text-orange-400 mt-0.5 flex-shrink-0" />
                  <div>
                    <p className="text-sm font-medium text-white">Include Access Codes</p>
                    <p className="text-xs text-bambu-gray">For transferring to another machine</p>
                  </div>
                </div>
                <Toggle checked={includeAccessCodes} onChange={setIncludeAccessCodes} />
              </div>
              {includeAccessCodes && (
                <div className="mt-2 p-2 rounded bg-orange-500/10 border border-orange-500/30">
                  <div className="flex items-start gap-2 text-xs">
                    <AlertTriangle className="w-3 h-3 text-orange-500 dark:text-orange-400 mt-0.5 flex-shrink-0" />
                    <span className="text-orange-700 dark:text-orange-200">
                      Access codes will be included in plain text. Keep this backup file secure!
                    </span>
                  </div>
                </div>
              )}
            </div>
          )}
 
          {/* Footer */}
          <div className="flex items-center justify-between p-4 border-t border-bambu-dark-tertiary">
            <span className="text-sm text-bambu-gray">
              {t('backup.selectedCount', {
                count: selectedCount,
                defaultValue: `${selectedCount} categories selected`,
              })}
            </span>
            <div className="flex gap-3">
              <Button variant="secondary" onClick={onClose} disabled={isExporting}>
                {t('common.cancel', { defaultValue: 'Cancel' })}
              </Button>
              <Button
                onClick={handleExport}
                disabled={selectedCount === 0 || isExporting}
                className="bg-bambu-green hover:bg-bambu-green-dark disabled:opacity-50 disabled:cursor-not-allowed min-w-[100px]"
              >
                {isExporting ? (
                  <>
                    <Loader2 className="w-4 h-4 mr-2 animate-spin" />
                    {t('backup.exporting', { defaultValue: 'Exporting...' })}
                  </>
                ) : (
                  <>
                    <Download className="w-4 h-4 mr-2" />
                    {t('backup.export', { defaultValue: 'Export' })}
                  </>
                )}
              </Button>
            </div>
          </div>
        </CardContent>
      </Card>
    </div>
  );
}