File size: 1,214 Bytes
394073d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState } from 'react'
import axios from 'axios'

export const useSingleAnalysis = (onAnalysisComplete) => {
  const [isLoading, setIsLoading] = useState(false);
  const [isError, setIsError] = useState(null);

  const runAnalysis = async (location, filters) => {
    if (!location || location.isSearch) return;
    setIsLoading(true);
    setIsError(null);

    const payload = {
      location: { lat: location.lat, lng: location.lng },
      params: {
        bufferKm: filters.radius,
        maxCloudCover: filters.cloudCover,
        daysBack: filters.daysRange,
        model: filters.model
      }
    };

    try {
      const response = await axios.post('/api/analyze', payload);
      if (response.data.success && onAnalysisComplete) {
        onAnalysisComplete(response.data);
      } else {
        setIsError(response.data.error || 'Server returned an unsuccessful status');
      }
    } catch (error) {
      console.error('Connection error', error);
      const message = error.response?.data?.error || 'Backend connection error. Please try again later.'
      setIsError(message);
    } finally {
      setIsLoading(false);
    }
  };

  return { runAnalysis, isLoading, isError };
};