File size: 1,102 Bytes
cd99321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useEffect, useState } from 'react'
import { airtrailApi } from '../api/client'
import { useAddonStore } from '../store/addonStore'

/**
 * Resolves whether the current user can use AirTrail in a trip: the addon must
 * be enabled globally AND the user must have a working connection. Drives the
 * "AirTrail Import/Sync" button visibility in the Transport panel.
 */
export function useAirtrailConnection() {
  const airtrailEnabled = useAddonStore(s => s.isEnabled('airtrail'))
  const [connected, setConnected] = useState(false)
  const [loading, setLoading] = useState(false)

  useEffect(() => {
    if (!airtrailEnabled) {
      setConnected(false)
      return
    }
    let cancelled = false
    setLoading(true)
    airtrailApi
      .status()
      .then(d => { if (!cancelled) setConnected(!!d.connected) })
      .catch(() => { if (!cancelled) setConnected(false) })
      .finally(() => { if (!cancelled) setLoading(false) })
    return () => { cancelled = true }
  }, [airtrailEnabled])

  return { airtrailEnabled, connected, available: airtrailEnabled && connected, loading }
}