File size: 4,211 Bytes
f051f8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use client';

import React, { useState, useCallback, useEffect } from 'react';
import { Upload, X, Image as ImageIcon } from 'lucide-react';
import { cn } from '@/lib/utils';

interface ImageUploadZoneProps {
  onImageSelected: (file: File, previewUrl: string) => void;
  onClear: () => void;
  disabled?: boolean;
  externalPreview?: string | null;
}

export function ImageUploadZone({ onImageSelected, onClear, disabled, externalPreview }: ImageUploadZoneProps) {
  const [isDragging, setIsDragging] = useState(false);
  const [preview, setPreview] = useState<string | null>(null);

  // Sync with external preview (from parent)
  useEffect(() => {
    if (externalPreview !== undefined) {
      setPreview(externalPreview);
    }
  }, [externalPreview]);

  const handleFile = useCallback((file: File) => {
    if (file && file.type.startsWith('image/')) {
      const url = URL.createObjectURL(file);
      setPreview(url);
      onImageSelected(file, url);
    }
  }, [onImageSelected]);

  const onDrop = useCallback((e: React.DragEvent) => {
    e.preventDefault();
    setIsDragging(false);
    if (disabled) return;

    const file = e.dataTransfer.files?.[0];
    if (file) handleFile(file);
  }, [disabled, handleFile]);

  const onChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
    if (disabled) return;
    const file = e.target.files?.[0];
    if (file) handleFile(file);
  }, [disabled, handleFile]);

  const handleClear = (e: React.MouseEvent) => {
    e.stopPropagation();
    e.preventDefault();
    setPreview(null);
    onClear();
  };

  return (
    <div
      onDragOver={(e) => { e.preventDefault(); setIsDragging(true); }}
      onDragLeave={() => setIsDragging(false)}
      onDrop={onDrop}
      className={cn(
        "relative rounded-xl border-2 border-dashed transition-all duration-200 group cursor-pointer",
        isDragging ? "border-indigo-500 bg-indigo-500/10" : "border-slate-800 hover:border-slate-700 hover:bg-slate-800/50",
        disabled && "opacity-50 cursor-not-allowed",
        preview ? "p-0 overflow-hidden aspect-video bg-black" : "p-12"
      )}
    >
      {!preview && (
        <input
          type="file"
          className="absolute inset-0 opacity-0 cursor-pointer disabled:cursor-not-allowed z-10"
          onChange={onChange}
          accept="image/*"
          disabled={disabled}
        />
      )}

      {preview ? (
        <div className="relative w-full h-full flex items-center justify-center">
          <img src={preview} alt="Upload preview" className="max-w-full max-h-full object-contain" />
          
          {/* Close Button */}
          <button
            onClick={handleClear}
            disabled={disabled}
            className="absolute top-2 right-2 p-1.5 rounded-full bg-red-500/80 hover:bg-red-500 text-white z-20 transition-all opacity-0 group-hover:opacity-100"
            title="Clear image"
          >
            <X className="w-4 h-4" />
          </button>

          <div className="absolute inset-0 bg-black/40 opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center pointer-events-none">
            <p className="text-white text-xs font-medium">Click top-right to clear or drop new image</p>
          </div>
          
          {/* Hidden input for re-uploading via clicking on the preview (non-close button area) */}
          <input
            type="file"
            className="absolute inset-0 opacity-0 cursor-pointer z-10"
            onChange={onChange}
            accept="image/*"
            disabled={disabled}
          />
        </div>
      ) : (
        <div className="flex flex-col items-center justify-center space-y-4 text-center">
          <div className="p-3 rounded-full bg-slate-800 text-slate-400 group-hover:text-indigo-400 transition-colors">
            <Upload className="w-8 h-8" />
          </div>
          <div>
            <p className="text-sm font-medium text-slate-200">
              Drop your satellite image here
            </p>
            <p className="text-xs text-slate-500 mt-1">
              Supports JPEG, PNG up to 10MB
            </p>
          </div>
        </div>
      )}
    </div>
  );
}