# All files / src/hooks useStorageConfig.ts

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

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

     import { useState, useCallback } from 'react'
    import { toast } from 'sonner'
    import {
      saveStorageConfig,
      loadStorageConfig,
      FlaskStorageAdapter,
      type StorageBackend
    } from '@/lib/storage'

    export function useStorageConfig() {
      const [storageBackend, setStorageBackend] = useState<StorageBackend>('indexeddb')
      const [flaskUrl, setFlaskUrl] = useState('')
      const [flaskConnectionStatus, setFlaskConnectionStatus] = useState<'unknown' | 'connected' | 'failed'>('unknown')
      const [testingConnection, setTestingConnection] = useState(false)
      const [envVarSet, setEnvVarSet] = useState(false)

      const testFlaskConnection = useCallback(async (url: string) => {
        setTestingConnection(true)
        try {
          const adapter = new FlaskStorageAdapter(url)
          const connected = await adapter.testConnection()
          setFlaskConnectionStatus(connected ? 'connected' : 'failed')
          return connected
        } catch (error) {
          console.error('Connection test failed:', error)
          setFlaskConnectionStatus('failed')
          return false
        } finally {
          setTestingConnection(false)
        }
      }, [])

      const loadConfig = useCallback(() => {
        const config = loadStorageConfig()
        const envFlaskUrl = process.env.NEXT_PUBLIC_FLASK_BACKEND_URL
        const isEnvSet = Boolean(envFlaskUrl)

        setEnvVarSet(isEnvSet)
        setStorageBackend(config.backend)
        setFlaskUrl(config.flaskUrl || envFlaskUrl || 'http://localhost:5000')
      }, [])

      const handleTestConnection = useCallback(async () => {
        await testFlaskConnection(flaskUrl)
      }, [flaskUrl, testFlaskConnection])

      const handleSaveStorageConfig = useCallback(async (onSuccess?: () => Promise<void>) => {
        if (storageBackend === 'flask') {
          if (!flaskUrl) {
            toast.error('Please enter a Flask backend URL')
            return
          }

          const connected = await testFlaskConnection(flaskUrl)
          if (!connected) {
            toast.error('Cannot connect to Flask backend. Please check the URL and ensure the server is running.')
            return
          }
        }

        saveStorageConfig({
          backend: storageBackend,
          flaskUrl: storageBackend === 'flask' ? flaskUrl : undefined
        })

        toast.success('Storage backend updated successfully')

        if (onSuccess) {
          await onSuccess()
        }
      }, [storageBackend, flaskUrl, testFlaskConnection])

      return {
        storageBackend,
        setStorageBackend,
        flaskUrl,
        setFlaskUrl,
        flaskConnectionStatus,
        setFlaskConnectionStatus,
        testingConnection,
        envVarSet,
        loadConfig,
        handleTestConnection,
        handleSaveStorageConfig,
      }
    }