# All files / src/components/settings StorageBackendCard.tsx

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

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

     'use client'

    import { Card, CardHeader, CardTitle, CardDescription, CardContent } from '@/components/ui/card'
    import { Button } from '@/components/ui/button'
    import { Input } from '@/components/ui/input'
    import { Label } from '@/components/ui/label'
    import { Alert, AlertDescription } from '@/components/ui/alert'
    import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
    import { Database, CloudArrowUp, CloudCheck, CloudSlash, Upload, Download } from '@phosphor-icons/react'
    import { type StorageBackend } from '@/lib/storage'

    interface StorageBackendCardProps {
      storageBackend: StorageBackend
      flaskUrl: string
      flaskConnectionStatus: 'unknown' | 'connected' | 'failed'
      testingConnection: boolean
      envVarSet: boolean
      onStorageBackendChange: (backend: StorageBackend) => void
      onFlaskUrlChange: (url: string) => void
      onTestConnection: () => Promise<void>
      onSaveConfig: () => Promise<void>
      onMigrateToFlask: () => Promise<void>
      onMigrateToIndexedDB: () => Promise<void>
    }

    export function StorageBackendCard({
      storageBackend,
      flaskUrl,
      flaskConnectionStatus,
      testingConnection,
      envVarSet,
      onStorageBackendChange,
      onFlaskUrlChange,
      onTestConnection,
      onSaveConfig,
      onMigrateToFlask,
      onMigrateToIndexedDB,
    }: StorageBackendCardProps) {
      return (
        <Card>
          <CardHeader>
            <CardTitle className="flex items-center gap-2">
              <CloudArrowUp weight="duotone" size={24} />
              Storage Backend
            </CardTitle>
            <CardDescription>
              Choose where your snippets are stored
            </CardDescription>
          </CardHeader>
          <CardContent className="space-y-6">
            {envVarSet && (
              <Alert className="border-accent bg-accent/10">
                <AlertDescription className="flex items-center gap-2">
                  <CloudCheck weight="fill" size={16} className="text-accent" />
                  <span>
                    Storage backend is configured via <code className="px-1.5 py-0.5 rounded bg-muted text-xs font-mono">NEXT_PUBLIC_FLASK_BACKEND_URL</code> environment variable and cannot be changed here.
                  </span>
                </AlertDescription>
              </Alert>
            )}

            <RadioGroup
              value={storageBackend}
              onValueChange={(value) => onStorageBackendChange(value as StorageBackend)}
            >
              <div className="flex items-start space-x-3 space-y-0">
                <RadioGroupItem value="indexeddb" id="storage-indexeddb" disabled={envVarSet} />
                <div className="flex-1">
                  <Label htmlFor="storage-indexeddb" className={`font-semibold ${envVarSet ? 'opacity-50' : 'cursor-pointer'}`}>
                    IndexedDB (Local Browser Storage)
                  </Label>
                  <p className="text-sm text-muted-foreground mt-1">
                    Store snippets locally in your browser. Data persists on this device only.
                  </p>
                </div>
              </div>

              <div className="flex items-start space-x-3 space-y-0 mt-4">
                <RadioGroupItem value="flask" id="storage-flask" disabled={envVarSet} />
                <div className="flex-1">
                  <Label htmlFor="storage-flask" className={`font-semibold ${envVarSet ? 'opacity-50' : 'cursor-pointer'}`}>
                    Flask Backend (Remote Server)
                  </Label>
                  <p className="text-sm text-muted-foreground mt-1">
                    Store snippets on a Flask backend server. Data is accessible from any device.
                  </p>
                </div>
              </div>
            </RadioGroup>

            {storageBackend === 'flask' && (
              <div className="space-y-4 p-4 border border-border rounded-lg bg-muted/50">
                <div>
                  <Label htmlFor="flask-url">Flask Backend URL</Label>
                  <div className="flex gap-2 mt-2">
                    <Input
                      id="flask-url"
                      type="url"
                      placeholder="http://localhost:5000"
                      value={flaskUrl}
                      onChange={(e) => onFlaskUrlChange(e.target.value)}
                      disabled={envVarSet}
                    />
                    <Button
                      onClick={onTestConnection}
                      variant="outline"
                      disabled={testingConnection || !flaskUrl}
                    >
                      {testingConnection ? 'Testing...' : 'Test'}
                    </Button>
                  </div>
                  {flaskConnectionStatus === 'connected' && (
                    <div className="flex items-center gap-2 mt-2 text-sm text-green-600">
                      <CloudCheck weight="fill" size={16} />
                      Connected successfully
                    </div>
                  )}
                  {flaskConnectionStatus === 'failed' && (
                    <div className="flex items-center gap-2 mt-2 text-sm text-destructive">
                      <CloudSlash weight="fill" size={16} />
                      Connection failed
                    </div>
                  )}
                </div>

                <div className="pt-2 space-y-2">
                  <Button
                    onClick={onMigrateToFlask}
                    variant="outline"
                    size="sm"
                    className="w-full gap-2"
                  >
                    <Upload weight="bold" size={16} />
                    Migrate IndexedDB Data to Flask
                  </Button>
                  <Button
                    onClick={onMigrateToIndexedDB}
                    variant="outline"
                    size="sm"
                    className="w-full gap-2"
                  >
                    <Download weight="bold" size={16} />
                    Migrate Flask Data to IndexedDB
                  </Button>
                </div>
              </div>
            )}

            <div className="pt-2">
              <Button onClick={onSaveConfig} className="gap-2" disabled={envVarSet}>
                <Database weight="bold" size={16} />
                Save Storage Settings
              </Button>
            </div>
          </CardContent>
        </Card>
      )
    }